fix for lookup table
[blender-addons-contrib.git] / object_booleans.py
1 bl_info = {
2     "name": "Boolean Operators",
3     "location": "View3D > Toolshelf > Addons",
4     "description": "Add Boolean Tools for running boolean operations on two selected objects.",
5     "author": "Jonathan Williamson",
6     "version": (0, 4),
7     "blender": (2, 71, 0),
8     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/3D_interaction/booleanoperators",
9     "tracker_url": "https://developer.blender.org/T34502",
10     "category": "Object"}
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     bl_category = 'Tools'
97     bl_options = {'DEFAULT_CLOSED'}
98
99     def draw(self, context):
100         layout = self.layout
101
102         col = layout.column(align=True)
103
104         col.label(text="Operation:", icon="MOD_BOOLEAN")
105
106         row = col.row()
107         union = row.operator("mesh.boolean", "Union")
108         union.modOp = 'UNION'
109
110         intersect = row.operator("mesh.boolean", "Intersect")
111         intersect.modOp = 'INTERSECT'
112
113         difference = row.operator("mesh.boolean", "Difference")
114         difference.modOp = 'DIFFERENCE'
115
116 ###------- Define the Hotkeys and Register Operators ---------###
117
118 #addon_keymaps = []
119
120
121 def register():
122     bpy.utils.register_class(boolean)
123     bpy.utils.register_class(booleanMenu)
124     bpy.utils.register_class(booleanToolbar)
125
126     wm = bpy.context.window_manager
127
128     # create the boolean menu hotkey
129 #    km = wm.keyconfigs.addon.keymaps.new(name='Object Mode')
130 #    kmi = km.keymap_items.new('wm.call_menu', 'B', 'PRESS', ctrl=True, shift=True)
131 #    kmi.properties.name = 'object.boolean_menu'
132
133 #    addon_keymaps.append(km)
134
135 def unregister():
136     bpy.utils.unregister_class(boolean)
137     bpy.utils.unregister_class(booleanMenu)
138     bpy.utils.unregister_class(booleanToolbar)
139
140
141     # remove keymaps when add-on is deactivated
142     #wm = bpy.context.window_manager
143     #for km in addon_keymaps:
144         #wm.keyconfigs.addon.keymaps.remove(km)
145     #del addon_keymaps[:]
146
147 if __name__ == "__main__":
148     register()