glTF exporter: fix PBR values when emission node is used
[blender-addons.git] / object_boolean_tools.py
blobfaa93b12c508109fa22d8346bc08947a3ff324db
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # <pep8 compliant>
5 bl_info = {
6 "name": "Bool Tool",
7 "author": "Vitor Balbio, Mikhail Rachinskiy, TynkaTopi, Meta-Androcto, Simon Appelt",
8 "version": (0, 4, 1),
9 "blender": (2, 80, 0),
10 "location": "View3D > Sidebar > Edit Tab",
11 "description": "Bool Tool Hotkey: Ctrl Shift B",
12 "doc_url": "{BLENDER_MANUAL_URL}/addons/object/bool_tools.html",
13 "category": "Object",
16 import bpy
17 from bpy.types import (
18 AddonPreferences,
19 Operator,
20 Panel,
21 Menu,
23 from bpy.props import (
24 BoolProperty,
25 StringProperty,
29 # ------------------- Bool Tool FUNCTIONS -------------------------
30 # Utils:
32 # Hide boolean objects
33 def update_BoolHide(self, context):
34 ao = context.view_layer.objects.active
35 objs = [i.object for i in ao.modifiers if i.type == "BOOLEAN"]
36 hide_state = context.scene.BoolHide
38 for o in objs:
39 o.hide_viewport = hide_state
42 def isCanvas(_obj):
43 try:
44 if _obj["BoolToolRoot"]:
45 return True
46 except:
47 return False
50 def isBrush(_obj):
51 try:
52 if _obj["BoolToolBrush"]:
53 return True
54 except:
55 return False
58 # TODO
59 # def isPolyBrush(_obj):
60 # try:
61 # if _obj["BoolToolPolyBrush"]:
62 # return True
63 # except:
64 # return False
67 def object_visibility_set(ob, value=False):
68 ob.visible_camera = value
69 ob.visible_diffuse = value
70 ob.visible_glossy = value
71 ob.visible_shadow = value
72 ob.visible_transmission = value
73 ob.visible_volume_scatter = value
76 def BT_ObjectByName(obj):
77 for ob in bpy.context.view_layer.objects:
78 if isCanvas(ob) or isBrush(ob):
79 if ob.name == obj:
80 return ob
83 def FindCanvas(obj):
84 for ob in bpy.context.view_layer.objects:
85 if isCanvas(ob):
86 for mod in ob.modifiers:
87 if "BTool_" in mod.name:
88 if obj.name in mod.name:
89 return ob
92 def isFTransf():
93 preferences = bpy.context.preferences
94 addons = preferences.addons
95 addon_prefs = addons[__name__].preferences
96 if addon_prefs.fast_transform:
97 return True
98 else:
99 return False
102 def ConvertToMesh(obj):
103 act = bpy.context.view_layer.objects.active
104 bpy.context.view_layer.objects.active = obj
105 bpy.ops.object.convert(target="MESH")
106 bpy.context.view_layer.objects.active = act
109 # Do the Union, Difference and Intersection Operations with a Brush
110 def Operation(context, _operation):
111 prefs = context.preferences.addons[__name__].preferences
112 useWire = prefs.use_wire
114 for selObj in context.selected_objects:
115 if (
116 selObj != context.active_object and
117 (selObj.type == "MESH" or selObj.type == "CURVE")
119 if selObj.type == "CURVE":
120 ConvertToMesh(selObj)
121 actObj = context.active_object
122 selObj.hide_render = True
124 if useWire:
125 selObj.display_type = "WIRE"
126 else:
127 selObj.display_type = "BOUNDS"
129 object_visibility_set(selObj, value=False)
131 if _operation == "SLICE":
132 # copies instance_collection property(empty), but group property is empty (users_group = None)
133 clone = actObj.copy()
134 context.collection.objects.link(clone)
136 space_data = context.space_data
137 is_local_view = bool(space_data.local_view)
139 if is_local_view:
140 clone.local_view_set(space_data, True)
142 sliceMod = clone.modifiers.new("BTool_" + selObj.name, "BOOLEAN") # add mod to clone obj
143 sliceMod.object = selObj
144 sliceMod.operation = "DIFFERENCE"
145 clone["BoolToolRoot"] = True
147 newMod = actObj.modifiers.new("BTool_" + selObj.name, "BOOLEAN")
148 newMod.object = selObj
150 if _operation == "SLICE":
151 newMod.operation = "INTERSECT"
152 else:
153 newMod.operation = _operation
155 actObj["BoolToolRoot"] = True
156 selObj["BoolToolBrush"] = _operation
157 selObj["BoolTool_FTransform"] = "False"
160 # Remove Objects form the BoolTool System
161 def Remove(context, thisObj_name, Prop):
162 # Find the Brush pointed in the Tree View and Restore it, active is the Canvas
163 actObj = context.active_object
165 # Restore the Brush
166 def RemoveThis(_thisObj_name):
167 for obj in bpy.context.view_layer.objects:
168 # if it's the brush object
169 if obj.name == _thisObj_name:
170 obj.display_type = "TEXTURED"
171 del obj["BoolToolBrush"]
172 del obj["BoolTool_FTransform"]
173 object_visibility_set(obj, value=True)
175 # Remove it from the Canvas
176 for mod in actObj.modifiers:
177 if "BTool_" in mod.name:
178 if _thisObj_name in mod.name:
179 actObj.modifiers.remove(mod)
181 if Prop == "THIS":
182 RemoveThis(thisObj_name)
184 # If the remove was called from the Properties:
185 else:
186 # Remove the Brush Property
187 if Prop == "BRUSH":
188 Canvas = FindCanvas(actObj)
190 if Canvas:
191 for mod in Canvas.modifiers:
192 if "BTool_" in mod.name and actObj.name in mod.name:
193 Canvas.modifiers.remove(mod)
195 actObj.display_type = "TEXTURED"
196 del actObj["BoolToolBrush"]
197 del actObj["BoolTool_FTransform"]
198 object_visibility_set(actObj, value=True)
200 if Prop == "CANVAS":
201 for mod in actObj.modifiers:
202 if "BTool_" in mod.name:
203 RemoveThis(mod.object.name)
206 # Toggle the Enable the Brush Object Property
207 def EnableBrush(context, objList, canvas):
208 for obj in objList:
209 for mod in canvas.modifiers:
210 if "BTool_" in mod.name and mod.object.name == obj:
212 if mod.show_viewport:
213 mod.show_viewport = False
214 mod.show_render = False
215 else:
216 mod.show_viewport = True
217 mod.show_render = True
220 # Find the Canvas and Enable this Brush
221 def EnableThisBrush(context, set):
222 canvas = None
223 for obj in bpy.context.view_layer.objects:
224 if obj != bpy.context.active_object:
225 if isCanvas(obj):
226 for mod in obj.modifiers:
227 if "BTool_" in mod.name:
228 if mod.object == bpy.context.active_object:
229 canvas = obj
231 for mod in canvas.modifiers:
232 if "BTool_" in mod.name:
233 if mod.object == bpy.context.active_object:
234 if set == "None":
235 if mod.show_viewport:
236 mod.show_viewport = False
237 mod.show_render = False
238 else:
239 mod.show_viewport = True
240 mod.show_render = True
241 else:
242 if set == "True":
243 mod.show_viewport = True
244 else:
245 mod.show_viewport = False
246 return
249 # Toggle the Fast Transform Property of the Active Brush
250 def EnableFTransf(context):
251 actObj = bpy.context.active_object
253 if actObj["BoolTool_FTransform"] == "True":
254 actObj["BoolTool_FTransform"] = "False"
255 else:
256 actObj["BoolTool_FTransform"] = "True"
257 return
260 # Apply All Brushes to the Canvas
261 def ApplyAll(context, list):
262 objDeleteList = []
263 for selObj in list:
264 if isCanvas(selObj) and selObj == context.active_object:
265 for mod in selObj.modifiers:
266 if "BTool_" in mod.name:
267 objDeleteList.append(mod.object)
268 try:
269 bpy.ops.object.modifier_apply(modifier=mod.name)
270 except: # if fails the means it is multiuser data
271 context.active_object.data = context.active_object.data.copy() # so just make data unique
272 bpy.ops.object.modifier_apply(modifier=mod.name)
273 del selObj["BoolToolRoot"]
275 for obj in context.scene.objects:
276 if isCanvas(obj):
277 for mod in obj.modifiers:
278 # do not delete brush that is used by another canvas
279 if mod.type == "BOOLEAN" and mod.object in objDeleteList:
280 objDeleteList.remove(mod.object) # remove it from deletion
282 bpy.ops.object.select_all(action="DESELECT")
283 for obj in objDeleteList:
284 obj.select_set(True)
285 bpy.ops.object.delete()
288 # Apply This Brush to the Canvas
289 def ApplyThisBrush(context, brush):
290 for obj in context.scene.objects:
291 if isCanvas(obj):
292 for mod in obj.modifiers:
293 if "BTool_" + brush.name in mod.name:
294 # Apply This Brush
295 context.view_layer.objects.active = obj
296 try:
297 bpy.ops.object.modifier_apply(modifier=mod.name)
298 except: # if fails the means it is multiuser data
299 context.active_object.data = context.active_object.data.copy() # so just make data unique
300 bpy.ops.object.modifier_apply(modifier=mod.name)
301 bpy.ops.object.select_all(action="TOGGLE")
302 bpy.ops.object.select_all(action="DESELECT")
304 # Garbage Collector
305 brush.select_set(True)
306 # bpy.ops.object.delete()
309 # ------------------ Bool Tool OPERATORS --------------------------------------
311 # TODO
312 # class BTool_DrawPolyBrush(Operator):
313 # bl_idname = "btool.draw_polybrush"
314 # bl_label = "Draw Poly Brush"
315 # bl_description = (
316 # "Draw Polygonal Mask, can be applied to Canvas > Brush or Directly\n"
317 # "Note: ESC to Cancel, Enter to Apply, Right Click to erase the Lines"
320 # count = 0
321 # store_cont_draw = False
323 # @classmethod
324 # def poll(cls, context):
325 # return context.active_object is not None
327 # def set_cont_draw(self, context, start=False):
328 # # store / restore GP continuous drawing (see T52321)
329 # scene = context.scene
330 # tool_settings = scene.tool_settings
331 # continuous = tool_settings.use_gpencil_continuous_drawing
332 # if start:
333 # self.store_cont_draw = continuous
334 # tool_settings.use_gpencil_continuous_drawing = True
335 # else:
336 # tool_settings.use_gpencil_continuous_drawing = self.store_cont_draw
338 # def modal(self, context, event):
339 # self.count += 1
340 # actObj = bpy.context.active_object
341 # if self.count == 1:
342 # actObj.select_set(True)
343 # bpy.ops.gpencil.draw("INVOKE_DEFAULT", mode="DRAW_POLY")
345 # if event.type == "RIGHTMOUSE":
346 # # use this to pass to the Grease Pencil eraser (see T52321)
347 # pass
349 # if event.type in {"RET", "NUMPAD_ENTER"}:
351 # bpy.ops.gpencil.convert(type="POLY")
352 # self.set_cont_draw(context)
354 # for obj in context.selected_objects:
355 # if obj.type == "CURVE":
356 # obj.name = "PolyDraw"
357 # bpy.context.view_layer.objects.active = obj
358 # bpy.ops.object.select_all(action="DESELECT")
359 # obj.select_set(True)
360 # bpy.ops.object.convert(target="MESH")
361 # bpy.ops.object.mode_set(mode="EDIT")
362 # bpy.ops.mesh.select_all(action="SELECT")
363 # bpy.ops.mesh.edge_face_add()
364 # bpy.ops.mesh.flip_normals()
365 # bpy.ops.object.mode_set(mode="OBJECT")
366 # bpy.ops.object.origin_set(type="ORIGIN_CENTER_OF_MASS")
367 # bpy.ops.object.modifier_add(type="SOLIDIFY")
368 # for mod in obj.modifiers:
369 # if mod.name == "Solidify":
370 # mod.name = "BTool_PolyBrush"
371 # mod.thickness = 1
372 # mod.offset = 0
373 # obj["BoolToolPolyBrush"] = True
375 # bpy.ops.object.select_all(action="DESELECT")
376 # bpy.context.view_layer.objects.active = actObj
377 # bpy.context.view_layer.update()
378 # actObj.select_set(True)
379 # obj.select_set(True)
381 # bpy.context.view_layer.grease_pencil.clear()
382 # bpy.ops.gpencil.data_unlink()
384 # return {"FINISHED"}
386 # if event.type == "ESC":
387 # bpy.ops.ed.undo() # remove o Grease Pencil
388 # self.set_cont_draw(context)
390 # self.report({"INFO"}, "Draw Poly Brush: Operation Cancelled by User")
391 # return {"CANCELLED"}
393 # return {"RUNNING_MODAL"}
395 # def invoke(self, context, event):
396 # if context.object:
397 # self.set_cont_draw(context, start=True)
398 # context.window_manager.modal_handler_add(self)
399 # return {"RUNNING_MODAL"}
400 # else:
401 # self.report({"WARNING"}, "No active object, could not finish")
402 # return {"CANCELLED"}
405 # Fast Transform
406 class BTool_FastTransform(Operator):
407 bl_idname = "btool.fast_transform"
408 bl_label = "Fast Transform"
409 bl_description = "Enable Fast Transform"
411 operator: StringProperty("")
413 count = 0
415 def modal(self, context, event):
416 self.count += 1
417 actObj = bpy.context.active_object
418 useWire = bpy.context.preferences.addons[__name__].preferences.use_wire
419 if self.count == 1:
421 if isBrush(actObj) and actObj["BoolTool_FTransform"] == "True":
422 EnableThisBrush(bpy.context, "False")
423 if useWire:
424 actObj.display_type = "WIRE"
425 else:
426 actObj.display_type = "BOUNDS"
428 if self.operator == "Translate":
429 bpy.ops.transform.translate("INVOKE_DEFAULT")
430 if self.operator == "Rotate":
431 bpy.ops.transform.rotate("INVOKE_DEFAULT")
432 if self.operator == "Scale":
433 bpy.ops.transform.resize("INVOKE_DEFAULT")
435 if event.type == "LEFTMOUSE":
436 if isBrush(actObj):
437 EnableThisBrush(bpy.context, "True")
438 actObj.display_type = "WIRE"
439 return {"FINISHED"}
441 if event.type in {"RIGHTMOUSE", "ESC"}:
442 if isBrush(actObj):
443 EnableThisBrush(bpy.context, "True")
444 actObj.display_type = "WIRE"
445 return {"CANCELLED"}
447 return {"RUNNING_MODAL"}
449 def invoke(self, context, event):
450 if context.object:
451 context.window_manager.modal_handler_add(self)
452 return {"RUNNING_MODAL"}
453 else:
454 self.report({"WARNING"}, "No active object, could not finish")
455 return {"CANCELLED"}
458 # ------------------- Bool Tool OPERATOR CLASSES --------------------------------------------------------
461 # Brush operators
462 # --------------------------------------------------------------------------------------
465 class BToolSetup():
467 def execute(self, context):
468 Operation(context, self.mode)
469 return {"FINISHED"}
471 def invoke(self, context, event):
472 if len(context.selected_objects) < 2:
473 self.report({"ERROR"}, "At least two objects must be selected")
474 return {"CANCELLED"}
476 return self.execute(context)
479 class BTool_Union(Operator, BToolSetup):
480 bl_idname = "btool.boolean_union"
481 bl_label = "Brush Union"
482 bl_description = "This operator add a union brush to a canvas"
483 bl_options = {"REGISTER", "UNDO"}
485 mode = "UNION"
488 class BTool_Inters(Operator, BToolSetup):
489 bl_idname = "btool.boolean_inters"
490 bl_label = "Brush Intersection"
491 bl_description = "This operator add a intersect brush to a canvas"
492 bl_options = {"REGISTER", "UNDO"}
494 mode = "INTERSECT"
497 class BTool_Diff(Operator, BToolSetup):
498 bl_idname = "btool.boolean_diff"
499 bl_label = "Brush Difference"
500 bl_description = "This operator add a difference brush to a canvas"
501 bl_options = {"REGISTER", "UNDO"}
503 mode = "DIFFERENCE"
506 class BTool_Slice(Operator, BToolSetup):
507 bl_idname = "btool.boolean_slice"
508 bl_label = "Brush Slice"
509 bl_description = "This operator add a intersect brush to a canvas"
510 bl_options = {"REGISTER", "UNDO"}
512 mode = "SLICE"
515 # Auto Boolean operators
516 # --------------------------------------------------------------------------------------
519 class Auto_Boolean:
521 def objects_prepare(self):
522 for ob in bpy.context.selected_objects:
523 if ob.type != "MESH":
524 ob.select_set(False)
525 bpy.ops.object.make_single_user(object=True, obdata=True)
526 bpy.ops.object.convert(target="MESH")
528 def mesh_selection(self, ob, select_action):
529 obj = bpy.context.active_object
531 bpy.context.view_layer.objects.active = ob
532 bpy.ops.object.mode_set(mode="EDIT")
534 bpy.ops.mesh.reveal()
535 bpy.ops.mesh.select_all(action=select_action)
537 bpy.ops.object.mode_set(mode="OBJECT")
538 bpy.context.view_layer.objects.active = obj
540 def boolean_operation(self):
541 obj = bpy.context.active_object
542 obj.select_set(False)
543 obs = bpy.context.selected_objects
545 self.mesh_selection(obj, "DESELECT")
547 for ob in obs:
548 self.mesh_selection(ob, "SELECT")
549 self.boolean_mod(obj, ob, self.mode)
551 obj.select_set(True)
553 def boolean_mod(self, obj, ob, mode, ob_delete=True):
554 md = obj.modifiers.new("Auto Boolean", "BOOLEAN")
555 md.show_viewport = False
556 md.operation = mode
557 md.object = ob
559 override = {"object": obj}
560 bpy.ops.object.modifier_apply(override, modifier=md.name)
562 if ob_delete:
563 bpy.data.objects.remove(ob)
565 def execute(self, context):
566 self.objects_prepare()
567 self.boolean_operation()
568 return {"FINISHED"}
570 def invoke(self, context, event):
571 if len(context.selected_objects) < 2:
572 self.report({"ERROR"}, "At least two objects must be selected")
573 return {"CANCELLED"}
575 return self.execute(context)
578 class OBJECT_OT_BoolTool_Auto_Union(Operator, Auto_Boolean):
579 bl_idname = "object.booltool_auto_union"
580 bl_label = "Bool Tool Union"
581 bl_description = "Combine selected objects"
582 bl_options = {"REGISTER", "UNDO"}
584 mode = "UNION"
587 class OBJECT_OT_BoolTool_Auto_Difference(Operator, Auto_Boolean):
588 bl_idname = "object.booltool_auto_difference"
589 bl_label = "Bool Tool Difference"
590 bl_description = "Subtract selected objects from active object"
591 bl_options = {"REGISTER", "UNDO"}
593 mode = "DIFFERENCE"
596 class OBJECT_OT_BoolTool_Auto_Intersect(Operator, Auto_Boolean):
597 bl_idname = "object.booltool_auto_intersect"
598 bl_label = "Bool Tool Intersect"
599 bl_description = "Keep only intersecting geometry"
600 bl_options = {"REGISTER", "UNDO"}
602 mode = "INTERSECT"
605 class OBJECT_OT_BoolTool_Auto_Slice(Operator, Auto_Boolean):
606 bl_idname = "object.booltool_auto_slice"
607 bl_label = "Bool Tool Slice"
608 bl_description = "Slice active object along the selected objects"
609 bl_options = {"REGISTER", "UNDO"}
611 def execute(self, context):
612 space_data = context.space_data
613 is_local_view = bool(space_data.local_view)
614 self.objects_prepare()
616 ob1 = context.active_object
617 ob1.select_set(False)
618 self.mesh_selection(ob1, "DESELECT")
620 for ob2 in context.selected_objects:
622 self.mesh_selection(ob2, "SELECT")
624 ob1_copy = ob1.copy()
625 ob1_copy.data = ob1.data.copy()
627 for coll in ob1.users_collection:
628 coll.objects.link(ob1_copy)
630 if is_local_view:
631 ob1_copy.local_view_set(space_data, True)
633 self.boolean_mod(ob1, ob2, "DIFFERENCE", ob_delete=False)
634 self.boolean_mod(ob1_copy, ob2, "INTERSECT")
635 ob1_copy.select_set(True)
637 context.view_layer.objects.active = ob1_copy
639 return {"FINISHED"}
642 # Utils Class ---------------------------------------------------------------
644 # Find the Brush Selected in Three View
645 class BTool_FindBrush(Operator):
646 bl_idname = "btool.find_brush"
647 bl_label = ""
648 bl_description = "Find the selected brush"
650 obj: StringProperty("")
652 @classmethod
653 def poll(cls, context):
654 return context.active_object is not None
656 def execute(self, context):
657 for ob in bpy.context.view_layer.objects:
658 if ob.name == self.obj:
659 bpy.ops.object.select_all(action="TOGGLE")
660 bpy.ops.object.select_all(action="DESELECT")
661 bpy.context.view_layer.objects.active = ob
662 ob.set_select(state=True)
663 return {"FINISHED"}
666 # Move The Modifier in The Stack Up or Down
667 class BTool_MoveStack(Operator):
668 bl_idname = "btool.move_stack"
669 bl_label = ""
670 bl_description = "Move this Brush Up/Down in the Stack"
672 modif: StringProperty("")
673 direction: StringProperty("")
675 @classmethod
676 def poll(cls, context):
677 return context.active_object is not None
679 def execute(self, context):
680 if self.direction == "UP":
681 bpy.ops.object.modifier_move_up(modifier=self.modif)
682 if self.direction == "DOWN":
683 bpy.ops.object.modifier_move_down(modifier=self.modif)
684 return {"FINISHED"}
687 # Enable or Disable a Brush in the Three View
688 class BTool_EnableBrush(Operator):
689 bl_idname = "btool.enable_brush"
690 bl_label = ""
691 bl_description = "Removes all BoolTool config assigned to it"
693 thisObj: StringProperty("")
695 @classmethod
696 def poll(cls, context):
697 return context.active_object is not None
699 def execute(self, context):
700 # in this case is just one object but the function accept more than one at once
701 EnableBrush(context, [self.thisObj], context.active_object)
702 return {"FINISHED"}
705 # Enable or Disable a Brush Directly
706 class BTool_EnableThisBrush(Operator):
707 bl_idname = "btool.enable_this_brush"
708 bl_label = ""
709 bl_description = "Toggles this brush"
711 @classmethod
712 def poll(cls, context):
713 return context.active_object is not None
715 def execute(self, context):
716 EnableThisBrush(context, "None")
717 return {"FINISHED"}
720 # Enable or Disable a Brush Directly
721 class BTool_EnableFTransform(Operator):
722 bl_idname = "btool.enable_ftransf"
723 bl_label = ""
724 bl_description = "Use Fast Transformations to improve speed"
726 @classmethod
727 def poll(cls, context):
728 return context.active_object is not None
730 def execute(self, context):
731 EnableFTransf(context)
732 return {"FINISHED"}
735 # Other Operations -------------------------------------------------------
737 # Remove a Brush or a Canvas
738 class BTool_Remove(Operator):
739 bl_idname = "btool.remove"
740 bl_label = "Bool Tool Remove"
741 bl_description = "Removes all BoolTool config assigned to it"
742 bl_options = {"UNDO"}
744 thisObj: StringProperty("")
745 Prop: StringProperty("")
747 @classmethod
748 def poll(cls, context):
749 return context.active_object is not None
751 def execute(self, context):
752 Remove(context, self.thisObj, self.Prop)
753 return {"FINISHED"}
756 # Apply All to Canvas
757 class BTool_AllBrushToMesh(Operator):
758 bl_idname = "btool.to_mesh"
759 bl_label = "Apply All Canvas"
760 bl_description = "Apply all brushes of this canvas"
761 bl_options = {"UNDO"}
763 @classmethod
764 def poll(cls, context):
765 return context.active_object is not None
767 def execute(self, context):
768 lists = bpy.context.selected_objects
769 ApplyAll(context, lists)
770 return {"FINISHED"}
773 # Apply This Brush to the Canvas
774 class BTool_BrushToMesh(Operator):
775 bl_idname = "btool.brush_to_mesh"
776 bl_label = "Apply this Brush to Canvas"
777 bl_description = "Apply this brush to the canvas"
778 bl_options = {"UNDO"}
780 @classmethod
781 def poll(cls, context):
783 if isBrush(context.active_object):
784 return True
785 else:
786 return False
788 def execute(self, context):
789 ApplyThisBrush(context, bpy.context.active_object)
790 return {"FINISHED"}
793 # TODO
794 # Apply This Brush To Mesh
797 # ------------------- MENU CLASSES ------------------------------
799 # 3Dview Header Menu
800 class VIEW3D_MT_booltool_menu(Menu):
801 bl_label = "Bool Tool"
802 bl_idname = "VIEW3D_MT_booltool_menu"
804 def draw(self, context):
805 layout = self.layout
807 layout.label(text="Auto Boolean")
808 layout.operator(OBJECT_OT_BoolTool_Auto_Difference.bl_idname, text="Difference", icon="SELECT_SUBTRACT")
809 layout.operator(OBJECT_OT_BoolTool_Auto_Union.bl_idname, text="Union", icon="SELECT_EXTEND")
810 layout.operator(OBJECT_OT_BoolTool_Auto_Intersect.bl_idname, text="Intersect", icon="SELECT_INTERSECT")
811 layout.operator(OBJECT_OT_BoolTool_Auto_Slice.bl_idname, text="Slice", icon="SELECT_DIFFERENCE")
813 layout.separator()
815 layout.label(text="Brush Boolean")
816 layout.operator(BTool_Diff.bl_idname, text="Difference", icon="SELECT_SUBTRACT")
817 layout.operator(BTool_Union.bl_idname, text="Union", icon="SELECT_EXTEND")
818 layout.operator(BTool_Inters.bl_idname, text="Intersect", icon="SELECT_INTERSECT")
819 layout.operator(BTool_Slice.bl_idname, text="Slice", icon="SELECT_DIFFERENCE")
821 if isCanvas(context.active_object):
822 layout.separator()
823 layout.operator(BTool_AllBrushToMesh.bl_idname, icon="MOD_LATTICE", text="Apply All")
824 Rem = layout.operator(BTool_Remove.bl_idname, icon="X", text="Remove All")
825 Rem.thisObj = ""
826 Rem.Prop = "CANVAS"
828 if isBrush(context.active_object):
829 layout.separator()
830 layout.operator(BTool_BrushToMesh.bl_idname, icon="MOD_LATTICE", text="Apply Brush")
831 Rem = layout.operator(BTool_Remove.bl_idname, icon="X", text="Remove Brush")
832 Rem.thisObj = ""
833 Rem.Prop = "BRUSH"
836 def VIEW3D_BoolTool_Menu(self, context):
837 self.layout.menu(VIEW3D_MT_booltool_menu.bl_idname)
840 # ---------------- Toolshelf: Tools ---------------------
843 class VIEW3D_PT_booltool_tools(Panel):
844 bl_category = "objectmode"
845 bl_label = "Bool Tool"
846 bl_space_type = "VIEW_3D"
847 bl_region_type = "UI"
848 bl_context = "objectmode"
849 bl_options = {'DEFAULT_CLOSED'}
851 @classmethod
852 def poll(cls, context):
853 return context.active_object is not None
855 def draw(self, context):
856 layout = self.layout
858 col = layout.column(align=True)
859 col.label(text="Auto Boolean")
860 col.operator(OBJECT_OT_BoolTool_Auto_Difference.bl_idname, text="Difference", icon="SELECT_SUBTRACT")
861 col.operator(OBJECT_OT_BoolTool_Auto_Union.bl_idname, text="Union", icon="SELECT_EXTEND")
862 col.operator(OBJECT_OT_BoolTool_Auto_Intersect.bl_idname, text="Intersect", icon="SELECT_INTERSECT")
863 col.operator(OBJECT_OT_BoolTool_Auto_Slice.bl_idname, text="Slice", icon="SELECT_DIFFERENCE")
865 col = layout.column(align=True)
866 col.label(text="Brush Boolean")
867 col.operator(BTool_Diff.bl_idname, text="Difference", icon="SELECT_SUBTRACT")
868 col.operator(BTool_Union.bl_idname, text="Union", icon="SELECT_EXTEND")
869 col.operator(BTool_Inters.bl_idname, text="Intersect", icon="SELECT_INTERSECT")
870 col.operator(BTool_Slice.bl_idname, text="Slice", icon="SELECT_DIFFERENCE")
872 # TODO Draw Poly Brush
873 # main.separator()
875 # col = main.column(align=True)
876 # col.label(text="Draw:", icon="MESH_CUBE")
877 # col.separator()
878 # col.operator(BTool_DrawPolyBrush.bl_idname, icon="LINE_DATA")
881 # ---------- Toolshelf: Properties --------------------------------------------------------
884 class VIEW3D_PT_booltool_config(Panel):
885 bl_category = "objectmode"
886 bl_label = "Properties"
887 bl_space_type = "VIEW_3D"
888 bl_region_type = "UI"
889 bl_context = "objectmode"
890 bl_parent_id = "VIEW3D_PT_booltool_tools"
892 @classmethod
893 def poll(cls, context):
894 actObj = context.active_object
895 return isCanvas(actObj) or isBrush(actObj) # or isPolyBrush(actObj)
897 def draw(self, context):
898 layout = self.layout
899 actObj = context.active_object
901 row = layout.row(align=True)
903 if isCanvas(actObj):
905 row.label(text="CANVAS", icon="MESH_GRID")
906 row = layout.row()
907 row.prop(context.scene, "BoolHide", text="Hide Bool objects")
908 row = layout.row(align=True)
909 row.operator(BTool_AllBrushToMesh.bl_idname, icon="MOD_LATTICE", text="Apply All")
911 row = layout.row(align=True)
912 Rem = row.operator(BTool_Remove.bl_idname, icon="X", text="Remove All")
913 Rem.thisObj = ""
914 Rem.Prop = "CANVAS"
916 if isBrush(actObj):
917 layout.separator()
919 if isBrush(actObj):
921 if actObj["BoolToolBrush"] == "DIFFERENCE":
922 icon = "SELECT_SUBTRACT"
923 elif actObj["BoolToolBrush"] == "UNION":
924 icon = "SELECT_EXTEND"
925 elif actObj["BoolToolBrush"] == "INTERSECT":
926 icon = "SELECT_INTERSECT"
927 elif actObj["BoolToolBrush"] == "SLICE":
928 icon = "SELECT_DIFFERENCE"
930 row.label(text="BRUSH", icon=icon)
932 if actObj["BoolTool_FTransform"] == "True":
933 icon = "PMARKER_ACT"
934 else:
935 icon = "PMARKER"
936 if isFTransf():
937 pass
939 if isFTransf():
940 row = layout.row(align=True)
941 row.operator(BTool_EnableFTransform.bl_idname, text="Fast Vis", icon=icon)
942 row.operator(BTool_EnableThisBrush.bl_idname, text="Enable", icon="HIDE_OFF")
943 else:
944 row.operator(BTool_EnableThisBrush.bl_idname, icon="HIDE_OFF")
946 layout.operator(BTool_BrushToMesh.bl_idname, icon="MOD_LATTICE", text="Apply Brush")
947 Rem = layout.operator(BTool_Remove.bl_idname, icon="X", text="Remove Brush")
948 Rem.thisObj = ""
949 Rem.Prop = "BRUSH"
951 # TODO
952 # if isPolyBrush(actObj):
953 # layout.label(text="POLY BRUSH", icon="LINE_DATA")
954 # mod = actObj.modifiers["BTool_PolyBrush"]
955 # layout.prop(mod, "thickness", text="Size")
958 # ---------- Toolshelf: Brush Viewer -------------------------------------------------------
961 class VIEW3D_PT_booltool_bviewer(Panel):
962 bl_category = "objectmode"
963 bl_label = "Brush Viewer"
964 bl_space_type = "VIEW_3D"
965 bl_region_type = "UI"
966 bl_context = "objectmode"
967 bl_parent_id = "VIEW3D_PT_booltool_tools"
969 @classmethod
970 def poll(cls, context):
971 actObj = bpy.context.active_object
973 if isCanvas(actObj):
974 return True
975 else:
976 return False
978 def draw(self, context):
980 actObj = bpy.context.active_object
982 if isCanvas(actObj):
984 for mod in actObj.modifiers:
985 container = self.layout.box()
986 row = container.row(align=True)
988 if "BTool_" in mod.name:
990 if mod.operation == "DIFFERENCE":
991 icon = "SELECT_SUBTRACT"
992 elif mod.operation == "UNION":
993 icon = "SELECT_EXTEND"
994 elif mod.operation == "INTERSECT":
995 icon = "SELECT_INTERSECT"
996 elif mod.operation == "SLICE":
997 icon = "SELECT_DIFFERENCE"
999 objSelect = row.operator("btool.find_brush", text=mod.object.name, icon=icon, emboss=False)
1000 objSelect.obj = mod.object.name
1002 EnableIcon = "RESTRICT_VIEW_ON"
1003 if mod.show_viewport:
1004 EnableIcon = "RESTRICT_VIEW_OFF"
1005 Enable = row.operator(BTool_EnableBrush.bl_idname, icon=EnableIcon, emboss=False)
1006 Enable.thisObj = mod.object.name
1008 Remove = row.operator("btool.remove", text="", icon="X", emboss=False)
1009 Remove.thisObj = mod.object.name
1010 Remove.Prop = "THIS"
1012 else:
1013 row.label(text=mod.name)
1015 Up = row.operator("btool.move_stack", icon="TRIA_UP", emboss=False)
1016 Up.modif = mod.name
1017 Up.direction = "UP"
1019 Dw = row.operator("btool.move_stack", icon="TRIA_DOWN", emboss=False)
1020 Dw.modif = mod.name
1021 Dw.direction = "DOWN"
1024 # ------------------ BOOL TOOL ADD-ON PREFERENCES ----------------------------
1027 shortcut_list = (
1028 ("3D View", None),
1029 ("Menu", "Ctrl Shift B"),
1031 ("Auto Operators", None),
1032 ("Difference", "Ctrl Shift Num -"),
1033 ("Union", "Ctrl Shift Num +"),
1034 ("Intersect", "Ctrl Shift Num *"),
1035 ("Slice", "Ctrl Shift Num /"),
1037 ("Brush Operators", None),
1038 ("Difference", "Ctrl Num -"),
1039 ("Union", "Ctrl Num +"),
1040 ("Intersect", "Ctrl Num *"),
1041 ("Slice", "Ctrl Num /"),
1042 ("Brush To Mesh", "Ctrl Num Enter"),
1043 ("All Brushes To Mesh", "Ctrl Shift Num Enter"),
1047 def UpdateBoolTool_Pref(self, context):
1048 if self.fast_transform:
1049 RegisterFastT()
1050 else:
1051 UnRegisterFastT()
1054 # Define Panel classes for updating
1055 panels = (
1056 VIEW3D_PT_booltool_tools,
1057 VIEW3D_PT_booltool_config,
1058 VIEW3D_PT_booltool_bviewer,
1062 def update_panels(self, context):
1063 try:
1064 for panel in panels:
1065 if "bl_rna" in panel.__dict__:
1066 bpy.utils.unregister_class(panel)
1068 for panel in panels:
1069 panel.bl_category = context.preferences.addons[
1070 __name__
1071 ].preferences.category
1072 bpy.utils.register_class(panel)
1074 except Exception as e:
1075 message = "Bool Tool: Updating Panel locations has failed"
1076 print("\n[{}]\n{}\n\nError:\n{}".format(__name__, message, e))
1079 def icon_tria(prop):
1080 if prop:
1081 return "TRIA_DOWN"
1082 return "TRIA_RIGHT"
1085 class PREFS_BoolTool_Props(AddonPreferences):
1086 bl_idname = __name__
1088 fast_transform: BoolProperty(
1089 name="Fast Transformations",
1090 update=UpdateBoolTool_Pref,
1091 description="Replace the Transform HotKeys (G,R,S)\n"
1092 "for a custom version that can optimize the visualization of Brushes",
1094 use_wire: BoolProperty(
1095 name="Display As Wirewrame",
1096 description="Display brush as wireframe instead of bounding box",
1098 category: StringProperty(
1099 name="Tab Name",
1100 description="Set sidebar tab name",
1101 default="Edit",
1102 update=update_panels,
1104 show_shortcuts: BoolProperty(name="Shortcuts")
1106 def draw(self, context):
1107 layout = self.layout
1108 layout.use_property_split = True
1109 layout.use_property_decorate = False
1111 col = layout.column()
1112 col.prop(self, "category")
1113 col.prop(self, "fast_transform")
1114 col.prop(self, "use_wire")
1116 col = layout.column()
1117 col.scale_y = 1.2
1118 col.use_property_split = False
1119 col.prop(self, "show_shortcuts", icon=icon_tria(self.show_shortcuts))
1121 if self.show_shortcuts:
1123 col = layout.column()
1125 for key_name, key_comb in shortcut_list:
1126 if key_comb is None:
1127 col.separator()
1128 col.label(text=key_name)
1129 else:
1130 row = col.row(align=True)
1131 row.scale_y = 0.7
1132 row.box().label(text=key_name)
1133 row.box().label(text=key_comb)
1136 # ------------------- Class List ------------------------------------------------
1138 classes = (
1139 PREFS_BoolTool_Props,
1140 VIEW3D_MT_booltool_menu,
1141 VIEW3D_PT_booltool_tools,
1142 VIEW3D_PT_booltool_config,
1143 VIEW3D_PT_booltool_bviewer,
1144 OBJECT_OT_BoolTool_Auto_Union,
1145 OBJECT_OT_BoolTool_Auto_Difference,
1146 OBJECT_OT_BoolTool_Auto_Intersect,
1147 OBJECT_OT_BoolTool_Auto_Slice,
1148 BTool_Union,
1149 BTool_Diff,
1150 BTool_Inters,
1151 BTool_Slice,
1152 # TODO Draw Poly Brush
1153 # BTool_DrawPolyBrush,
1154 BTool_Remove,
1155 BTool_AllBrushToMesh,
1156 BTool_BrushToMesh,
1157 BTool_FindBrush,
1158 BTool_MoveStack,
1159 BTool_EnableBrush,
1160 BTool_EnableThisBrush,
1161 BTool_EnableFTransform,
1162 BTool_FastTransform,
1166 # ------------------- REGISTER ------------------------------------------------
1168 addon_keymaps = []
1169 addon_keymapsFastT = []
1172 # Fast Transform HotKeys Register
1173 def RegisterFastT():
1174 wm = bpy.context.window_manager
1175 km = wm.keyconfigs.addon.keymaps.new(name="Object Mode", space_type="EMPTY")
1177 kmi = km.keymap_items.new(BTool_FastTransform.bl_idname, "G", "PRESS")
1178 kmi.properties.operator = "Translate"
1179 addon_keymapsFastT.append((km, kmi))
1181 kmi = km.keymap_items.new(BTool_FastTransform.bl_idname, "R", "PRESS")
1182 kmi.properties.operator = "Rotate"
1183 addon_keymapsFastT.append((km, kmi))
1185 kmi = km.keymap_items.new(BTool_FastTransform.bl_idname, "S", "PRESS")
1186 kmi.properties.operator = "Scale"
1187 addon_keymapsFastT.append((km, kmi))
1190 # Fast Transform HotKeys UnRegister
1191 def UnRegisterFastT():
1192 wm = bpy.context.window_manager
1193 kc = wm.keyconfigs.addon
1194 if kc:
1195 for km, kmi in addon_keymapsFastT:
1196 km.keymap_items.remove(kmi)
1198 addon_keymapsFastT.clear()
1201 def register():
1202 for cls in classes:
1203 bpy.utils.register_class(cls)
1204 update_panels(None, bpy.context)
1206 # Scene variables
1207 bpy.types.Scene.BoolHide = BoolProperty(
1208 default=False,
1209 description="Hide boolean objects",
1210 update=update_BoolHide,
1212 bpy.types.VIEW3D_MT_object.append(VIEW3D_BoolTool_Menu)
1214 wm = bpy.context.window_manager
1215 kc = wm.keyconfigs.addon
1217 # create the boolean menu hotkey
1218 if kc is not None:
1219 km = kc.keymaps.new(name="Object Mode")
1221 kmi = km.keymap_items.new("wm.call_menu", "B", "PRESS", ctrl=True, shift=True)
1222 kmi.properties.name = "VIEW3D_MT_booltool_menu"
1223 addon_keymaps.append((km, kmi))
1225 # Brush Operators
1226 kmi = km.keymap_items.new(BTool_Union.bl_idname, "NUMPAD_PLUS", "PRESS", ctrl=True)
1227 addon_keymaps.append((km, kmi))
1228 kmi = km.keymap_items.new(BTool_Diff.bl_idname, "NUMPAD_MINUS", "PRESS", ctrl=True)
1229 addon_keymaps.append((km, kmi))
1230 kmi = km.keymap_items.new(BTool_Inters.bl_idname, "NUMPAD_ASTERIX", "PRESS", ctrl=True)
1231 addon_keymaps.append((km, kmi))
1232 kmi = km.keymap_items.new(BTool_Slice.bl_idname, "NUMPAD_SLASH", "PRESS", ctrl=True)
1233 addon_keymaps.append((km, kmi))
1234 kmi = km.keymap_items.new(BTool_BrushToMesh.bl_idname, "NUMPAD_ENTER", "PRESS", ctrl=True)
1235 addon_keymaps.append((km, kmi))
1236 kmi = km.keymap_items.new(
1237 BTool_AllBrushToMesh.bl_idname,
1238 "NUMPAD_ENTER",
1239 "PRESS",
1240 ctrl=True,
1241 shift=True,
1243 addon_keymaps.append((km, kmi))
1245 # Auto Operators
1246 kmi = km.keymap_items.new(
1247 OBJECT_OT_BoolTool_Auto_Union.bl_idname,
1248 "NUMPAD_PLUS",
1249 "PRESS",
1250 ctrl=True,
1251 shift=True,
1253 addon_keymaps.append((km, kmi))
1254 kmi = km.keymap_items.new(
1255 OBJECT_OT_BoolTool_Auto_Difference.bl_idname,
1256 "NUMPAD_MINUS",
1257 "PRESS",
1258 ctrl=True,
1259 shift=True,
1261 addon_keymaps.append((km, kmi))
1262 kmi = km.keymap_items.new(
1263 OBJECT_OT_BoolTool_Auto_Intersect.bl_idname,
1264 "NUMPAD_ASTERIX",
1265 "PRESS",
1266 ctrl=True,
1267 shift=True,
1269 addon_keymaps.append((km, kmi))
1270 kmi = km.keymap_items.new(
1271 OBJECT_OT_BoolTool_Auto_Slice.bl_idname,
1272 "NUMPAD_SLASH",
1273 "PRESS",
1274 ctrl=True,
1275 shift=True,
1277 addon_keymaps.append((km, kmi))
1280 def unregister():
1281 # Keymapping
1282 # remove keymaps when add-on is deactivated
1283 wm = bpy.context.window_manager
1284 kc = wm.keyconfigs.addon
1285 if kc is not None:
1286 for km, kmi in addon_keymaps:
1287 km.keymap_items.remove(kmi)
1289 addon_keymaps.clear()
1290 UnRegisterFastT()
1291 bpy.types.VIEW3D_MT_object.remove(VIEW3D_BoolTool_Menu)
1292 del bpy.types.Scene.BoolHide
1294 for cls in classes:
1295 bpy.utils.unregister_class(cls)
1298 if __name__ == "__main__":
1299 register()