Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bl_info = {
- 'name': 'Boolean Collections',
- 'category': 'Object',
- 'author': 'Ispheria',
- 'version': (0, 0, 1),
- 'blender': (2, 80, 0)
- }
- import bpy
- class AddBoolean(bpy.types.Operator):
- bl_idname = 'object.add_boolean'
- bl_label = 'Add Boolean'
- bl_options = {'UNDO'}
- op: bpy.props.StringProperty(name='Boolean Operation', default='DIFFERENCE')
- def execute(self, context):
- C = context
- A = C.object
- S = C.selected_objects
- D = bpy.data
- O = bpy.ops
- if S.__len__() > 1:
- if A.name in D.collections.keys():
- col = D.collections[A.name]
- else:
- col = D.collections.new(A.name)
- C.scene.collection.children.link(col)
- for obj in C.selected_objects:
- if obj.name not in col.objects: col.objects.link(obj)
- if obj is not A:
- O.object.modifier_add(type='BOOLEAN')
- boolean = A.modifiers[-1]
- boolean.name = obj.name
- boolean.operation = self.op
- boolean.object = obj
- obj.display_type = "WIRE"
- obj.hide_render = True
- O = A
- A.select_set(False)
- for col in D.collections:
- for obj in S:
- if obj.name in col.all_objects and col.name not in col.objects:
- bpy.ops.collection.objects_remove(collection=col.name)
- O.select_set(True)
- return {'FINISHED'}
- else: return {'CANCELLED'}
- class ToggleCollectionVisibility(bpy.types.Operator):
- bl_idname = 'object.toggle_collection_visibility'
- bl_label = 'Toggle Collection Visibility'
- def execute(self, context):
- A = context.object
- D = bpy.data
- if A.name in D.collections.keys():
- D.collections[A.name].hide_viewport = not D.collections[A.name].hide_viewport
- return {'FINISHED'}
- else: return {'CANCELLED'}
- class SelectAllBooleanModifiers(bpy.types.Operator):
- bl_idname = 'object.select_all_boolean_modifiers'
- bl_label = 'Select All Boolean Modifiers'
- def execute(self, context):
- A = context.object
- D = bpy.data
- if A.name in D.collections.keys():
- D.collections[A.name].hide_viewport = False
- for obj in D.collections[A.name].all_objects:
- obj.select_set(True)
- return {'FINISHED'}
- else: return {'CANCELLED'}
- class ApplyAllBooleans(bpy.types.Operator):
- bl_idname = 'object.apply_all_booleans'
- bl_label = 'Apply All Booleans'
- bl_options = {'UNDO'}
- def execute(self, context):
- A = context.object
- D = bpy.data
- meshes = []
- for i in range(A.modifiers.__len__()):
- if A.modifiers[0].type == 'BOOLEAN':
- meshes.append(A.modifiers[0].object)
- bpy.ops.object.modifier_apply(apply_as='DATA', modifier=A.modifiers[0].name)
- for col in D.collections:
- if col.name != A.name:
- for i in range(meshes.__len__() - 1, -1, -1):
- if meshes[i].name in col.all_objects:
- meshes.remove(meshes[i])
- O = A
- bpy.ops.object.select_all(action='DESELECT')
- for obj in meshes:
- obj.select_set(True)
- bpy.ops.object.delete(use_global=False, confirm=False)
- O.select_set(True)
- context.scene.collection.children.unlink(D.collections[A.name])
- D.collections.remove(D.collections[A.name])
- return {'FINISHED'}
- from bpy.types import Menu
- class VIEW3D_PIE_BooleanCollections(Menu):
- bl_idname = 'object.boolean_collections'
- bl_label = 'Boolean Collections'
- def draw(self, context):
- pie = self.layout.menu_pie()
- pie.operator('object.add_boolean', text='Union').op = 'UNION'
- pie.operator('object.add_boolean', text='Intersect').op = 'INTERSECT'
- pie.operator('object.add_boolean', text='Difference').op = 'DIFFERENCE'
- pie.operator('object.toggle_collection_visibility')
- pie.operator('object.select_all_boolean_modifiers')
- pie.operator('object.apply_all_booleans')
- def register():
- bpy.utils.register_class(AddBoolean)
- bpy.utils.register_class(ToggleCollectionVisibility)
- bpy.utils.register_class(SelectAllBooleanModifiers)
- bpy.utils.register_class(ApplyAllBooleans)
- bpy.utils.register_class(VIEW3D_PIE_BooleanCollections)
- def unregister():
- bpy.utils.unregister_class(AddBoolean)
- bpy.utils.unregister_class(ToggleCollectionVisibility)
- bpy.utils.unregister_class(SelectAllBooleanModifiers)
- bpy.utils.unregister_class(ApplyAllBooleans)
- bpy.utils.unregister_class(VIEW3D_PIE_BooleanCollections)
- if __name__ == '__main__':
- register()
- bpy.ops.wm.call_menu_pie(name='object.boolean_collections')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement