-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBoolMulti
246 lines (187 loc) · 7.92 KB
/
BoolMulti
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
bl_info = {
"name": "Bool Mod Apply Tool",
"author": "Robert Fornof",
"version": (1, 4),
"blender": (2, 8, 0),
"location": "View3D > Tool_Tab> BoolMod",
"description": "Set boolean",
"warning": "",
"wiki_url": "",
"category": "Object"}
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (Panel,
Operator,
AddonPreferences,
PropertyGroup,
)
import bpy
from bpy.app.handlers import persistent
#------------------- FUNCTIONS------------------------------
is28 = False # set this to true if you want to use in 2.8+
# Do the Basic Union, Difference and Intersection Operations
def Operation(context,_operation):
''' select the object, then select what you want it's mirror object to be '''
#select N context objects more than 1
#try:
# select objects
if(len(bpy.context.selected_objects)) < 2 :
return
# one is selected , add mirror mod immediately to that object#
# modifier_ob = bpy.context.active_object
# print("one is selected")
# boolean_mod = modifier_ob.modifiers.new("boolean_once","BOOLEAN")
# if modifier_ob.type !='MESH' and modifier_ob.type !="CURVE":
# boolean_ob = modifier_ob # set to boolean_ob , hope the other one is a mesh
# boolean_ob.select = False
# modifier_ob = bpy.context.selected_objects[0]
else:
selected = bpy.context.selected_objects
for i in range(len(selected)-1):
boolean_ob = selected[i]#bpy.context.active_object # last ob selected
if is28:
boolean_ob.select_set(False) # pop modifier_ob from sel_stack
else:
selected[0].select = False
#modifier_ob
modifier_ob = selected[i+1]
print("Modifier object:" +str(modifier_ob.name))
#modifier_ob.select=1
print("boolean_ob",boolean_ob)
print("modifier_ob",modifier_ob)
# put boolean modifier on modifier_ob
modifier_name = "boolean"
boolean_mod = modifier_ob.modifiers.new(modifier_name,"BOOLEAN")
# set boolean object to boolean_ob
boolean_mod.object = boolean_ob
boolean_mod.operation = _operation
if bpy.context.scene.isapply:
bpy.context.view_layer.objects.active = modifier_ob
bpy.ops.object.modifier_apply(modifier=modifier_name)
bpy.ops.object.delete({"selected_objects": [boolean_ob]})
else:
if is28:
boolean_ob.display_type = 'WIRE'
else:
selected[0].draw_type = 'WIRE'
#except Exception as e:
# print("error!" + str(e))
# print("please select exactly two objects, the last one gets the modifier unless its not a mesh")
#------------------- OPERATOR CLASSES ------------------------------
# Mirror Tool
class Union(bpy.types.Operator):
"""This adds a Union to the selected object"""
bl_idname = "object.union"
bl_label = "UNION"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"UNION")
return {'FINISHED'}
class Intersect(bpy.types.Operator):
"""This adds a Intersect modifier"""
bl_idname = "object.intersect"
bl_label = "INTERSECT"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"INTERSECT")
return {'FINISHED'}
class Difference(bpy.types.Operator):
"""This add a difference boolean modifier"""
bl_idname = "object.difference"
bl_label = "DIFFERENCE"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
Operation(context,"DIFFERENCE")
return {'FINISHED'}
#------------------- MENU CLASSES ------------------------------
class BooleanMenu(bpy.types.Menu):
bl_label = "Mirror_Mirror_Tool_MT_"
bl_idname = "OBJECT_MT_mirror"
def draw(self, context):
layout = self.layout
self.layout.operator(MirrorTool.bl_idname,icon = "ZOOMIN")
class BooleanTab(bpy.types.Panel):
#"[note]: Add a mirror on the x, y , or z axis using : ALT+SHIFT+X , ALT+SHIFT+Y, ALT+SHIFT+Z in object mode"
bl_label = "Mirror"
bl_idname = "Mirror_Mirror_Tool_PT_"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Mirror"
bl_context = "objectmode"
def draw(self,context):
layout = self.layout
box = layout.box()
box.label(text="Boolean on 2+ items:",icon = "MODIFIER")
box.operator(Union.bl_idname ,icon = "ZOOM_IN")
box.operator(Intersect.bl_idname ,icon = "ZOOM_IN")
box.operator(Difference.bl_idname ,icon = "ZOOM_IN")
box.separator()
scene = bpy.context.scene
mytool = scene.isapply
# display the properties
box.label(text=" Additional Settings",icon = "MODIFIER")
box.prop(scene, "isapply", text="Apply Modifier")
#---------- Tree Viewer--------------
def VIEW3D_BooleanMenu(self, context):
self.layout.menu(BooleanMenu.bl_idname)
#------------------- REGISTER ------------------------------
addon_keymaps = []
def register():
# Operators
bpy.types.Scene.isapply = bpy.props.BoolProperty(
name="Apply Modifier",
description="Apply modifier on selected",
default = True)
bpy.utils.register_class(Union)
bpy.utils.register_class(Intersect)
bpy.utils.register_class(Difference)
#Append 3DVIEW Menu
#bpy.utils.register_class(BooleanMenu)
#bpy.types.VIEW3D_MT_object.append(VIEW3D_BooleanMenu)
# Append 3DVIEW Tab
bpy.utils.register_class(BooleanTab)
# handle the keymap
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(Union.bl_idname, 'U', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(Intersect.bl_idname, 'I', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(Difference.bl_idname, 'D', 'PRESS', alt=True, shift = True)
addon_keymaps.append((km, kmi))
def unregister():
#bpy.utils.unregister_class(BooleanMenu)
bpy.utils.unregister_class(BooleanTab)
#bpy.types.VIEW3D_MT_object.remove(VIEW3D_BooleanMenu)
#Operators
bpy.utils.unregister_class(Union)
bpy.utils.unregister_class(Intersect)
bpy.utils.unregister_class(Difference)
del bpy.types.Scene.isapply
# bpy.app.handlers.scene_update_post.remove(HandleScene)
#bpy.types.VIEW3D_MT_object.remove(VIEW3D_BooleanMenu)
# Keymapping
# handle the keymap
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
try:
unregister()
except:
pass
register()