diff options
Diffstat (limited to 'uv_magic_uv/op/unwrap_constraint.py')
-rw-r--r-- | uv_magic_uv/op/unwrap_constraint.py | 87 |
1 files changed, 74 insertions, 13 deletions
diff --git a/uv_magic_uv/op/unwrap_constraint.py b/uv_magic_uv/op/unwrap_constraint.py index df16f783..89727160 100644 --- a/uv_magic_uv/op/unwrap_constraint.py +++ b/uv_magic_uv/op/unwrap_constraint.py @@ -27,10 +27,33 @@ from bpy.props import ( EnumProperty, FloatProperty, ) +import bmesh +from .. import common from ..utils.bl_class_registry import BlClassRegistry from ..utils.property_class_registry import PropertyClassRegistry -from ..impl import unwrap_constraint_impl as impl +from ..utils import compatibility as compat + + +def _is_valid_context(context): + obj = context.object + + # only edit mode is allowed to execute + if obj is None: + return False + if obj.type != 'MESH': + return False + if context.object.mode != 'EDIT': + return False + + # only 'VIEW_3D' space is allowed to execute + for space in context.area.spaces: + if space.type == 'VIEW_3D': + break + else: + return False + + return True @PropertyClassRegistry() @@ -63,6 +86,7 @@ class _Properties: @BlClassRegistry(legacy=True) +@compat.make_annotations class MUV_OT_UnwrapConstraint(bpy.types.Operator): """ Operation class: Unwrap with constrain UV coordinate @@ -74,7 +98,7 @@ class MUV_OT_UnwrapConstraint(bpy.types.Operator): bl_options = {'REGISTER', 'UNDO'} # property for original unwrap - method: EnumProperty( + method = EnumProperty( name="Method", description="Unwrapping method", items=[ @@ -82,20 +106,20 @@ class MUV_OT_UnwrapConstraint(bpy.types.Operator): ('CONFORMAL', 'Conformal', 'Conformal') ], default='ANGLE_BASED') - fill_holes: BoolProperty( + fill_holes = BoolProperty( name="Fill Holes", description="Virtual fill holes in meshes before unwrapping", default=True) - correct_aspect: BoolProperty( + correct_aspect = BoolProperty( name="Correct Aspect", description="Map UVs taking image aspect ratio into account", default=True) - use_subsurf_data: BoolProperty( + use_subsurf_data = BoolProperty( name="Use Subsurf Modifier", description="""Map UVs taking vertex position after subsurf into account""", default=False) - margin: FloatProperty( + margin = FloatProperty( name="Margin", description="Space between islands", max=1.0, @@ -103,23 +127,60 @@ class MUV_OT_UnwrapConstraint(bpy.types.Operator): default=0.001) # property for this operation - u_const: BoolProperty( + u_const = BoolProperty( name="U-Constraint", description="Keep UV U-axis coordinate", default=False ) - v_const: BoolProperty( + v_const = BoolProperty( name="V-Constraint", description="Keep UV V-axis coordinate", default=False ) - def __init__(self): - self.__impl = impl.UnwrapConstraintImpl() - @classmethod def poll(cls, context): - return impl.UnwrapConstraintImpl.poll(context) + # we can not get area/space/region from console + if common.is_console_mode(): + return True + return _is_valid_context(context) def execute(self, context): - return self.__impl.execute(self, context) + obj = context.active_object + bm = bmesh.from_edit_mesh(obj.data) + if common.check_version(2, 73, 0) >= 0: + bm.faces.ensure_lookup_table() + + # bpy.ops.uv.unwrap() makes one UV map at least + if not bm.loops.layers.uv: + self.report({'WARNING'}, "Object must have more than one UV map") + return {'CANCELLED'} + uv_layer = bm.loops.layers.uv.verify() + + # get original UV coordinate + faces = [f for f in bm.faces if f.select] + uv_list = [] + for f in faces: + uvs = [l[uv_layer].uv.copy() for l in f.loops] + uv_list.append(uvs) + + # unwrap + bpy.ops.uv.unwrap( + method=self.method, + fill_holes=self.fill_holes, + correct_aspect=self.correct_aspect, + use_subsurf_data=self.use_subsurf_data, + margin=self.margin) + + # when U/V-Constraint is checked, revert original coordinate + for f, uvs in zip(faces, uv_list): + for l, uv in zip(f.loops, uvs): + if self.u_const: + l[uv_layer].uv.x = uv.x + if self.v_const: + l[uv_layer].uv.y = uv.y + + # update mesh + bmesh.update_edit_mesh(obj.data) + + return {'FINISHED'} |