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
2 changed files with 29 additions and 47 deletions
Showing only changes of commit a671fd32e0 - Show all commits

View File

@ -28,19 +28,24 @@ bl_info = {
from . import camera_lattice from . import camera_lattice
from . import tweak_lattice from . import tweak_lattice
from . import operators from . import operators
from . import utils # Just for importlib.reload()
import importlib import importlib
modules = [ modules = [
camera_lattice camera_lattice
,tweak_lattice ,tweak_lattice
,operators ,operators
,utils
] ]
def register(): def register():
for m in modules: for m in modules:
importlib.reload(m) importlib.reload(m)
m.register() if hasattr(m, 'register'):
m.register()
def unregister(): def unregister():
for m in modules: for m in modules:
m.unregister() m.unregister()
if hasattr(m, 'unregister'):
m.unregister()

View File

@ -7,13 +7,13 @@ class LATTICE_OT_Reset(bpy.types.Operator):
bl_idname = "lattice.reset_points" bl_idname = "lattice.reset_points"
bl_label = "Reset Lattice Points" bl_label = "Reset Lattice Points"
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
factor: FloatProperty(name="Factor", min=0, max=1, default=1) factor: FloatProperty(name="Factor", min=0, max=1, default=1)
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
return len(context.selected_objects)>0 and context.mode=='EDIT_LATTICE' return len(context.selected_objects)>0 and context.mode=='EDIT_LATTICE'
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
layout.use_property_split = True layout.use_property_split = True
@ -33,13 +33,12 @@ class LATTICE_OT_Reset(bpy.types.Operator):
basis_block = key_blocks[0] basis_block = key_blocks[0]
if active_index > 0: if active_index > 0:
for i, skp in enumerate(active_block.data): for i, skp in enumerate(active_block.data):
point = ob.data.points[i] if not ob.data.points[i].select: continue
if not point.select: continue skp.co = skp.co.lerp(basis_block.data[i].co, self.factor)
mix = skp.co.lerp(basis_block.data[i].co, self.factor)
skp.co = mix
continue continue
else: else:
for i, skp in enumerate(active_block.data): for i, skp in enumerate(active_block.data):
if not ob.data.points[i].select: continue
base = get_lattice_point_original_position(ob.data, i) base = get_lattice_point_original_position(ob.data, i)
# Resetting the Basis shape # Resetting the Basis shape
mix = basis_block.data[i].co.lerp(base, self.factor) mix = basis_block.data[i].co.lerp(base, self.factor)
@ -49,54 +48,30 @@ class LATTICE_OT_Reset(bpy.types.Operator):
# Otherwise, reset the actual points. # Otherwise, reset the actual points.
for i in range(len(ob.data.points)): for i in range(len(ob.data.points)):
point = ob.data.points[i] point = ob.data.points[i]
if not point.select: if not point.select: continue
continue
base = get_lattice_point_original_position(ob.data, i) base = get_lattice_point_original_position(ob.data, i)
mix = point.co_deform.lerp(base, self.factor) mix = point.co_deform.lerp(base, self.factor)
point.co_deform = base point.co_deform = mix
bpy.ops.object.mode_set(mode='EDIT') bpy.ops.object.mode_set(mode='EDIT')
return {'FINISHED'} return {'FINISHED'}
class ShapeKey_OT_Reset(bpy.types.Operator):
"""Reset shape of the active shape key of the active object"""
bl_idname = "object.reset_shape_key"
bl_label = "Reset Shape Key"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
ob = context.object
if not ob:
return False
if not ob.data.shape_keys:
return False
if len(ob.data.shape_keys.key_blocks)<2:
return False
if ob.active_shape_key_index==0:
return False
return True
def execute(self, context):
ob = context.object
active_index = ob.active_shape_key_index
key_blocks = ob.data.shape_keys.key_blocks
active_block = key_blocks[active_index]
basis_block = key_blocks[0]
for i, skp in enumerate(active_block.data):
skp.co = basis_block.data[i].co
return { 'FINISHED' }
def draw_shape_key_reset(self, context): def draw_shape_key_reset(self, context):
self.layout.operator(ShapeKey_OT_Reset.bl_idname, text="Reset Shape Key", icon='FILE_REFRESH') layout = self.layout
ob = context.object
if ob.type=='MESH':
op = layout.operator('mesh.blend_from_shape', text='Reset Shape Key', icon='FILE_REFRESH')
op.shape = ob.data.shape_keys.key_blocks[0].name
op.blend = 1
op.add = False
else:
layout.operator(LATTICE_OT_Reset.bl_idname, text="Reset Shape Key", icon='FILE_REFRESH')
def draw_lattice_reset(self, context):
self.layout.operator(LATTICE_OT_Reset.bl_idname, text="Reset Point Positions", icon='FILE_REFRESH')
classes = [ classes = [
ShapeKey_OT_Reset LATTICE_OT_Reset
,LATTICE_OT_Reset
] ]
def register(): def register():
@ -104,10 +79,12 @@ def register():
for c in classes: for c in classes:
register_class(c) register_class(c)
bpy.types.MESH_MT_shape_key_context_menu.append(draw_shape_key_reset) bpy.types.MESH_MT_shape_key_context_menu.append(draw_shape_key_reset)
bpy.types.VIEW3D_MT_edit_lattice.append(draw_lattice_reset)
def unregister(): def unregister():
from bpy.utils import unregister_class from bpy.utils import unregister_class
for c in reversed(classes): for c in reversed(classes):
unregister_class(c) unregister_class(c)
bpy.types.MESH_MT_shape_key_context_menu.remove(draw_shape_key_reset) bpy.types.MESH_MT_shape_key_context_menu.remove(draw_shape_key_reset)
bpy.types.VIEW3D_MT_edit_lattice.remove(draw_lattice_reset)