Add Lattice Magic to Addons #48

Merged
Nick Alberelli merged 36 commits from feature/lattice_magic into main 2023-05-17 20:48:52 +02:00
Showing only changes of commit 9ddcf15ba1 - Show all commits

View File

@ -35,24 +35,27 @@ def ensure_tweak_lattice_collection(scene: bpy.types.Scene) -> bpy.types.Collect
def ensure_falloff_vgroup( def ensure_falloff_vgroup(
lattice_ob: bpy.types.Object, lattice_ob: bpy.types.Object,
vg_name="Group", multiplier=1, power=1) -> bpy.types.VertexGroup: vg_name="Group", multiplier=1) -> bpy.types.VertexGroup:
lattice = lattice_ob.data lattice = lattice_ob.data
res_x, res_y, res_z = lattice.points_u, lattice.points_v, lattice.points_w res_x, res_y, res_z = lattice.points_u, lattice.points_v, lattice.points_w
vg = lattice_ob.vertex_groups.get(vg_name) vg = lattice_ob.vertex_groups.get(vg_name)
center = Vector((res_x/2, res_y/2, res_z/2))
max_res = max(res_x, res_y, res_z)
if not vg: if not vg:
vg = lattice_ob.vertex_groups.new(name=vg_name) vg = lattice_ob.vertex_groups.new(name=vg_name)
for x in range(res_x-4): for x in range(res_x-4):
for y in range(res_y-4): for y in range(res_y-4):
for z in range(res_z-4): for z in range(res_z-4):
index = get_lattice_vertex_index(lattice, (x+2, y+2, z+2)) index = get_lattice_vertex_index(lattice, (x+2, y+2, z+2))
inf = lambda x, res : 1 - abs( 1 - (x / res/2) - 0.5)
x_influence = pow( inf(x, res_x), power) coord = Vector((x+2, y+2, z+2))
y_influence = pow( inf(y, res_y), power) distance_from_center = (coord-center).length
z_influence = pow( inf(z, res_z), power) influence = 1 - distance_from_center / max_res * 2
min_influence = min(x_influence, y_influence, z_influence)
vg.add([index], min_influence * multiplier, 'REPLACE') vg.add([index], influence * multiplier, 'REPLACE')
return vg return vg
def add_radius_constraint(obj, hook, target): def add_radius_constraint(obj, hook, target):
@ -167,7 +170,6 @@ class TWEAKLAT_OT_Create(bpy.types.Operator):
# Create some custom properties # Create some custom properties
hook['Lattice'] = lattice_ob hook['Lattice'] = lattice_ob
hook['Power'] = 1.0
hook['Multiplier'] = 1.0 hook['Multiplier'] = 1.0
rna_idprop_ui_create( rna_idprop_ui_create(
@ -244,12 +246,10 @@ class TWEAKLAT_OT_Falloff(bpy.types.Operator):
if self.doing_invoke: return if self.doing_invoke: return
hook = context.object hook = context.object
lattice_ob = hook['Lattice'] lattice_ob = hook['Lattice']
ensure_falloff_vgroup(lattice_ob, 'Hook', multiplier=self.multiplier, power=self.power) ensure_falloff_vgroup(lattice_ob, 'Hook', multiplier=self.multiplier)
hook['Power'] = self.power
hook['Multiplier'] = self.multiplier hook['Multiplier'] = self.multiplier
# Actual parameters # Actual parameters
power: FloatProperty(name="Power", default=1, update=update, min=0, soft_max=2)
multiplier: FloatProperty(name="Multiplier", default=1, update=update, min=0, soft_max=2) multiplier: FloatProperty(name="Multiplier", default=1, update=update, min=0, soft_max=2)
# Storage to share info between Invoke and Update # Storage to share info between Invoke and Update
@ -264,7 +264,6 @@ class TWEAKLAT_OT_Falloff(bpy.types.Operator):
def invoke(self, context, event): def invoke(self, context, event):
hook = context.object hook = context.object
self.power = hook['Power']
self.multiplier = hook['Multiplier'] self.multiplier = hook['Multiplier']
self.hook_start_scale = hook.scale.copy() self.hook_start_scale = hook.scale.copy()
lattice_ob = hook['Lattice'] lattice_ob = hook['Lattice']
@ -278,7 +277,6 @@ class TWEAKLAT_OT_Falloff(bpy.types.Operator):
layout = self.layout layout = self.layout
layout.use_property_split = True layout.use_property_split = True
layout.use_property_decorate = False layout.use_property_decorate = False
layout.prop(self, 'power', text="Smoothness", slider=True)
layout.prop(self, 'multiplier', text="Strength", slider=True) layout.prop(self, 'multiplier', text="Strength", slider=True)
def execute(self, context): def execute(self, context):