tmp_simple_deform_helper_refactor #1

Merged
EMM merged 12 commits from tmp_simple_deform_helper_refactor into simple_deform_helper 2023-04-02 21:37:42 +02:00
9 changed files with 116 additions and 74 deletions
Showing only changes of commit df890375c9 - Show all commits

View File

@ -98,9 +98,7 @@ class Draw3D(GizmoUtils, DrawPublic, DrawText, Handler):
self.draw_3d(context)
def draw_3d(self, context):
obj = context.object # 活动物体
self.draw_scale_text(obj)
self.draw_scale_text(self.obj)
if not self.modifier_origin_angle_is_available:
self.draw_bound_box()
elif self.simple_deform_show_gizmo_poll(context):
@ -152,6 +150,8 @@ class Draw3D(GizmoUtils, DrawPublic, DrawText, Handler):
if scale_error and ('scale_text' not in self.G_GizmoData):
self.G_GizmoData['scale_text'] = bpy.types.SpaceView3D.draw_handler_add(
self.draw_str, (), 'WINDOW', 'POST_PIXEL')
elif not scale_error:
self.del_handler_text()
def draw_origin_error(self):
...

File diff suppressed because one or more lines are too long

View File

@ -10,16 +10,54 @@ from ..utils import GizmoUtils, GizmoGroupUtils
class AngleUpdate(GizmoUtils):
int_value_degrees: float
tmp_value_angle: float
def get_snap(self, delta, tweak):
is_snap = 'SNAP' in tweak
is_precise = 'PRECISE' in tweak
if is_snap and is_precise:
delta = round(delta)
elif is_snap:
delta //= 5
delta *= 5
elif is_precise:
delta /= self.mouse_dpi
delta //= 0.01
delta *= 0.01
return delta
def update_prop_value(self, event, tweak):
# radians 弧度
# degrees 角度
delta = self.get_delta(event)
value = math.degrees(self.int_value_angle - delta)
new_value = (self.get_snap(value, tweak))
old_value = math.degrees(self.target_get_value('angle'))
def v(va):
self.target_set_value('angle', math.radians(va))
self.target_set_value('angle', math.radians(new_value))
not_c_l = not event.alt and not event.ctrl
is_only_shift = event.shift and not_c_l
change_angle = self.get_delta(event)
if is_only_shift:
change_angle /= 50
new_value = self.tmp_value_angle - change_angle
old_value = self.target_get_value('angle')
snap_value = self.get_snap(new_value, tweak)
is_shift = event.type == 'LEFT_SHIFT'
if is_only_shift:
if event.value == 'PRESS':
self.init_mouse_region_x = event.mouse_region_x
self.tmp_value_angle = int(math.degrees(old_value))
v(self.tmp_value_angle)
return
value = (self.tmp_value_angle - change_angle) // 0.01 * 0.01
v(value)
return
elif not_c_l and not event.shift and is_shift and event.value == 'RELEASE':
self.init_mouse_region_x = event.mouse_region_x
new_value = self.tmp_value_angle = math.degrees(old_value)
return
v(snap_value)
def update_gizmo_matrix(self, context):
matrix = context.object.matrix_world
@ -28,11 +66,15 @@ class AngleUpdate(GizmoUtils):
self.matrix_basis.translation = matrix @ point
def update_header_text(self, context):
if self.modifier_origin_angle_is_available:
te = self.translate_text
text = te(self.modifier.deform_method.title()) + ' '
if self.modifier_is_use_angle_value:
value = round(math.degrees(self.modifier_angle), 3)
text = self.translate_header_text('Angle', value)
text += self.translate_header_text('Angle', value)
else:
text = self.translate_header_text('Coefficient', self.modifier.factor)
value = round(self.modifier.factor, 3)
text += self.translate_header_text('Coefficient', value)
context.area.header_text_set(text)
@ -49,21 +91,21 @@ class AngleGizmo(Gizmo, AngleUpdate):
'draw_type',
'mouse_dpi',
'empty_object',
'custom_shape',
'tmp_value_angle',
'int_value_degrees',
'init_mouse_region_y',
'init_mouse_region_x',
'custom_shape',
'int_value_angle',
)
int_value_angle: float
def setup(self):
self.mouse_dpi = 10
self.init_setup()
def invoke(self, context, event):
self.init_invoke(context, event)
self.int_value_angle = self.target_get_value('angle')
self.int_value_degrees = self.target_get_value('angle')
angle = math.degrees(self.int_value_degrees)
self.tmp_value_angle = angle
return {'RUNNING_MODAL'}
def modal(self, context, event, tweak):
@ -72,12 +114,12 @@ class AngleGizmo(Gizmo, AngleUpdate):
self.update_prop_value(event, tweak)
self.update_header_text(context)
self.update_multiple_modifiers_data()
return {'RUNNING_MODAL'}
return self.event_handle(event)
def exit(self, context, cancel):
context.area.header_text_set(None)
if cancel:
self.target_set_value('angle', self.int_value_angle)
self.target_set_value('angle', self.int_value_degrees)
class AngleGizmoGroup(GizmoGroup, GizmoGroupUtils):
@ -104,11 +146,11 @@ class AngleGizmoGroup(GizmoGroup, GizmoGroupUtils):
'use_draw_modal': True,
'scale_basis': 0.1,
'use_draw_value': True,
'mouse_dpi': 100,
'mouse_dpi': 5,
}),
]
self.generate_gizmo_mode(add_data)
self.generate_gizmo(add_data)
def refresh(self, context):
self.angle.target_set_prop('angle',

View File

@ -8,7 +8,7 @@ from ..utils import GizmoUtils, GizmoGroupUtils
class CustomGizmo(Gizmo, GizmoUtils):
"""绘制自定义Gizmo"""
"""Draw Custom Gizmo"""
bl_idname = '_Custom_Gizmo'
draw_type: str
custom_shape: dict
@ -37,9 +37,6 @@ class CustomGizmo(Gizmo, GizmoUtils):
class BendAxiSwitchGizmoGroup(GizmoGroup, GizmoGroupUtils):
"""绘制切换变型轴的
变换方向
"""
bl_idname = 'OBJECT_GGT_SimpleDeformGizmoGroup_display_bend_axis_switch_gizmo'
bl_label = 'SimpleDeformGizmoGroup_display_bend_axis_switch_gizmo'

View File

@ -136,9 +136,9 @@ class GizmoUpdate(GizmoProperty):
origin = self.obj_origin_property_group
mode = origin.bl_rna.properties['origin_mode'].enum_items[origin.origin_mode].name
te = self.translate_text
t = self.translate_header_text
text = self.translate_text(mode) + ' '
text = te(self.modifier.deform_method.title()) + ' ' + te(mode) + ' '
if self.is_up_limits_mode:
value = round(self.modifier_up_limits, 3)
text += t('Up limit', value)
@ -252,7 +252,7 @@ class UpDownLimitsGizmoGroup(GizmoGroup, GizmoGroupUtils):
'scale_basis': 0.1,
'use_draw_value': True, }),
]
self.generate_gizmo_mode(gizmo_data)
self.generate_gizmo(gizmo_data)
def refresh(self, context):
pro = context.object.SimpleDeformGizmo_PropertyGroup

