Newer
Older
Jonathan Williamson
committed
bl_info = {
"name": "Boolean Operators",
"location": "View3D > Toolbar",
"description": "Add Boolean Tools for running boolean operations on two selected objects.",
Jonathan Williamson
committed
"author": "Jonathan Williamson",
Jonathan Williamson
committed
"blender": (2, 6, 6),
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/3D_interaction/booleanoperators",
"tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=34502&group_id=153&atid=467",
Jonathan Williamson
committed
"category": "3D View",
}
import bpy
###------ Create Boolean Operators -------###
class boolean(bpy.types.Operator):
"""Boolean the currently selected objects"""
bl_idname = "mesh.boolean"
bl_label = "Boolean Operator"
bl_options = {'REGISTER', 'UNDO'}
modOp = bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return len(context.selected_objects) > 0
Jonathan Williamson
committed
def execute(self, context):
scene = bpy.context.scene
Jonathan Williamson
committed
modName = "Bool"
activeObj = context.active_object
selected = context.selected_objects
if selected:
if len(selected) > 1:
if len(selected) == 2:
for ob in selected:
Jonathan Williamson
committed
nonActive = ob
bpy.ops.object.modifier_add(type="BOOLEAN")
for mod in activeObj.modifiers:
if mod.type == 'BOOLEAN':
mod.operation = self.modOp
mod.object = nonActive
mod.name = modName
Jonathan Williamson
committed
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modName)
Jonathan Williamson
committed
activeObj.select = False
bpy.ops.object.delete(use_global=False)
activeObj.select = True
Jonathan Williamson
committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
else:
self.report({'INFO'}, "Select only 2 objects at a time")
else:
self.report({'INFO'}, "Only 1 object selected")
else:
self.report({'INFO'}, "No objects selected")
return {"FINISHED"}
###------- Create the Boolean Menu --------###
class booleanMenu(bpy.types.Menu):
bl_label = "Boolean Tools"
bl_idname = "object.boolean_menu"
def draw(self, context):
layout = self.layout
union = layout.operator("mesh.boolean", "Union")
union.modOp = 'UNION'
intersect = layout.operator("mesh.boolean", "Intersect")
intersect.modOp = 'INTERSECT'
difference = layout.operator("mesh.boolean", "Difference")
difference.modOp = 'DIFFERENCE'
###------- Create the Boolean Toolbar --------###
class booleanToolbar(bpy.types.Panel):
bl_label = "Boolean Tools"
bl_idname = "object.boolean_toolbar"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = 'objectmode'
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.label(text="Operation:", icon="MOD_BOOLEAN")
row = col.row()
union = row.operator("mesh.boolean", "Union")
union.modOp = 'UNION'
intersect = row.operator("mesh.boolean", "Intersect")
intersect.modOp = 'INTERSECT'
difference = row.operator("mesh.boolean", "Difference")
difference.modOp = 'DIFFERENCE'
###------- Define the Hotkeys and Register Operators ---------###
addon_keymaps = []
def register():
bpy.utils.register_class(boolean)
bpy.utils.register_class(booleanMenu)
bpy.utils.register_class(booleanToolbar)
wm = bpy.context.window_manager
# create the boolean menu hotkey
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
Jonathan Williamson
committed
kmi = km.keymap_items.new('wm.call_menu', 'B', 'PRESS', ctrl=True, shift=True)
kmi.properties.name = 'object.boolean_menu'
addon_keymaps.append(km)
def unregister():
bpy.utils.unregister_class(boolean)
bpy.utils.unregister_class(booleanMenu)
bpy.utils.unregister_class(booleanToolbar)
# remove keymaps when add-on is deactivated
wm = bpy.context.window_manager
for km in addon_keymaps:
wm.keyconfigs.addon.keymaps.remove(km)
del addon_keymaps[:]
if __name__ == "__main__":
register()