Skip to content

Commit

Permalink
feat: add smooth operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Feb 19, 2022
1 parent 00cfd4e commit 677309e
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 2 deletions.
3 changes: 2 additions & 1 deletion interface/utils_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def draw(self, context):
layout.operator("nd.set_lod_suffix", text="High LOD", icon='ANTIALIASED').suffix = 'HIGH'
layout.separator()
layout.operator("nd.set_origin", icon='TRANSFORM_ORIGINS')
layout.separator()
layout.operator("nd.smooth", icon='MOD_SMOOTH')



def draw_item(self, context):
layout = self.layout
layout.menu(ND_MT_utils_menu.bl_idname)
Expand Down
10 changes: 9 additions & 1 deletion interface/utils_ui_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ def draw(self, context):
row.operator("nd.set_lod_suffix", text="High LOD", icon='ANTIALIASED').suffix = 'HIGH'

box = layout.box()
box.label(text="Object TRS", icon='ORIENTATION_GIMBAL')
box.label(text="Object Transform", icon='ORIENTATION_GIMBAL')
column = box.column()

row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.set_origin", icon='TRANSFORM_ORIGINS')

box = layout.box()
box.label(text="Object Properties", icon='MESH_DATA')
column = box.column()

row = column.row(align=True)
row.scale_y = 1.2
row.operator("nd.smooth", icon='MOD_SMOOTH')


def register():
bpy.utils.register_class(ND_PT_utils_ui_panel)
Expand Down
4 changes: 4 additions & 0 deletions utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
from . import name_sync
from . import set_lod_suffix
from . import set_origin
from . import smooth


def reload():
importlib.reload(name_sync)
importlib.reload(set_lod_suffix)
importlib.reload(set_origin)
importlib.reload(smooth)


def register():
name_sync.register()
set_lod_suffix.register()
set_origin.register()
smooth.register()


def unregister():
name_sync.unregister()
set_lod_suffix.unregister()
set_origin.unregister()
smooth.unregister()
123 changes: 123 additions & 0 deletions utils/smooth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import bpy
import bmesh
from math import radians
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


class ND_OT_smooth(bpy.types.Operator):
bl_idname = "nd.smooth"
bl_label = "Smooth Shading"
bl_description = "Set and configure object smoothing properties"
bl_options = {'UNDO'}


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

angle_factor = 1 if self.key_shift else self.base_angle_factor

if self.key_toggle_operator_passthrough:
toggle_operator_passthrough(self)

elif self.key_toggle_pin_overlay:
toggle_pin_overlay(self)

elif self.key_cancel:
self.revert(context)

return {'CANCELLED'}

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

return {'PASS_THROUGH'}

elif self.key_step_up:
self.angle = min(180, self.angle + angle_factor)

elif self.key_step_down:
self.angle = max(0, self.angle - angle_factor)

elif self.key_confirm:
self.finish(context)

return {'FINISHED'}

elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

self.operate(context)
self.update_overlay_wrapper(context, event)

return {'RUNNING_MODAL'}


def update_overlay_wrapper(self, context, event):
update_overlay(self, context, event, x_offset=340, lines=1)


def invoke(self, context, event):
self.base_angle_factor = 15
self.angle = 30

self.add_smooth_shading(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 add_smooth_shading(self, context):
bpy.ops.object.shade_smooth()
context.object.data.use_auto_smooth = True
context.object.data.auto_smooth_angle = radians(self.angle)


def operate(self, context):
context.object.data.auto_smooth_angle = radians(self.angle)


def finish(self, context):
unregister_draw_handler()


def revert(self, context):
unregister_draw_handler()


def draw_text_callback(self):
draw_header(self)

draw_property(
self,
"Angle: {0:.0f}°".format(self.angle),
"(±{0:.0f}) | Shift + (±1)".format(self.base_angle_factor),
active=self.key_no_modifiers,
alt_mode=self.key_shift_no_modifiers)


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


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


def unregister():
bpy.utils.unregister_class(ND_OT_smooth)
bpy.types.VIEW3D_MT_object.remove(menu_func)
unregister_draw_handler(self, ND_OT_smooth.bl_label)

0 comments on commit 677309e

Please sign in to comment.