new addon simple_deform_helper #104464

Closed
EMM wants to merge 29 commits from Guai_Wo_Ge_EMM/blender-addons:simple_deform_helper into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
8 changed files with 68 additions and 60 deletions
Showing only changes of commit 4ea575bcbf - Show all commits

View File

@ -108,7 +108,7 @@ class Draw3D(GizmoUtils, DrawPublic, DrawText, Handler):
if self.simple_deform_show_gizmo_poll(context):
# draw bound box
self.draw_bound_box()
# cls.draw_deform_mesh(obj, context)
self.draw_deform_mesh()
self.draw_limits_line()
self.draw_limits_bound_box()
elif self.simple_deform_show_bend_axis_witch_poll(context):
@ -138,18 +138,17 @@ class Draw3D(GizmoUtils, DrawPublic, DrawText, Handler):
self.draw_3d_shader([line_pos[0]], (), (1, 0, 0, 0.5),
shader_name='3D_UNIFORM_COLOR', draw_type='POINTS')
@classmethod
def draw_deform_mesh(cls, ob, context):
pref = cls.pref_()
handler_dit = cls.G_GizmoData
active = context.object.modifiers.active
def draw_deform_mesh(self):
ob = self.obj
handler_dit = self.G_GizmoData
active = self.modifier
# draw deform mesh
if 'draw' in handler_dit:
pos, indices, mat, mod_data, limits = handler_dit['draw']
if ([getattr(active, i) for i in cls.G_MODIFIERS_PROPERTY] == mod_data) and (
if ([getattr(active, i) for i in self.G_MODIFIERS_PROPERTY] == mod_data) and (
ob.matrix_world == mat) and limits == active.limits[:]:
cls.draw_3d_shader(
pos, indices, pref.deform_wireframe_color)
self.draw_3d_shader(
pos, indices, self.pref.deform_wireframe_color)
def draw_scale_text(self, ob):
scale_error = (ob.scale != Vector((1, 1, 1)))

View File

@ -70,8 +70,9 @@ class AngleGizmo(Gizmo, AngleUpdate):
def modal(self, context, event, tweak):
self.clear_cache()
self.update_header_text(context)
self.update_prop_value(event, tweak)
self.update_header_text(context)
self.update_deform_wireframe()
return {'RUNNING_MODAL'}
def exit(self, context, cancel):

View File

@ -234,6 +234,7 @@ class UpDownLimitsGizmo(Gizmo, GizmoUpdate):
self.set_prop_value(event)
self.update_object_origin_matrix()
self.update_deform_wireframe()
self.update_header_text(context)
return self.event_handle(event)

View File

@ -26,7 +26,7 @@ class DeformAxisOperator(Operator, GizmoUtils):
return {'RUNNING_MODAL'}
def modal(self, context, event):
from gizmo.angle_and_factor import GizmoUtils
from .gizmo.angle_and_factor import GizmoUtils
mod = context.object.modifiers.active
mod.deform_axis = self.Deform_Axis

View File

@ -33,6 +33,12 @@ class SimpleDeformHelperToolPanel(Panel, GizmoUtils):
layout.prop(ctrl_obj,
'origin_mode',
text='')
layout.prop(pref,
'update_deform_wireframe',
icon='MOD_WIREFRAME',
text='Deform Wireframe')
layout.prop(pref,
'modifiers_limits_tolerance',
text='')

View File

@ -52,6 +52,10 @@ class SimpleDeformGizmoAddonPreferences(AddonPreferences, GizmoUtils):
default=False,
options={'SKIP_SAVE'})
update_deform_wireframe: BoolProperty(
name='Update Deform Wireframe',
default=True)
def draw(self, context):
col = self.layout.column()
col.prop(self, 'deform_wireframe_color')
@ -59,6 +63,7 @@ class SimpleDeformGizmoAddonPreferences(AddonPreferences, GizmoUtils):
col.prop(self, 'limits_bound_box_color')
col.prop(self, 'modifiers_limits_tolerance')
col.prop(self, 'display_bend_axis_switch_gizmo')
col.prop(self, 'update_deform_wireframe', icon='MOD_WIREFRAME', )
def draw_header_tool_settings(self, context):
if GizmoUtils.simple_deform_public_poll(context):

View File

@ -11,7 +11,7 @@ def remove_not_use_empty(scene, dep):
"""循环场景内的所有物体,找出没用的空物体并删掉
"""
GizmoUpdate.clear_cache()
remove_name: str = "ViewSimpleDeformGizmo__Empty_"
remove_name: str = "ViewSimpleDeformGizmo_"
context = bpy.context
if GizmoUpdate.simple_deform_modifier_is_simple(context):
for obj in context.scene.objects:

View File