View File

@ -48,7 +48,7 @@ class SimpleDeformGizmoAddonPreferences(AddonPreferences, GizmoUtils):
min=0.0001
)
display_bend_axis_switch_gizmo: BoolProperty(
name='Show Toggle Axis Gizmo',
name='Show Toggle Bend Axis Gizmo',
default=False,
options={'SKIP_SAVE'})

View File

@ -11,12 +11,16 @@ def origin_text(a, b):
translations_dict = {
"zh_CN": {
("上下文", "原文"): "翻译文字",
("*", "Show Toggle Axis Gizmo"): "显示切换轴向Gizmo",
("*", "Show Toggle Bend Axis Gizmo"): "显示切换弯曲轴向Gizmo",
("*", "Show Set Axis Button"): "显示设置轴向Gizmo",
("*", "Follow Upper Limit(Red)"): "跟随上限(红色)",
("*", "Follow Lower Limit(Green)"): "跟随下限(绿色)",
("*", "Lower limit(Green)"): "下限(绿色)",
("*", "UP Limits(Red)"): "上限(红色)",
("*", "Down limit"): "下限",
("*", "Up limit"): "上限",
("*", "Show Deform Wireframe"): "显示形变线框",
("*", "Minimum value between upper and lower limits"): "上限与下限之间的最小值",
("*", "Upper and lower limit tolerance"): "上下限容差",
("*", "Draw Upper and lower limit Bound Box Color"): "绘制网格上限下限边界线框的颜色",

View File

@ -8,12 +8,14 @@ from .utils import GizmoUpdate
@persistent
def remove_not_use_empty(scene, dep):
"""循环场景内的所有物体,找出没用的空物体并删掉
"""Remove unused Empty Object
"""
remove_name: str = "ViewSimpleDeformGizmo_"
context = bpy.context
if GizmoUpdate.simple_deform_modifier_is_simple(context):
GizmoUpdate.clear_cache()
gizmo = GizmoUpdate()
gizmo.clear_cache()
gizmo.fix_origin_parent_and_angle()
if gizmo.simple_deform_modifier_is_simple(context):
for obj in context.scene.objects:
is_empty = obj.type == "EMPTY"
not_parent = not obj.parent

View File

@ -15,7 +15,8 @@ class PublicData:
"""Public data class, all fixed data will be placed here
"""
G_CustomShape = {}
G_GizmoData = {'modifiers_co': {}}
G_GizmoData = {}
G_Modifiers_Data = {}
G_INDICES = (
(0, 1), (0, 2), (1, 3), (2, 3),
(4, 5), (4, 6), (5, 7), (6, 7),
@ -393,7 +394,7 @@ class PublicProperty(GizmoClassMethod):
@classmethod
def clear_data(cls):
cls.G_GizmoData.clear()
cls.G_GizmoData['modifiers_co'] = {}
cls.G_Modifiers_Data.clear()
# --------------- Cache Data ----------------------
@ -403,7 +404,7 @@ class PublicProperty(GizmoClassMethod):
@property
def modifier_bound_co(self):
return self.G_GizmoData['modifiers_co'].get(self.modifier.name, self.get_bound_co_data())
return self.G_Modifiers_Data.get(self.modifier.name, self.get_bound_co_data())
@property
def modifier_bound_box_pos(self):
@ -526,6 +527,22 @@ class PublicProperty(GizmoClassMethod):
class GizmoUpdate(PublicProperty):
def fix_origin_parent_and_angle(self):
obj = self.obj
mod = self.modifier
if not obj or not mod:
return
origin = mod.origin
if not origin:
return
if origin.parent != obj:
origin.parent = obj
origin.rotation_euler.zero()
if not self.modifier_origin_angle_is_available:
origin.location.zero()
origin.scale = 1, 1, 1
def new_origin_empty_object(self):
mod = self.modifier
@ -543,9 +560,6 @@ class GizmoUpdate(PublicProperty):
origin_object.hide_viewport = False
if origin_object == obj:
return
if origin_object.parent != obj:
origin_object.parent = obj
# add constraints
name = self.G_CON_LIMIT_NAME
if origin_object.constraints.keys().__len__() > 2:
@ -573,26 +587,24 @@ class GizmoUpdate(PublicProperty):
copy_constraints.mix_mode = 'BEFORE'
copy_constraints.target_space = 'WORLD'
copy_constraints.owner_space = 'WORLD'
origin_object.rotation_euler.zero()
origin_object.scale = 1, 1, 1
self.fix_origin_parent_and_angle()
return origin_object
def update_object_origin_matrix(self):
self.clear_cache()
origin_mode = self.origin_mode
empty_object = self.modifier.origin
if empty_object and self.modifier_is_use_origin_axis:
origin_object = self.modifier.origin
if origin_object and self.modifier_is_use_origin_axis:
if origin_mode == 'UP_LIMITS':
empty_object.matrix_world.translation = Vector(self.point_limits_up)
origin_object.matrix_world.translation = Vector(self.point_limits_up)
elif origin_mode == 'DOWN_LIMITS':
empty_object.matrix_world.translation = Vector(self.point_limits_down)
origin_object.matrix_world.translation = Vector(self.point_limits_down)
elif origin_mode == 'LIMITS_MIDDLE':
translation = (self.point_limits_up + self.point_limits_down) / 2
empty_object.matrix_world.translation = translation
origin_object.matrix_world.translation = translation
elif origin_mode == 'MIDDLE':
translation = (self.point_up + self.point_down) / 2
empty_object.matrix_world.translation = translation
origin_object.matrix_world.translation = translation
def update_multiple_modifiers_data(self):
self.clear_data()
@ -624,12 +636,12 @@ class GizmoUpdate(PublicProperty):
deform_obj.modifiers.clear()
subdivision = deform_obj.modifiers.new('1', 'SUBSURF')
subdivision.levels = 7
self.G_GizmoData['modifiers_co']['co'] = self.get_bound_co_data()
self.G_GizmoData['co'] = self.get_bound_co_data()
for mo in context.object.modifiers:
if mo.type == 'SIMPLE_DEFORM':
obj = self.get_depsgraph(deform_obj)
self.G_GizmoData['modifiers_co'][mo.name] = self.get_mesh_max_min_co(
self.G_GizmoData[mo.name] = self.get_mesh_max_min_co(
obj)
simple_deform = deform_obj.modifiers.new(
mo.name, 'SIMPLE_DEFORM')
@ -695,9 +707,8 @@ class GizmoUtils(GizmoUpdate):
matrix_basis: Matrix
draw_type: str
def generate_gizmo_mode(self, gizmo_data):
"""生成gizmo的上限下限及角度设置
def generate_gizmo(self, gizmo_data):
"""Generate Gizmo From Input Data
Args:
gizmo_data (_type_): _description_
"""
@ -746,27 +757,11 @@ class GizmoUtils(GizmoUpdate):
delta = (self.init_mouse_region_x - event.mouse_region_x) / self.mouse_dpi
return delta
def get_snap(self, delta, tweak):
# TODO ctrl SNAP
# TODO shift PRECISE
is_snap = 'SNAP' in tweak
is_precise = 'PRECISE' in tweak
if is_snap and is_precise:
delta = round(delta)
elif is_snap:
delta //= 5
delta *= 5
elif is_precise:
delta /= self.mouse_dpi
delta //= 0.01
delta *= 0.01
return delta
def update_gizmo_matrix(self):
...
def event_handle(self, event):
"""通过输入键位来更改属性"""
"""General event triggeringXXX"""
# event ctrl
data_path = ('object.SimpleDeformGizmo_PropertyGroup.origin_mode',
'object.modifiers.active.origin.SimpleDeformGizmo_PropertyGroup.origin_mode')
@ -778,9 +773,11 @@ class GizmoUtils(GizmoUpdate):
data_path=path, reverse=reverse, wrap=True)
elif event.type in ('X', 'Y', 'Z'):
self.obj.modifiers.active.deform_axis = event.type
elif event.type == 'A':
elif event.type == 'A' and 'BEND' == self.modifier.deform_method:
self.pref.display_bend_axis_switch_gizmo = True
return {'FINISHED'}
elif event.type == 'W' and event.value == 'RELEASE':
self.pref.update_deform_wireframe = self.pref.update_deform_wireframe ^ True
return {'RUNNING_MODAL'}