Skip to content

Commit

Permalink
feat: add boolean slice operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Mar 31, 2022
1 parent 5a85cd2 commit 722dd8d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
4 changes: 4 additions & 0 deletions booleans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import importlib
from . import vanilla
from . import boolean_slice


def reload():
importlib.reload(vanilla)
importlib.reload(boolean_slice)


def register():
vanilla.register()
boolean_slice.register()


def unregister():
vanilla.unregister()
boolean_slice.unregister()
55 changes: 55 additions & 0 deletions booleans/boolean_slice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import bpy


class ND_OT_bool_slice(bpy.types.Operator):
bl_idname = "nd.bool_slice"
bl_label = "Slice"
bl_description = "Perform a boolean operation on the selected objects"
bl_options = {'UNDO'}


@classmethod
def poll(cls, context):
if context.mode == 'OBJECT':
return len(context.selected_objects) == 2


def execute(self, context):
a, b = context.selected_objects
reference_obj = a if a.name != context.object.name else b

difference_obj = context.object

intersecting_obj = context.object.copy()
intersecting_obj.data = context.object.data.copy()
intersecting_obj.animation_data_clear()
context.collection.objects.link(intersecting_obj)

boolean_diff = difference_obj.modifiers.new("Difference — ND Bool", 'BOOLEAN')
boolean_diff.operation = 'DIFFERENCE'
boolean_diff.object = reference_obj
boolean_diff.solver = 'EXACT'

boolean_isect = intersecting_obj.modifiers.new("Intersection — ND Bool", 'BOOLEAN')
boolean_isect.operation = 'INTERSECT'
boolean_isect.object = reference_obj
boolean_isect.solver = 'EXACT'

reference_obj.display_type = 'WIRE'
reference_obj.hide_render = True

return {'FINISHED'}


def menu_func(self, context):
self.layout.operator(ND_OT_bool_slice.bl_idname, text=ND_OT_bool_slice.bl_label)


def register():
bpy.utils.register_class(ND_OT_bool_slice)
bpy.types.VIEW3D_MT_object.append(menu_func)


def unregister():
bpy.utils.unregister_class(ND_OT_bool_slice)
bpy.types.VIEW3D_MT_object.remove(menu_func)
6 changes: 5 additions & 1 deletion interface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
from . import utils_ui_panel
from . import main_menu
from . import utils_menu
from . import boolean_menu


def reload():
importlib.reload(main_ui_panel)
importlib.reload(utils_ui_panel)
importlib.reload(main_menu)
importlib.reload(utils_menu)
importlib.reload(boolean_menu)


def register():
main_ui_panel.register()
utils_ui_panel.register()
main_menu.register()
utils_menu.register()
boolean_menu.register()


def unregister():
main_ui_panel.unregister()
utils_ui_panel.unregister()
main_menu.unregister()
utils_menu.unregister()
utils_menu.unregister()
boolean_menu.unregister()
31 changes: 31 additions & 0 deletions interface/boolean_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import bpy
from .. import bl_info


class ND_MT_boolean_menu(bpy.types.Menu):
bl_label = "Booleans"
bl_idname = "ND_MT_boolean_menu"


def draw(self, context):
layout = self.layout
layout.operator_context = 'INVOKE_DEFAULT'
layout.operator("nd.bool_vanilla", text="Difference", icon='MOD_BOOLEAN').mode = 'DIFFERENCE'
layout.operator("nd.bool_vanilla", text="Union", icon='MOD_BOOLEAN').mode = 'UNION'
layout.operator("nd.bool_vanilla", text="Intersect", icon='MOD_BOOLEAN').mode = 'INTERSECT'
layout.operator("nd.bool_slice", icon='MOD_BOOLEAN')


def draw_item(self, context):
layout = self.layout
layout.menu(ND_MT_boolean_menu.bl_idname)


def register():
bpy.utils.register_class(ND_MT_boolean_menu)
bpy.types.INFO_HT_header.append(draw_item)


def unregister():
bpy.utils.unregister_class(ND_MT_boolean_menu)
bpy.types.INFO_HT_header.remove(draw_item)
4 changes: 1 addition & 3 deletions interface/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def draw(self, context):
layout.operator("nd.blank_sketch", icon='GREASEPENCIL')
layout.operator("nd.vertex_bevel", icon='MOD_BEVEL')
layout.separator()
layout.operator("nd.bool_vanilla", text="Difference", icon='MOD_BOOLEAN').mode = 'DIFFERENCE'
layout.operator("nd.bool_vanilla", text="Union", icon='MOD_BOOLEAN').mode = 'UNION'
layout.operator("nd.bool_vanilla", text="Intersect", icon='MOD_BOOLEAN').mode = 'INTERSECT'
layout.menu("ND_MT_boolean_menu", icon='MOD_BOOLEAN')
layout.operator("nd.weighted_normal_bevel", icon='MOD_BEVEL')
layout.operator("nd.solidify", icon='MOD_SOLIDIFY')
layout.operator("nd.screw", icon='MOD_SCREW')
Expand Down
4 changes: 1 addition & 3 deletions interface/main_ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ def draw(self, context):
row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.bool_vanilla", text="Difference", icon='MOD_BOOLEAN').mode = 'DIFFERENCE'

row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.bool_vanilla", text="Union", icon='MOD_BOOLEAN').mode = 'UNION'

row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.bool_vanilla", text="Intersect", icon='MOD_BOOLEAN').mode = 'INTERSECT'
row.operator("nd.bool_slice", icon='MOD_BOOLEAN')

row = column.row(align=True)
row.scale_y = 1.2
Expand Down

0 comments on commit 722dd8d

Please sign in to comment.