@ -15,7 +15,7 @@ class PublicData:
"""Public data class, all fixed data will be placed here
"""
G_CustomShape = {}
G_GizmoData = {}
G_GizmoData = {'modifiers_co': {}}
G_INDICES = (
(0, 1), (0, 2), (1, 3), (2, 3),
(4, 5), (4, 6), (5, 7), (6, 7),
@ -393,6 +393,7 @@ class PublicProperty(GizmoClassMethod):
@classmethod
def clear_data(cls):
cls.G_GizmoData.clear()
cls.G_GizmoData['modifiers_co'] = {}
# --------------- Cache Data ----------------------
@ -566,7 +567,7 @@ class GizmoUpdate(PublicProperty):
origin_object.scale = 1, 1, 1
def update_object_origin_matrix(self):
self.clear_data()
self.clear_cache()
origin_mode = self.origin_mode
empty_object = self.modifier.origin
if empty_object and self.modifier_is_use_origin_axis:
@ -582,46 +583,42 @@ class GizmoUpdate(PublicProperty):
empty_object.matrix_world.translation = translation
def update_deform_wireframe(self):
if not self.pref.update_deform_wireframe:
return
context = bpy.context
data = bpy.data
obj = self.obj
matrix = obj.matrix_world.copy() # 物体矩阵
# add simple_deform mesh
(min_x, min_y, min_z), (max_x, max_y,
max_z) = cls.get_mesh_max_min_co(obj)
vertexes = ((max_x, min_y, min_z),
(min_x, min_y, min_z),
(max_x, max_y, min_z),
(min_x, max_y, min_z),
(max_x, min_y, max_z),
(min_x, min_y, max_z),
(max_x, max_y, max_z),
(min_x, max_y, max_z))
name = cls.G_NAME
if data.objects.get(name):
data.objects.remove(data.objects.get(name))
name = self.G_NAME
if data.meshes.get(name):
data.meshes.remove(data.meshes.get(name))
mesh = data.meshes.new(name)
mesh.from_pydata(vertexes, cls.G_INDICES, [])
mesh.update()
vertices = self.matrix_calculation(self.obj_matrix_world.inverted(), self.modifier_limits_bound_box)
new_mesh = data.meshes.new(name)
new_mesh.from_pydata(vertices, self.G_INDICES, [])
new_mesh.update()
new_object = data.objects.new(name, mesh)
deform_obj = data.objects.get(name, None)
if deform_obj and deform_obj.type == 'MESH':
deform_obj.data = new_mesh
else:
if deform_obj:
data.objects.remove(deform_obj)
deform_obj = data.objects.new(name, new_mesh)
cls.link_obj_to_active_collection(new_object)
self.link_obj_to_active_collection(deform_obj)
if deform_obj == obj:
return
if deform_obj.parent != obj:
deform_obj.parent = obj
if new_object.parent != obj:
new_object.parent = obj
new_object.modifiers.clear()
subdivision = new_object.modifiers.new('1', 'SUBSURF')
deform_obj.modifiers.clear()
subdivision = deform_obj.modifiers.new('1', 'SUBSURF')
subdivision.levels = 7
cls.G_GizmoData['modifiers_co'] = {}
cls.G_GizmoData['modifiers_co']['co'] = (
min_x, min_y, min_z), (max_x, max_y, max_z)
self.G_GizmoData['modifiers_co']['co'] = self.get_bound_co_data()
for mo in context.object.modifiers:
if mo.type == 'SIMPLE_DEFORM':
simple_deform = new_object.modifiers.new(
simple_deform = deform_obj.modifiers.new(
mo.name, 'SIMPLE_DEFORM')
simple_deform.deform_method = mo.deform_method
simple_deform.deform_axis = mo.deform_axis
@ -629,26 +626,26 @@ class GizmoUpdate(PublicProperty):
simple_deform.lock_y = mo.lock_y
simple_deform.lock_z = mo.lock_z
simple_deform.origin = mo.origin
simple_deform.limits[1] = mo.limits[1]
simple_deform.limits[0] = mo.limits[0]
# simple_deform.limits[1] = mo.limits[1]
# simple_deform.limits[0] = mo.limits[0]
simple_deform.angle = mo.angle
simple_deform.show_viewport = mo.show_viewport
obj = PublicUtils.get_depsgraph(new_object)
cls.G_GizmoData['modifiers_co'][mo.name] = cls.get_mesh_max_min_co(
obj = PublicUtils.get_depsgraph(deform_obj)
self.G_GizmoData['modifiers_co'][mo.name] = self.get_mesh_max_min_co(
obj)
new_object.hide_set(True)
new_object.hide_viewport = False
new_object.hide_select = True
new_object.hide_render = True
new_object.hide_viewport = True
new_object.hide_set(True)
# deform_obj.hide_set(True)
# deform_obj.hide_viewport = False
# deform_obj.hide_select = True
# deform_obj.hide_render = True
# deform_obj.hide_viewport = True
# deform_obj.hide_set(True)
ver_len = obj.data.vertices.__len__()
edge_len = obj.data.edges.__len__()
if 'numpy_data' not in cls.G_GizmoData:
cls.G_GizmoData['numpy_data'] = {}
if 'numpy_data' not in self.G_GizmoData:
self.G_GizmoData['numpy_data'] = {}
numpy_data = cls.G_GizmoData['numpy_data']
numpy_data = self.G_GizmoData['numpy_data']
key = (ver_len, edge_len)
if key in numpy_data:
list_edges, list_vertices = numpy_data[key]
@ -669,9 +666,8 @@ class GizmoUpdate(PublicProperty):
limits = context.object.modifiers.active.limits[:]
modifiers = [getattr(context.object.modifiers.active, i)
for i in cls.G_MODIFIERS_PROPERTY]
cls.G_GizmoData['draw'] = (ver, indices, matrix, modifiers, limits)
for i in self.G_MODIFIERS_PROPERTY]
self.G_GizmoData['draw'] = (ver, indices, matrix, modifiers, limits)
class GizmoUtils(GizmoUpdate):