1 # SPDX-License-Identifier: GPL-2.0-or-later
7 "author": "Vitor Balbio, Mikhail Rachinskiy, TynkaTopi, Meta-Androcto, Simon Appelt",
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",
17 from bpy
.types
import (
23 from bpy
.props
import (
29 # ------------------- Bool Tool FUNCTIONS -------------------------
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
39 o
.hide_viewport
= hide_state
44 if _obj
["BoolToolRoot"]:
52 if _obj
["BoolToolBrush"]:
59 # def isPolyBrush(_obj):
61 # if _obj["BoolToolPolyBrush"]:
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
):
84 for ob
in bpy
.context
.view_layer
.objects
:
86 for mod
in ob
.modifiers
:
87 if "BTool_" in mod
.name
:
88 if obj
.name
in mod
.name
:
93 preferences
= bpy
.context
.preferences
94 addons
= preferences
.addons
95 addon_prefs
= addons
[__name__
].preferences
96 if addon_prefs
.fast_transform
:
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
:
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
125 selObj
.display_type
= "WIRE"
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
)
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"
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
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
)
182 RemoveThis(thisObj_name
)
184 # If the remove was called from the Properties:
186 # Remove the Brush Property
188 Canvas
= FindCanvas(actObj
)
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)
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
):
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
216 mod
.show_viewport
= True
217 mod
.show_render
= True
220 # Find the Canvas and Enable this Brush
221 def EnableThisBrush(context
, set):
223 for obj
in bpy
.context
.view_layer
.objects
:
224 if obj
!= bpy
.context
.active_object
:
226 for mod
in obj
.modifiers
:
227 if "BTool_" in mod
.name
:
228 if mod
.object == bpy
.context
.active_object
:
231 for mod
in canvas
.modifiers
:
232 if "BTool_" in mod
.name
:
233 if mod
.object == bpy
.context
.active_object
:
235 if mod
.show_viewport
:
236 mod
.show_viewport
= False
237 mod
.show_render
= False
239 mod
.show_viewport
= True
240 mod
.show_render
= True
243 mod
.show_viewport
= True
245 mod
.show_viewport
= False
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"
256 actObj
["BoolTool_FTransform"] = "True"
260 # Apply All Brushes to the Canvas
261 def ApplyAll(context
, 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)
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
:
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
:
285 bpy
.ops
.object.delete()
288 # Apply This Brush to the Canvas
289 def ApplyThisBrush(context
, brush
):
290 for obj
in context
.scene
.objects
:
292 for mod
in obj
.modifiers
:
293 if "BTool_" + brush
.name
in mod
.name
:
295 context
.view_layer
.objects
.active
= obj
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")
305 brush
.select_set(True)
306 # bpy.ops.object.delete()
309 # ------------------ Bool Tool OPERATORS --------------------------------------
312 # class BTool_DrawPolyBrush(Operator):
313 # bl_idname = "btool.draw_polybrush"
314 # bl_label = "Draw Poly Brush"
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"
321 # store_cont_draw = False
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
333 # self.store_cont_draw = continuous
334 # tool_settings.use_gpencil_continuous_drawing = True
336 # tool_settings.use_gpencil_continuous_drawing = self.store_cont_draw
338 # def modal(self, context, event):
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)
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"
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):
397 # self.set_cont_draw(context, start=True)
398 # context.window_manager.modal_handler_add(self)
399 # return {"RUNNING_MODAL"}
401 # self.report({"WARNING"}, "No active object, could not finish")
402 # return {"CANCELLED"}
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("")
415 def modal(self
, context
, event
):
417 actObj
= bpy
.context
.active_object
418 useWire
= bpy
.context
.preferences
.addons
[__name__
].preferences
.use_wire
421 if isBrush(actObj
) and actObj
["BoolTool_FTransform"] == "True":
422 EnableThisBrush(bpy
.context
, "False")
424 actObj
.display_type
= "WIRE"
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":
437 EnableThisBrush(bpy
.context
, "True")
438 actObj
.display_type
= "WIRE"
441 if event
.type in {"RIGHTMOUSE", "ESC"}:
443 EnableThisBrush(bpy
.context
, "True")
444 actObj
.display_type
= "WIRE"
447 return {"RUNNING_MODAL"}
449 def invoke(self
, context
, event
):
451 context
.window_manager
.modal_handler_add(self
)
452 return {"RUNNING_MODAL"}
454 self
.report({"WARNING"}, "No active object, could not finish")
458 # ------------------- Bool Tool OPERATOR CLASSES --------------------------------------------------------
462 # --------------------------------------------------------------------------------------
467 def execute(self
, context
):
468 Operation(context
, self
.mode
)
471 def invoke(self
, context
, event
):
472 if len(context
.selected_objects
) < 2:
473 self
.report({"ERROR"}, "At least two objects must be selected")
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"}
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"}
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"}
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"}
515 # Auto Boolean operators
516 # --------------------------------------------------------------------------------------
521 def objects_prepare(self
):
522 for ob
in bpy
.context
.selected_objects
:
523 if ob
.type != "MESH":
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")
548 self
.mesh_selection(ob
, "SELECT")
549 self
.boolean_mod(obj
, ob
, self
.mode
)
553 def boolean_mod(self
, obj
, ob
, mode
, ob_delete
=True):
554 md
= obj
.modifiers
.new("Auto Boolean", "BOOLEAN")
555 md
.show_viewport
= False
559 override
= {"object": obj
}
560 bpy
.ops
.object.modifier_apply(override
, modifier
=md
.name
)
563 bpy
.data
.objects
.remove(ob
)
565 def execute(self
, context
):
566 self
.objects_prepare()
567 self
.boolean_operation()
570 def invoke(self
, context
, event
):
571 if len(context
.selected_objects
) < 2:
572 self
.report({"ERROR"}, "At least two objects must be selected")
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"}
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"}
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"}
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
)
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
642 # Utils Class ---------------------------------------------------------------
644 # Find the Brush Selected in Three View
645 class BTool_FindBrush(Operator
):
646 bl_idname
= "btool.find_brush"
648 bl_description
= "Find the selected brush"
650 obj
: StringProperty("")
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)
666 # Move The Modifier in The Stack Up or Down
667 class BTool_MoveStack(Operator
):
668 bl_idname
= "btool.move_stack"
670 bl_description
= "Move this Brush Up/Down in the Stack"
672 modif
: StringProperty("")
673 direction
: StringProperty("")
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
)
687 # Enable or Disable a Brush in the Three View
688 class BTool_EnableBrush(Operator
):
689 bl_idname
= "btool.enable_brush"
691 bl_description
= "Removes all BoolTool config assigned to it"
693 thisObj
: StringProperty("")
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
)
705 # Enable or Disable a Brush Directly
706 class BTool_EnableThisBrush(Operator
):
707 bl_idname
= "btool.enable_this_brush"
709 bl_description
= "Toggles this brush"
712 def poll(cls
, context
):
713 return context
.active_object
is not None
715 def execute(self
, context
):
716 EnableThisBrush(context
, "None")
720 # Enable or Disable a Brush Directly
721 class BTool_EnableFTransform(Operator
):
722 bl_idname
= "btool.enable_ftransf"
724 bl_description
= "Use Fast Transformations to improve speed"
727 def poll(cls
, context
):
728 return context
.active_object
is not None
730 def execute(self
, context
):
731 EnableFTransf(context
)
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("")
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
)
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"}
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
)
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"}
781 def poll(cls
, context
):
783 if isBrush(context
.active_object
):
788 def execute(self
, context
):
789 ApplyThisBrush(context
, bpy
.context
.active_object
)
794 # Apply This Brush To Mesh
797 # ------------------- MENU CLASSES ------------------------------
800 class VIEW3D_MT_booltool_menu(Menu
):
801 bl_label
= "Bool Tool"
802 bl_idname
= "VIEW3D_MT_booltool_menu"
804 def draw(self
, context
):
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")
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
):
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")
828 if isBrush(context
.active_object
):
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")
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'}
852 def poll(cls
, context
):
853 return context
.active_object
is not None
855 def draw(self
, context
):
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
875 # col = main.column(align=True)
876 # col.label(text="Draw:", icon="MESH_CUBE")
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"
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
):
899 actObj
= context
.active_object
901 row
= layout
.row(align
=True)
905 row
.label(text
="CANVAS", icon
="MESH_GRID")
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")
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":
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")
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")
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"
970 def poll(cls
, context
):
971 actObj
= bpy
.context
.active_object
978 def draw(self
, context
):
980 actObj
= bpy
.context
.active_object
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"
1013 row
.label(text
=mod
.name
)
1015 Up
= row
.operator("btool.move_stack", icon
="TRIA_UP", emboss
=False)
1019 Dw
= row
.operator("btool.move_stack", icon
="TRIA_DOWN", emboss
=False)
1021 Dw
.direction
= "DOWN"
1024 # ------------------ BOOL TOOL ADD-ON PREFERENCES ----------------------------
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
:
1054 # Define Panel classes for updating
1056 VIEW3D_PT_booltool_tools
,
1057 VIEW3D_PT_booltool_config
,
1058 VIEW3D_PT_booltool_bviewer
,
1062 def update_panels(self
, context
):
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
[
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
):
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(
1100 description
="Set sidebar tab name",
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()
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:
1128 col
.label(text
=key_name
)
1130 row
= col
.row(align
=True)
1132 row
.box().label(text
=key_name
)
1133 row
.box().label(text
=key_comb
)
1136 # ------------------- Class List ------------------------------------------------
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
,
1152 # TODO Draw Poly Brush
1153 # BTool_DrawPolyBrush,
1155 BTool_AllBrushToMesh
,
1160 BTool_EnableThisBrush
,
1161 BTool_EnableFTransform
,
1162 BTool_FastTransform
,
1166 # ------------------- REGISTER ------------------------------------------------
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
1195 for km
, kmi
in addon_keymapsFastT
:
1196 km
.keymap_items
.remove(kmi
)
1198 addon_keymapsFastT
.clear()
1203 bpy
.utils
.register_class(cls
)
1204 update_panels(None, bpy
.context
)
1207 bpy
.types
.Scene
.BoolHide
= BoolProperty(
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
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
))
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
,
1243 addon_keymaps
.append((km
, kmi
))
1246 kmi
= km
.keymap_items
.new(
1247 OBJECT_OT_BoolTool_Auto_Union
.bl_idname
,
1253 addon_keymaps
.append((km
, kmi
))
1254 kmi
= km
.keymap_items
.new(
1255 OBJECT_OT_BoolTool_Auto_Difference
.bl_idname
,
1261 addon_keymaps
.append((km
, kmi
))
1262 kmi
= km
.keymap_items
.new(
1263 OBJECT_OT_BoolTool_Auto_Intersect
.bl_idname
,
1269 addon_keymaps
.append((km
, kmi
))
1270 kmi
= km
.keymap_items
.new(
1271 OBJECT_OT_BoolTool_Auto_Slice
.bl_idname
,
1277 addon_keymaps
.append((km
, kmi
))
1282 # remove keymaps when add-on is deactivated
1283 wm
= bpy
.context
.window_manager
1284 kc
= wm
.keyconfigs
.addon
1286 for km
, kmi
in addon_keymaps
:
1287 km
.keymap_items
.remove(kmi
)
1289 addon_keymaps
.clear()
1291 bpy
.types
.VIEW3D_MT_object
.remove(VIEW3D_BoolTool_Menu
)
1292 del bpy
.types
.Scene
.BoolHide
1295 bpy
.utils
.unregister_class(cls
)
1298 if __name__
== "__main__":