Skip to content

Commit

Permalink
feat: add bevel operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Apr 4, 2022
1 parent 8fa29fc commit 126af0d
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 0 deletions.
1 change: 1 addition & 0 deletions interface/main_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def draw(self, context):
layout.operator("nd.vertex_bevel", icon='MOD_BEVEL')
layout.separator()
layout.menu("ND_MT_boolean_menu", icon='MOD_BOOLEAN')
layout.operator("nd.bevel", icon='MOD_BEVEL')
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: 4 additions & 0 deletions interface/main_ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def draw(self, context):
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
row.operator("nd.bevel", icon='MOD_BEVEL')

row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.weighted_normal_bevel", icon='MOD_BEVEL')
Expand Down
4 changes: 4 additions & 0 deletions power_mods/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from . import mirror
from . import profile_extrude
from . import circular_array
from . import bevel


def reload():
Expand All @@ -16,6 +17,7 @@ def reload():
importlib.reload(mirror)
importlib.reload(profile_extrude)
importlib.reload(circular_array)
importlib.reload(bevel)


def register():
Expand All @@ -26,6 +28,7 @@ def register():
mirror.register()
profile_extrude.register()
circular_array.register()
bevel.register()


def unregister():
Expand All @@ -36,5 +39,6 @@ def unregister():
mirror.unregister()
profile_extrude.unregister()
circular_array.unregister()
bevel.unregister()


189 changes: 189 additions & 0 deletions power_mods/bevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys


mod_bevel = "Bevel — ND B"
mod_summon_list = [mod_bevel]


class ND_OT_bevel(bpy.types.Operator):
bl_idname = "nd.bevel"
bl_label = "Bevel"
bl_description = "Adds a bevel modifier to the selected object"
bl_options = {'UNDO'}


def modal(self, context, event):
capture_modifier_keys(self, event)

width_factor = (self.base_width_factor / 10.0) if event.shift else self.base_width_factor
segment_factor = 1 if self.key_shift else 2

if self.key_toggle_operator_passthrough:
toggle_operator_passthrough(self)

elif self.key_toggle_pin_overlay:
toggle_pin_overlay(self, event)

elif self.operator_passthrough:
update_overlay(self, context, event)

return {'PASS_THROUGH'}

elif self.key_cancel:
self.revert(context)

return {'CANCELLED'}

elif self.key_increase_factor:
self.base_width_factor = min(1, self.base_width_factor * 10.0)

elif self.key_decrease_factor:
self.base_width_factor = max(0.001, self.base_width_factor / 10.0)

elif self.key_step_up:
if self.key_alt:
self.segments = 2 if self.segments == 1 else self.segments + segment_factor
else:
self.width += width_factor

self.dirty = True

elif self.key_step_down:
if self.key_alt:
self.segments = max(1, self.segments - segment_factor)
else:
self.width = max(0.0001, self.width - width_factor)

self.dirty = True

elif self.key_confirm:
self.finish(context)

return {'FINISHED'}

elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if self.dirty:
self.operate(context)

update_overlay(self, context, event)

return {'RUNNING_MODAL'}


def invoke(self, context, event):
self.dirty = False
self.base_width_factor = 0.001
self.segments = 1

if len(context.selected_objects) == 1:
mods = context.active_object.modifiers
mod_names = list(map(lambda x: x.name, mods))
previous_op = all(m in mod_names for m in mod_summon_list)

if previous_op:
self.summon_old_operator(context, mods)
else:
self.prepare_new_operator(context)
else:
self.prepare_new_operator(context)

self.operate(context)

capture_modifier_keys(self)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)

context.window_manager.modal_handler_add(self)

return {'RUNNING_MODAL'}


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


def prepare_new_operator(self, context):
self.summoned = False

self.width = 0.001

self.add_bevel_modifier(context)


def summon_old_operator(self, context, mods):
self.summoned = True

self.bevel = mods[mod_bevel]

self.width_prev = self.width = self.bevel.width
self.segments_prev = self.segments = self.bevel.segments


def add_bevel_modifier(self, context):
bevel = context.object.modifiers.new(mod_bevel, 'BEVEL')
bevel.offset_type = 'WIDTH'

self.bevel = bevel


def operate(self, context):
self.bevel.width = self.width
self.bevel.segments = self.segments

self.dirty = False


def finish(self, context):
unregister_draw_handler()


def revert(self, context):
if not self.summoned:
bpy.ops.object.modifier_remove(modifier=self.bevel.name)

if self.summoned:
self.bevel.width = self.width_prev
self.bevel.segments = self.segments_prev

unregister_draw_handler()


def draw_text_callback(self):
draw_header(self)

draw_property(
self,
"Width: {0:.1f}".format(self.width * 1000),
"(±{0:.1f}) | Shift (±{1:.1f})".format(self.base_width_factor * 1000, (self.base_width_factor / 10) * 1000),
active=self.key_no_modifiers,
alt_mode=self.key_shift_no_modifiers)

draw_property(
self,
"Segments: {}".format(self.segments),
"Alt (±2) | Shift (±1)",
active=self.key_alt,
alt_mode=self.key_shift_alt)


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


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


def unregister():
bpy.utils.unregister_class(ND_OT_bevel)
bpy.types.VIEW3D_MT_object.remove(menu_func)
unregister_draw_handler()

0 comments on commit 126af0d

Please sign in to comment.