Fix for broken hotkey
[blender-addons-contrib.git] / object_booleans.py
1 bl_info = {
2     "name": "Boolean Operators",
3     "location": "View3D > Toolbar",
4     "description": "Add Boolean Tools for running boolean operations on two selected objects.",
5     "author": "Jonathan Williamson",
6     "version": (0,4),
7     "blender": (2, 6, 6),
8     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/3D_interaction/booleanoperators",
9     "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=34502&group_id=153&atid=467",
10     "category": "3D View",
11     }
12
13 import bpy
14
15 ###------ Create Boolean Operators -------###
16    
17 class boolean(bpy.types.Operator):
18     """Boolean the currently selected objects"""
19     bl_idname = "mesh.boolean"
20     bl_label = "Boolean Operator"
21     bl_options = {'REGISTER', 'UNDO'}     
22             
23     modOp = bpy.props.StringProperty()
24     
25     @classmethod        
26     def poll(cls, context):
27         return len(context.selected_objects) > 0
28     
29     def execute(self, context):
30  
31         scene = bpy.context.scene
32          
33         modName = "Bool"
34     
35         activeObj = context.active_object
36         selected = context.selected_objects
37
38         if selected:
39             if len(selected) > 1:
40                 if len(selected) == 2:
41                     for ob in selected:
42                         if ob != activeObj:
43                             nonActive = ob
44     
45                     bpy.ops.object.modifier_add(type="BOOLEAN")   
46                     
47                     for mod in activeObj.modifiers:
48                         if mod.type == 'BOOLEAN':
49                             mod.operation = self.modOp
50                             mod.object = nonActive
51                             mod.name = modName
52                     
53                     bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modName)
54                     scene.objects.active = nonActive 
55                     activeObj.select = False
56                     bpy.ops.object.delete(use_global=False)
57                     activeObj.select = True
58                     scene.objects.active = activeObj
59                 else:
60                     self.report({'INFO'}, "Select only 2 objects at a time")
61             else:
62                 self.report({'INFO'}, "Only 1 object selected")
63         else:
64             self.report({'INFO'}, "No objects selected")
65                 
66         return {"FINISHED"}     
67
68
69 ###------- Create the Boolean Menu --------###          
70
71 class booleanMenu(bpy.types.Menu):
72     bl_label = "Boolean Tools"
73     bl_idname = "object.boolean_menu"
74        
75     def draw(self, context):
76         layout = self.layout
77         
78         union = layout.operator("mesh.boolean", "Union")
79         union.modOp = 'UNION'
80         
81         intersect = layout.operator("mesh.boolean", "Intersect")
82         intersect.modOp = 'INTERSECT'
83         
84         difference = layout.operator("mesh.boolean", "Difference")
85         difference.modOp = 'DIFFERENCE'
86
87
88 ###------- Create the Boolean Toolbar --------###          
89
90 class booleanToolbar(bpy.types.Panel):
91     bl_label = "Boolean Tools"
92     bl_idname = "object.boolean_toolbar"
93     bl_space_type = 'VIEW_3D'
94     bl_region_type = 'TOOLS'
95     bl_context = 'objectmode'
96     
97     def draw(self, context):
98         layout = self.layout
99         
100         col = layout.column(align=True)
101         
102         col.label(text="Operation:", icon="MOD_BOOLEAN")
103         
104         row = col.row()
105         union = row.operator("mesh.boolean", "Union")
106         union.modOp = 'UNION'
107         
108         intersect = row.operator("mesh.boolean", "Intersect")
109         intersect.modOp = 'INTERSECT'
110         
111         difference = row.operator("mesh.boolean", "Difference")
112         difference.modOp = 'DIFFERENCE'
113     
114 ###------- Define the Hotkeys and Register Operators ---------###
115
116 addon_keymaps = []
117     
118             
119 def register():
120     bpy.utils.register_class(boolean)
121     bpy.utils.register_class(booleanMenu)
122     bpy.utils.register_class(booleanToolbar)
123     
124     wm = bpy.context.window_manager
125     
126     # create the boolean menu hotkey
127     km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
128     kmi = km.keymap_items.new('wm.call_menu', 'B', 'PRESS', ctrl=True, shift=True)
129     kmi.properties.name = 'object.boolean_menu'
130
131     addon_keymaps.append(km)
132     
133 def unregister():
134     bpy.utils.unregister_class(boolean)
135     bpy.utils.unregister_class(booleanMenu)
136     bpy.utils.unregister_class(booleanToolbar)
137     
138     
139     # remove keymaps when add-on is deactivated
140     wm = bpy.context.window_manager
141     for km in addon_keymaps:
142         wm.keyconfigs.addon.keymaps.remove(km)
143     del addon_keymaps[:]
144     
145 if __name__ == "__main__":
146     register()