Advertisement
Guest User

Edit Origin tool

a guest
Jul 28th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. import bpy
  2.  
  3. # In this example operator, use_snap is being turned on. If you run the operator, commit the modal then
  4. # undo and then redo toolsettings.use_snap will be turned back on
  5.  
  6. # WARNING: This operator only works if there is an active object selected and you are in Object Mode
  7.  
  8. class EDITORIGINTEST_OT_edit_origin(bpy.types.Operator):
  9.     bl_idname = "edit_origin_test.edit_origin"
  10.     bl_label = "Edit Origin"
  11.     bl_options = {'REGISTER'}
  12.  
  13.     def modal(self, context, event):
  14.         if self.finished:
  15.             # Disable use_snap. It is REQUIRED to do it after return {'PASS_THROUGH'} or else it will not
  16.             # disable due to the transform.translate being active, hence my theory of it being automatically
  17.             # optimized with transform.translate's built in snapping option
  18.             context.scene.tool_settings.use_snap = False
  19.             return {'CANCELLED'}
  20.  
  21.         if event.type in {'LEFTMOUSE', 'RET', 'RIGHTMOUSE', 'ESC'}:
  22.             context.scene.tool_settings.use_transform_data_origin = False
  23.            
  24.             # Initiate modal end
  25.             self.finished = True
  26.             return {'PASS_THROUGH'}
  27.         return {'PASS_THROUGH'}
  28.        
  29.     def invoke(self, context, event):
  30.         # Modal end detection
  31.         self.finished = False
  32.        
  33.         # Turn on use transform data origin
  34.         context.scene.tool_settings.use_transform_data_origin = True
  35.        
  36.         # Enable use_snap
  37.         context.scene.tool_settings.use_snap = True
  38.  
  39.         # Translate modal
  40.         bpy.ops.transform.translate('INVOKE_DEFAULT')
  41.        
  42.         # Modal handler
  43.         wm = context.window_manager
  44.         wm.modal_handler_add(self)
  45.         return {'RUNNING_MODAL'}
  46.    
  47. # The reason I consider this broken is because if you use 'context.scene.tool_settings.use_snap'
  48. # without the translate modal and without registering the undo within bl_options of the operator
  49. # it will not register the undo and thus undoing and redoing will not result in use_snap being turned back on
  50.  
  51.  
  52. # This is a bit of a redundant example since I think text explains it well enough, but in this
  53. # example if you were to undo and redo use_snap would not be turned on after undoing and redoing
  54.  
  55. class EDITORIGINTEST_OT_toggle_use_snap(bpy.types.Operator):
  56.     bl_idname = "edit_origin_test.toggle_snap"
  57.     bl_label = "Snap Toggle"
  58.     bl_options = {'REGISTER'}
  59.    
  60.     def execute(self, context):
  61.         context.scene.tool_settings.use_snap = True
  62.        
  63.         context.scene.tool_settings.use_snap = False
  64.         return {'FINISHED'}
  65.  
  66.  
  67. # Register the operators
  68.  
  69. bpy.utils.register_class(EDITORIGINTEST_OT_edit_origin)
  70. bpy.utils.register_class(EDITORIGINTEST_OT_toggle_use_snap)
  71.  
  72.  
  73. # I understand that this is a niche scenario, and if it would be too difficult to implement a fix I would
  74. # totally understand. I also made this report in hopes that maybe I missed something in my hours of testing
  75. # that would make this a non-issue entirely. Any off the books suggestions would be appreciated.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement