Files
test2/scripts/startup/bl_operators/mesh.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

251 lines
7.2 KiB
Python
Raw Normal View History

# SPDX-FileCopyrightText: 2009-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from bpy.types import Operator
from bpy.props import (
EnumProperty,
IntProperty,
)
from bpy.app.translations import pgettext_rpt as rpt_
2010-01-31 14:46:28 +00:00
class MeshMirrorUV(Operator):
"""Copy mirror UV coordinates on the X axis based on a mirrored mesh"""
bl_idname = "mesh.faces_mirror_uv"
bl_label = "Copy Mirrored UV Coords"
bl_options = {'REGISTER', 'UNDO'}
2010-01-31 14:46:28 +00:00
direction: EnumProperty(
2018-06-26 19:41:37 +02:00
name="Axis Direction",
items=(
('POSITIVE', "Positive", ""),
('NEGATIVE', "Negative", ""),
),
)
precision: IntProperty(
2018-06-26 19:41:37 +02:00
name="Precision",
description=("Tolerance for finding vertex duplicates"),
min=1, max=16,
soft_min=1, soft_max=16,
default=3,
)
# Returns has_active_UV_layer, double_warn.
def do_mesh_mirror_UV(self, mesh, DIR):
precision = self.precision
double_warn = 0
2010-01-31 14:46:28 +00:00
if not mesh.uv_layers.active:
# has_active_UV_layer, double_warn
return False, 0
2010-01-31 14:46:28 +00:00
# mirror lookups
mirror_gt = {}
mirror_lt = {}
vcos = (v.co.to_tuple(precision) for v in mesh.vertices)
for i, co in enumerate(vcos):
if co[0] >= 0.0:
double_warn += co in mirror_gt
mirror_gt[co] = i
if co[0] <= 0.0:
double_warn += co in mirror_lt
mirror_lt[co] = i
vmap = {}
for mirror_a, mirror_b in (
(mirror_gt, mirror_lt),
(mirror_lt, mirror_gt),
):
for co, i in mirror_a.items():
nco = (-co[0], co[1], co[2])
j = mirror_b.get(nco)
if j is not None:
vmap[i] = j
2010-01-31 14:46:28 +00:00
polys = mesh.polygons
loops = mesh.loops
uv_loops = mesh.uv_layers.active.data
nbr_polys = len(polys)
mirror_pm = {}
pmap = {}
puvs = [None] * nbr_polys
puvs_cpy = [None] * nbr_polys
puvsel = [None] * nbr_polys
pcents = [None] * nbr_polys
vidxs = [None] * nbr_polys
for i, p in enumerate(polys):
lstart = lend = p.loop_start
lend += p.loop_total
puvs[i] = tuple(uv.uv for uv in uv_loops[lstart:lend])
puvs_cpy[i] = tuple(uv.copy() for uv in puvs[i])
puvsel[i] = (False not in
2012-10-08 08:28:05 +00:00
(uv.select for uv in uv_loops[lstart:lend]))
2023-09-05 10:49:20 +10:00
# Vert index of the poly.
vidxs[i] = tuple(l.vertex_index for l in loops[lstart:lend])
pcents[i] = p.center
# Preparing next step finding matching polys.
mirror_pm[tuple(sorted(vidxs[i]))] = i
for i in range(nbr_polys):
# Find matching mirror poly.
tvidxs = [vmap.get(j) for j in vidxs[i]]
if None not in tvidxs:
tvidxs.sort()
j = mirror_pm.get(tuple(tvidxs))
if j is not None:
pmap[i] = j
2010-01-31 14:46:28 +00:00
for i, j in pmap.items():
if not puvsel[i] or not puvsel[j]:
continue
2023-04-13 13:14:05 +10:00
if DIR == 0 and pcents[i][0] < 0.0:
continue
2023-04-13 13:14:05 +10:00
if DIR == 1 and pcents[i][0] > 0.0:
continue
# copy UVs
uv1 = puvs[i]
uv2 = puvs_cpy[j]
2010-01-31 14:46:28 +00:00
# get the correct rotation
v1 = vidxs[j]
v2 = tuple(vmap[k] for k in vidxs[i])
2010-01-31 14:46:28 +00:00
if len(v1) == len(v2):
for k in range(len(v1)):
k_map = v1.index(v2[k])
uv1[k].xy = - (uv2[k_map].x - 0.5) + 0.5, uv2[k_map].y
2010-01-31 14:46:28 +00:00
# has_active_UV_layer, double_warn
return True, double_warn
@classmethod
def poll(cls, context):
obj = context.view_layer.objects.active
return (obj and obj.type == 'MESH')
def execute(self, context):
DIR = (self.direction == 'NEGATIVE')
total_no_active_UV = 0
total_duplicates = 0
meshes_with_duplicates = 0
ob = context.view_layer.objects.active
is_editmode = (ob.mode == 'EDIT')
if is_editmode:
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
meshes = [
ob.data for ob in context.view_layer.objects.selected
if ob.type == 'MESH' and ob.data.is_editable
]
for mesh in meshes:
mesh.tag = False
for mesh in meshes:
if mesh.tag:
continue
mesh.tag = True
has_active_UV_layer, double_warn = self.do_mesh_mirror_UV(mesh, DIR)
if not has_active_UV_layer:
total_no_active_UV = total_no_active_UV + 1
elif double_warn:
total_duplicates += double_warn
meshes_with_duplicates = meshes_with_duplicates + 1
if is_editmode:
bpy.ops.object.mode_set(mode='EDIT', toggle=False)
if total_duplicates and total_no_active_UV:
self.report(
{'WARNING'},
rpt_(
"{:d} mesh(es) with no active UV layer, "
"{:d} duplicates found in {:d} mesh(es), mirror may be incomplete"
).format(total_no_active_UV, total_duplicates, meshes_with_duplicates),
)
elif total_no_active_UV:
self.report(
{'WARNING'},
rpt_("{:d} mesh(es) with no active UV layer").format(total_no_active_UV),
)
elif total_duplicates:
self.report(
{'WARNING'},
rpt_(
"{:d} duplicates found in {:d} mesh(es), mirror may be incomplete"
).format(total_duplicates, meshes_with_duplicates),
)
return {'FINISHED'}
class MeshSelectNext(Operator):
"""Select the next element (using selection order)"""
bl_idname = "mesh.select_next_item"
bl_label = "Select Next Element"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return (context.mode == 'EDIT_MESH')
def execute(self, context):
import bmesh
from .bmesh import find_adjacent
obj = context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if find_adjacent.select_next(bm, self.report):
bm.select_flush_mode()
PyAPI: use keyword only arguments Use keyword only arguments for the following functions. - addon_utils.module_bl_info 2nd arg `info_basis`. - addon_utils.modules 1st `module_cache`, 2nd arg `refresh`. - addon_utils.modules_refresh 1st arg `module_cache`. - bl_app_template_utils.activate 1nd arg `template_id`. - bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`. - bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`. - bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`. - bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`. - bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`. - bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`. - bmesh.types.BMesh.calc_volume 1st arg `signed`. - bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`. - bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`. - bmesh.types.BMesh.transform 2nd arg `filter`. - bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`. - bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`. - bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`. - bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`. - bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`. - bpy.msgbus.subscribe_rna 5th arg `options`. - bpy.path.abspath 2nd & 3rd args `start` & `library`. - bpy.path.clean_name 2nd arg `replace`. - bpy.path.ensure_ext 3rd arg `case_sensitive`. - bpy.path.module_names 2nd arg `recursive`. - bpy.path.relpath 2nd arg `start`. - bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`. - bpy.types.Operator.as_keywords 1st arg `ignore`. - bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`. - bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`. - bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`. - bpy.utils.app_template_paths 1st arg `subdir`. - bpy.utils.app_template_paths 1st arg `subdir`. - bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`. - bpy.utils.execfile 2nd arg `mod`. - bpy.utils.keyconfig_set 2nd arg `report`. - bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`. - bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`. - bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`. - bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`. - bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.system_resource 2nd arg `subdir`. - bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`. - bpy.utils.units.to_value 4th arg `str_ref_unit`. - bpy.utils.user_resource 2nd & 3rd args `subdir`, `create` - bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`. - bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`. - gpu.offscreen.unbind 1st arg `restore`. - gpu_extras.batch.batch_for_shader 4th arg `indices`. - gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`. - gpu_extras.presets.draw_circle_2d 4th arg `segments`. - imbuf.types.ImBuf.resize 2nd arg `resize`. - imbuf.write 2nd arg `filepath`. - mathutils.kdtree.KDTree.find 2nd arg `filter`. - nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`. - nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`. - nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`. - rna_prop_ui.draw 5th arg `use_edit`. - rna_prop_ui.rna_idprop_ui_get 2nd arg `create`. - rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`. - rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`. - rna_xml.xml2rna 2nd arg `root_rna`. - rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
bmesh.update_edit_mesh(me, loop_triangles=False)
return {'FINISHED'}
class MeshSelectPrev(Operator):
2017-10-19 11:09:27 +11:00
"""Select the previous element (using selection order)"""
bl_idname = "mesh.select_prev_item"
bl_label = "Select Previous Element"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return (context.mode == 'EDIT_MESH')
def execute(self, context):
import bmesh
from .bmesh import find_adjacent
obj = context.active_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
if find_adjacent.select_prev(bm, self.report):
bm.select_flush_mode()
PyAPI: use keyword only arguments Use keyword only arguments for the following functions. - addon_utils.module_bl_info 2nd arg `info_basis`. - addon_utils.modules 1st `module_cache`, 2nd arg `refresh`. - addon_utils.modules_refresh 1st arg `module_cache`. - bl_app_template_utils.activate 1nd arg `template_id`. - bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`. - bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`. - bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`. - bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`. - bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`. - bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`. - bmesh.types.BMesh.calc_volume 1st arg `signed`. - bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`. - bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`. - bmesh.types.BMesh.transform 2nd arg `filter`. - bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`. - bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`. - bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`. - bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`. - bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`. - bpy.msgbus.subscribe_rna 5th arg `options`. - bpy.path.abspath 2nd & 3rd args `start` & `library`. - bpy.path.clean_name 2nd arg `replace`. - bpy.path.ensure_ext 3rd arg `case_sensitive`. - bpy.path.module_names 2nd arg `recursive`. - bpy.path.relpath 2nd arg `start`. - bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`. - bpy.types.Operator.as_keywords 1st arg `ignore`. - bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`. - bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`. - bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`. - bpy.utils.app_template_paths 1st arg `subdir`. - bpy.utils.app_template_paths 1st arg `subdir`. - bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`. - bpy.utils.execfile 2nd arg `mod`. - bpy.utils.keyconfig_set 2nd arg `report`. - bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`. - bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`. - bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`. - bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`. - bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.system_resource 2nd arg `subdir`. - bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`. - bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`. - bpy.utils.units.to_value 4th arg `str_ref_unit`. - bpy.utils.user_resource 2nd & 3rd args `subdir`, `create` - bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`. - bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`. - gpu.offscreen.unbind 1st arg `restore`. - gpu_extras.batch.batch_for_shader 4th arg `indices`. - gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`. - gpu_extras.presets.draw_circle_2d 4th arg `segments`. - imbuf.types.ImBuf.resize 2nd arg `resize`. - imbuf.write 2nd arg `filepath`. - mathutils.kdtree.KDTree.find 2nd arg `filter`. - nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`. - nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`. - nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`. - rna_prop_ui.draw 5th arg `use_edit`. - rna_prop_ui.rna_idprop_ui_get 2nd arg `create`. - rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`. - rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`. - rna_xml.xml2rna 2nd arg `root_rna`. - rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 18:03:14 +10:00
bmesh.update_edit_mesh(me, loop_triangles=False)
return {'FINISHED'}
classes = (
MeshMirrorUV,
MeshSelectNext,
MeshSelectPrev,
)