diff options
Diffstat (limited to 'magic_uv/op/move_uv.py')
-rw-r--r-- | magic_uv/op/move_uv.py | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/magic_uv/op/move_uv.py b/magic_uv/op/move_uv.py index 881ab378..19160a46 100644 --- a/magic_uv/op/move_uv.py +++ b/magic_uv/op/move_uv.py @@ -20,8 +20,8 @@ __author__ = "kgeogeo, mem, Nutti <nutti.metro@gmail.com>" __status__ = "production" -__version__ = "6.2" -__date__ = "31 Jul 2019" +__version__ = "6.3" +__date__ = "10 Aug 2020" import bpy from bpy.props import BoolProperty @@ -54,20 +54,6 @@ def _is_valid_context(context): return True -def _find_uv(context): - bm = bmesh.from_edit_mesh(context.object.data) - topology_dict = [] - uvs = [] - active_uv = bm.loops.layers.uv.active - for fidx, f in enumerate(bm.faces): - for vidx, v in enumerate(f.verts): - if v.select: - uvs.append(f.loops[vidx][active_uv].uv.copy()) - topology_dict.append([fidx, vidx]) - - return topology_dict, uvs - - @PropertyClassRegistry() class _Properties: idname = "move_uv" @@ -106,6 +92,9 @@ class MUV_OT_MoveUV(bpy.types.Operator): self.__ini_uvs = [] self.__operating = False + # Creation of BMesh is high cost, so cache related objects. + self.__cache = {} + @classmethod def poll(cls, context): # we can not get area/space/region from console @@ -119,7 +108,18 @@ class MUV_OT_MoveUV(bpy.types.Operator): def is_running(cls, _): return cls.__running - def modal(self, context, event): + def _find_uv(self, bm, active_uv): + topology_dict = [] + uvs = [] + for fidx, f in enumerate(bm.faces): + for vidx, v in enumerate(f.verts): + if v.select: + uvs.append(f.loops[vidx][active_uv].uv.copy()) + topology_dict.append([fidx, vidx]) + + return topology_dict, uvs + + def modal(self, _, event): if self.__first_time is True: self.__prev_mouse = Vector(( event.mouse_region_x, event.mouse_region_y)) @@ -146,12 +146,11 @@ class MUV_OT_MoveUV(bpy.types.Operator): return {'RUNNING_MODAL'} # update UV - obj = context.object - bm = bmesh.from_edit_mesh(obj.data) - active_uv = bm.loops.layers.uv.active - for fidx, vidx in self.__topology_dict: - l = bm.faces[fidx].loops[vidx] - l[active_uv].uv = l[active_uv].uv + dv + obj = self.__cache["active_object"] + bm = self.__cache["bmesh"] + active_uv = self.__cache["active_uv"] + for uv in self.__cache["target_uv"]: + uv += dv bmesh.update_edit_mesh(obj.data) # check mouse preference @@ -163,10 +162,12 @@ class MUV_OT_MoveUV(bpy.types.Operator): for (fidx, vidx), uv in zip(self.__topology_dict, self.__ini_uvs): bm.faces[fidx].loops[vidx][active_uv].uv = uv MUV_OT_MoveUV.__running = False + self.__cache = {} return {'FINISHED'} # confirmed if event.type == confirm_btn and event.value == 'PRESS': MUV_OT_MoveUV.__running = False + self.__cache = {} return {'FINISHED'} return {'RUNNING_MODAL'} @@ -177,7 +178,21 @@ class MUV_OT_MoveUV(bpy.types.Operator): self.__first_time = True context.window_manager.modal_handler_add(self) - self.__topology_dict, self.__ini_uvs = _find_uv(context) + + obj = context.active_object + bm = bmesh.from_edit_mesh(obj.data) + active_uv = bm.loops.layers.uv.active + self.__topology_dict, self.__ini_uvs = self._find_uv(bm, active_uv) + + # Optimization: Store temporary variables which cause heavy + # calculation. + self.__cache["active_object"] = obj + self.__cache["bmesh"] = bm + self.__cache["active_uv"] = active_uv + self.__cache["target_uv"] = [] + for fidx, vidx in self.__topology_dict: + l = bm.faces[fidx].loops[vidx] + self.__cache["target_uv"].append(l[active_uv].uv) if context.area: context.area.tag_redraw() |