Guest User

Spread Loose Parts

a guest
Sep 2nd, 2021
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # BLENDER 2.9x+ addon to evenly spread out loose parts of a single object in edit mode.
  2. # written by Testure in response to a RCS thread: https://blender.community/c/rightclickselect/XQYk/
  3. # INSTALLATION: drop this into your Blender/scripts/startup folder and restart Blender. Operator will be available in edit mode only.
  4.  
  5. import bpy
  6.  
  7. class MESH_OT_spread_loose(bpy.types.Operator):
  8.     """Evenly spread out loose parts of a single object in edit mode"""
  9.     bl_idname = "mesh.spread_loose"
  10.     bl_label = "Spread Out Loose Parts"
  11.  
  12.     def __init__(self):
  13.         self.original_origin = None
  14.  
  15.     def modal(self, context, event):
  16.         # operator's modal is pass through, just listening for FINISH event conditions
  17.         if event.type in { 'LEFTMOUSE', 'RIGHTMOUSE', 'ESC'} and event.value == "RELEASE":
  18.             self.finish(context)
  19.             return {'FINISHED'}
  20.         return {'PASS_THROUGH'}
  21.  
  22.     @classmethod
  23.     def poll(cls, context):
  24.         return context.mode == 'EDIT_MESH' and len(context.selected_objects) == 1
  25.    
  26.     def finish(self, context):
  27.         bpy.ops.object.join()
  28.         cursor_matrix = context.scene.cursor.matrix.copy()
  29.         context.scene.cursor.matrix = self.original_origin        
  30.         bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
  31.         context.scene.cursor.matrix = cursor_matrix        
  32.         bpy.context.scene.tool_settings.use_transform_pivot_point_align = False
  33.         bpy.ops.object.editmode_toggle()
  34.        
  35.     def invoke(self, context, event):
  36.         self.original_origin = context.active_object.matrix_world.copy()
  37.         bpy.ops.mesh.separate(type='LOOSE')
  38.         bpy.ops.object.editmode_toggle()
  39.         bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='MEDIAN')
  40.         bpy.context.scene.tool_settings.use_transform_pivot_point_align = True
  41.        
  42.         bpy.ops.transform.resize('INVOKE_DEFAULT', constraint_axis=(False, False, False))
  43.         context.window_manager.modal_handler_add(self)
  44.         return {'RUNNING_MODAL'}
  45.  
  46. def register():
  47.     bpy.utils.register_class(MESH_OT_spread_loose)
  48.  
  49.  
  50. def unregister():
  51.     bpy.utils.unregister_class(MESH_OT_spread_loose)
  52.  
  53.  
  54. if __name__ == "__main__":
  55.     register()
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment