Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Automerge Slide modal operator by Testure
- # Original proposal here: https://blender.community/c/rightclickselect/Nhdbbc/
- # Purpose: Replaces "GG" vertex slide functionality. Enables automerge for the duration of the operator and resets the state when finished.
- # TODO: - expose bool property for automerge so you can disable it/enable it.
- # - restore property exposure for bpy.ops.transform.vert_slide so they show up in the operator panel and aren't hidden by this operator.
- import bpy
- from bpy.types import Operator
- class MESH_OT_automerge_slide(Operator):
- bl_idname = 'mesh.automerge_slide'
- bl_label = 'Automerge Slide'
- bl_options = {'REGISTER', 'UNDO'}
- def __init__(self):
- self._was_enabled = False
- def modal(self, context, event):
- if event.type == 'MOUSEMOVE' and event.value == 'PRESS':
- bpy.ops.transform.vert_slide(get_view3d_context_override(), 'INVOKE_DEFAULT', correct_uv=True)
- return {'RUNNING_MODAL'}
- if event.type in {'RIGHTMOUSE', 'ESC'}: # Cancel
- if self._was_enabled == False:
- bpy.context.scene.tool_settings.use_mesh_automerge = False
- return {'CANCELLED'}
- elif event.type == 'MOUSEMOVE' and event.value == 'RELEASE':
- if self._was_enabled == False:
- bpy.context.scene.tool_settings.use_mesh_automerge = False
- return {'FINISHED'}
- return {'RUNNING_MODAL'}
- def invoke(self, context, event):
- if bpy.context.scene.tool_settings.use_mesh_automerge == True:
- self._was_enabled = True # we were already in xray mode, don't turn it off when we're done with the modal op.
- bpy.context.scene.tool_settings.use_mesh_automerge = True
- context.window_manager.modal_handler_add(self)
- return {'RUNNING_MODAL'}
- def register():
- bpy.utils.register_class(MESH_OT_automerge_slide)
- def unregister():
- bpy.utils.unregister_class(MESH_OT_automerge_slide)
- def get_view3d_context_override():
- ''' Iterates through the blender GUI's windows, screens, areas, regions to find the View3D space and its associated window. Populate an 'oContextOverride context' that can be used with bpy.ops that require to be used from within a View3D (like most addon code that runs of View3D panels) '''
- # Original Author: dvochin (https://www.blender.org/forum/viewtopic.php?t=27834)
- # NOTE: If your operator fails the log will show an "PyContext: 'xyz' not found". To fix stuff 'xyz' into the override context and try again!
- for oWindow in bpy.context.window_manager.windows: #TODO: Find way to avoid doing four levels of traversals at every request!!
- oScreen = oWindow.screen
- for oArea in oScreen.areas:
- if oArea.type == 'VIEW_3D': # Frequently, bpy.ops operators are called from View3d's toolbox or property panel. By finding that window/screen/area we can fool operators in thinking they were called from the View3D!
- for oRegion in oArea.regions:
- if oRegion.type == 'WINDOW': # View3D has several 'windows' like 'HEADER' and 'WINDOW'. Most bpy.ops require 'WINDOW'
- # Now that we've found the View3D area all of it goes into a dictionary to pass into the bpy.ops override.
- # I stuffed extra info in there like selected objects, active objects, etc as most operators require them.
- # (If anything is missing operator will fail and log a 'PyContext: error on the log with what is missing in
- # context override)
- oContextOverride = {
- 'window': oWindow, 'screen': oScreen, 'area': oArea, 'region': oRegion,
- 'scene': bpy.context.scene, 'edit_object': bpy.context.edit_object,
- 'active_object': bpy.context.active_object, 'selected_objects': bpy.context.selected_objects
- } # Stuff the override context with very common requests by operators. MORE COULD BE NEEDED!
- #debug.log("-AssembleOverrideContextForView3dOps() created override context: ", oContextOverride)
- return oContextOverride
- raise Exception("ERROR: AssembleOverrideContextForView3dOps() could not find a VIEW_3D with WINDOW region to create override context to enable View3D operators. Operator cannot function.")
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment