1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
# 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_
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'}
direction: EnumProperty(
name="Axis Direction",
items=(
('POSITIVE', "Positive", ""),
('NEGATIVE', "Negative", ""),
),
)
precision: IntProperty(
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
if not mesh.uv_layers.active:
# has_active_UV_layer, double_warn
return False, 0
# 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
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
(uv.select for uv in uv_loops[lstart:lend]))
# 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
for i, j in pmap.items():
if not puvsel[i] or not puvsel[j]:
continue
if DIR == 0 and pcents[i][0] < 0.0:
continue
if DIR == 1 and pcents[i][0] > 0.0:
continue
# copy UVs
uv1 = puvs[i]
uv2 = puvs_cpy[j]
# get the correct rotation
v1 = vidxs[j]
v2 = tuple(vmap[k] for k in vidxs[i])
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
# 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()
bmesh.update_edit_mesh(me, loop_triangles=False)
return {'FINISHED'}
class MeshSelectPrev(Operator):
"""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()
bmesh.update_edit_mesh(me, loop_triangles=False)
return {'FINISHED'}
classes = (
MeshMirrorUV,
MeshSelectNext,
MeshSelectPrev,
)
|