Advertisement
Guest User

Blender 2.8 Boolean Collections Plugin by Ispheria

a guest
Nov 28th, 2018
3,556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. bl_info = {
  2.     'name': 'Boolean Collections',
  3.     'category': 'Object',
  4.     'author': 'Ispheria',
  5.     'version': (0, 0, 1),
  6.     'blender': (2, 80, 0)
  7. }
  8.  
  9. import bpy
  10.  
  11. class AddBoolean(bpy.types.Operator):
  12.     bl_idname = 'object.add_boolean'
  13.     bl_label = 'Add Boolean'
  14.     bl_options = {'UNDO'}
  15.    
  16.     op: bpy.props.StringProperty(name='Boolean Operation', default='DIFFERENCE')
  17.    
  18.     def execute(self, context):
  19.        
  20.         C = context
  21.         A = C.object
  22.         S = C.selected_objects
  23.         D = bpy.data
  24.         O = bpy.ops
  25.        
  26.         if S.__len__() > 1:
  27.  
  28.             if A.name in D.collections.keys():
  29.                 col = D.collections[A.name]
  30.  
  31.             else:
  32.                 col = D.collections.new(A.name)
  33.                 C.scene.collection.children.link(col)
  34.                
  35.             for obj in C.selected_objects:
  36.                 if obj.name not in col.objects: col.objects.link(obj)
  37.                 if obj is not A:
  38.                     O.object.modifier_add(type='BOOLEAN')
  39.                     boolean = A.modifiers[-1]
  40.                     boolean.name = obj.name
  41.                     boolean.operation = self.op
  42.                     boolean.object = obj
  43.                     obj.display_type = "WIRE"
  44.                     obj.hide_render = True
  45.            
  46.             O = A
  47.             A.select_set(False)
  48.             for col in D.collections:
  49.                 for obj in S:
  50.                     if obj.name in col.all_objects and col.name not in col.objects:
  51.                         bpy.ops.collection.objects_remove(collection=col.name)
  52.            
  53.             O.select_set(True)
  54.            
  55.             return {'FINISHED'}
  56.        
  57.         else: return {'CANCELLED'}
  58.  
  59.  
  60.  
  61. class ToggleCollectionVisibility(bpy.types.Operator):
  62.     bl_idname = 'object.toggle_collection_visibility'
  63.     bl_label = 'Toggle Collection Visibility'
  64.    
  65.     def execute(self, context):
  66.        
  67.         A = context.object
  68.         D = bpy.data
  69.        
  70.         if A.name in D.collections.keys():
  71.             D.collections[A.name].hide_viewport = not D.collections[A.name].hide_viewport
  72.            
  73.             return {'FINISHED'}
  74.        
  75.         else: return {'CANCELLED'}
  76.    
  77.  
  78.  
  79. class SelectAllBooleanModifiers(bpy.types.Operator):
  80.     bl_idname = 'object.select_all_boolean_modifiers'
  81.     bl_label = 'Select All Boolean Modifiers'
  82.    
  83.     def execute(self, context):
  84.        
  85.         A = context.object
  86.         D = bpy.data
  87.  
  88.         if A.name in D.collections.keys():
  89.             D.collections[A.name].hide_viewport = False
  90.             for obj in D.collections[A.name].all_objects:
  91.                 obj.select_set(True)
  92.             return {'FINISHED'}
  93.        
  94.         else: return {'CANCELLED'}
  95.  
  96.  
  97.  
  98. class ApplyAllBooleans(bpy.types.Operator):
  99.     bl_idname = 'object.apply_all_booleans'
  100.     bl_label = 'Apply All Booleans'
  101.     bl_options = {'UNDO'}
  102.    
  103.     def execute(self, context):
  104.        
  105.         A = context.object
  106.         D = bpy.data
  107.        
  108.         meshes = []
  109.        
  110.         for i in range(A.modifiers.__len__()):
  111.             if A.modifiers[0].type == 'BOOLEAN':
  112.                 meshes.append(A.modifiers[0].object)
  113.                 bpy.ops.object.modifier_apply(apply_as='DATA', modifier=A.modifiers[0].name)
  114.                
  115.         for col in D.collections:
  116.             if col.name != A.name:
  117.                 for i in range(meshes.__len__() - 1, -1, -1):
  118.                     if meshes[i].name in col.all_objects:
  119.                         meshes.remove(meshes[i])
  120.                        
  121.         O = A
  122.         bpy.ops.object.select_all(action='DESELECT')
  123.         for obj in meshes:
  124.             obj.select_set(True)
  125.            
  126.         bpy.ops.object.delete(use_global=False, confirm=False)
  127.         O.select_set(True)
  128.         context.scene.collection.children.unlink(D.collections[A.name])
  129.         D.collections.remove(D.collections[A.name])
  130.        
  131.         return {'FINISHED'}
  132.  
  133.  
  134.  
  135. from bpy.types import Menu
  136.  
  137. class VIEW3D_PIE_BooleanCollections(Menu):
  138.     bl_idname = 'object.boolean_collections'
  139.     bl_label = 'Boolean Collections'
  140.    
  141.    
  142.     def draw(self, context):
  143.         pie = self.layout.menu_pie()
  144.        
  145.         pie.operator('object.add_boolean', text='Union').op = 'UNION'
  146.         pie.operator('object.add_boolean', text='Intersect').op = 'INTERSECT'
  147.         pie.operator('object.add_boolean', text='Difference').op = 'DIFFERENCE'
  148.         pie.operator('object.toggle_collection_visibility')
  149.         pie.operator('object.select_all_boolean_modifiers')
  150.         pie.operator('object.apply_all_booleans')
  151.  
  152.  
  153.  
  154. def register():
  155.     bpy.utils.register_class(AddBoolean)
  156.     bpy.utils.register_class(ToggleCollectionVisibility)
  157.     bpy.utils.register_class(SelectAllBooleanModifiers)
  158.     bpy.utils.register_class(ApplyAllBooleans)
  159.     bpy.utils.register_class(VIEW3D_PIE_BooleanCollections)
  160.        
  161. def unregister():
  162.     bpy.utils.unregister_class(AddBoolean)
  163.     bpy.utils.unregister_class(ToggleCollectionVisibility)
  164.     bpy.utils.unregister_class(SelectAllBooleanModifiers)                  
  165.     bpy.utils.unregister_class(ApplyAllBooleans)
  166.     bpy.utils.unregister_class(VIEW3D_PIE_BooleanCollections)
  167.        
  168. if __name__ == '__main__':
  169.     register()
  170.     bpy.ops.wm.call_menu_pie(name='object.boolean_collections')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement