Unify calls to armature and pose library

This commit is contained in:
2024-12-30 15:12:04 -06:00
parent 8f223527a2
commit 0aa1014f55
3 changed files with 96 additions and 155 deletions

View File

@ -13,7 +13,7 @@ class DSPL_OT_CreatePoseLibrary(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
arm_object = getArmatureObject(context)
arm_object, pose_library = getArmatureData(context)
arm_object.pose_library = bpy.data.actions.new(
name=arm_object.name + "_PoseLib")
arm_object.pose_library.use_fake_user = True
@ -21,7 +21,7 @@ class DSPL_OT_CreatePoseLibrary(bpy.types.Operator):
return {'FINISHED'}
# Operator to convert a pose library to dspl property
# Operator to convert an action to pose library
class DSPL_OT_ConvertPoseLibrary(bpy.types.Operator):
@ -31,8 +31,9 @@ class DSPL_OT_ConvertPoseLibrary(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
arm_object = getArmatureObject(context)
arm_object.pose_library = arm_object.animation_data.action
arm_object, pose_library = getArmatureData(context)
if pose_library is None:
arm_object.pose_library = arm_object.animation_data.action
arm_object.animation_data.action = None
return {'FINISHED'}
@ -51,11 +52,9 @@ class DSPL_OT_AddPose(bpy.types.Operator):
replace: bpy.props.BoolProperty(name="Replace", description="Replace existing pose", default=False, options={'SKIP_SAVE'})
def execute(self, context):
arm_object, pose_library = getArmatureData(context)
if self.replace == False:
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
pose_markers = pose_library.pose_markers
new_name = self.posename
counter = 1
@ -83,15 +82,13 @@ class DSPL_OT_AddPose(bpy.types.Operator):
setKeyframesFromBones(context, arm_object, new_marker)
action_object.pose_markers.active = pose_markers[pose_name]
pose_library.pose_markers.active = pose_markers[pose_name]
bpy.context.area.tag_redraw()
self.report({'INFO'}, "DSPL: Added " + pose_markers[new_name].name + " to frame " + str(pose_markers[new_name].frame))
else:
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
pose_markers = pose_library.pose_markers
active_marker = pose_markers.active
new_name = self.posename
counter = 1
@ -127,18 +124,17 @@ class DSPL_OT_RemovePose(bpy.types.Operator):
posename: bpy.props.StringProperty()
def execute(self, context):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
arm_object, pose_library = getArmatureData(context)
pose_markers = pose_library.pose_markers
if self.posename:
action_object.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
active_index = action_object.pose_markers.active_index
pose_library.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
active_index = pose_library.pose_markers.active_index
else:
active_index = pose_markers.active_index
active_marker = pose_markers.active
active_frame = active_marker.frame
fcurves = action_object.fcurves
fcurves = pose_library.fcurves
for fcu in fcurves:
for kf in fcu.keyframe_points.values():
if kf.co.x == active_frame:
@ -156,8 +152,8 @@ class DSPL_OT_RemovePose(bpy.types.Operator):
pose_markers.remove(marker=active_marker)
action_object.pose_markers.active = next_marker
action_object.pose_markers.active_index = next_index
pose_library.pose_markers.active = next_marker
pose_library.pose_markers.active_index = next_index
self.report({'INFO'}, "DSPL: Removed " + self.posename)
@ -177,10 +173,8 @@ class DSPL_OT_RenamePose(bpy.types.Operator):
pose_new_name: bpy.props.StringProperty()
def execute(self, context):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
arm_object, pose_library = getArmatureData(context)
pose_markers = pose_library.pose_markers
active_marker = pose_markers.active
if self.posename:
@ -217,10 +211,8 @@ class DSPL_OT_MovePose(bpy.types.Operator):
posename: bpy.props.StringProperty(name="Pose Name", default="", options={'SKIP_SAVE'})
def execute(self, context):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_lib = getPoseLib(context)
pose_markers = action_object.pose_markers
arm_object, pose_library = getArmatureData(context)
pose_markers = pose_library.pose_markers
if self.posename:
active_index = searchPoseMarker(context, posename=self.posename, type="index")
@ -228,7 +220,6 @@ class DSPL_OT_MovePose(bpy.types.Operator):
active_frame = active_marker.frame
active_posename = active_marker.name
else:
arm_object = getArmatureObject(context)
active_marker = pose_markers.active
active_index = pose_markers.active_index
@ -240,21 +231,19 @@ class DSPL_OT_MovePose(bpy.types.Operator):
swap_index = active_index + move_dir
if swap_index < 0:
# swap_index = len(pose_lib.pose_markers) - 1
return {'FINISHED'}
elif swap_index >= len(pose_lib.pose_markers):
# swap_index = 0
elif swap_index >= len(pose_library.pose_markers):
return {'FINISHED'}
swap_marker = pose_markers[swap_index]
tmp_marker_name = swap_marker.name
tmp_marker_frame = swap_marker.frame
action_object.pose_markers[swap_index].name = active_marker.name
action_object.pose_markers[swap_index].frame = active_marker.frame
action_object.pose_markers[active_index].name = tmp_marker_name
action_object.pose_markers[active_index].frame = tmp_marker_frame
action_object.pose_markers.active_index = swap_index
pose_library.pose_markers[swap_index].name = active_marker.name
pose_library.pose_markers[swap_index].frame = active_marker.frame
pose_library.pose_markers[active_index].name = tmp_marker_name
pose_library.pose_markers[active_index].frame = tmp_marker_frame
pose_library.pose_markers.active_index = swap_index
return {'FINISHED'}
@ -272,15 +261,14 @@ class DSPL_OT_ApplyPose(bpy.types.Operator):
def execute(self, context):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
arm_object, pose_library = getArmatureData(context)
pose_markers = pose_library.pose_markers
if self.posename:
active_marker = searchPoseMarker(context, posename=self.posename, type="marker")
active_frame = active_marker.frame
active_posename = active_marker.name
action_object.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
pose_library.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
else:
active_index = pose_markers.active_index
active_marker = pose_markers.active
@ -296,8 +284,8 @@ class DSPL_OT_ApplyPose(bpy.types.Operator):
def invoke(self, context, event):
if event.ctrl:
# Select
action_object = getPoseLib(context)
action_object.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
arm_object, pose_library = getArmatureData(context)
pose_library.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
return {'FINISHED'}
elif event.alt:
# Remove
@ -327,7 +315,7 @@ class DSPL_OT_BrowsePoses(bpy.types.Operator):
font_dpi = 72
blf.position(font_id, 15, 30, 0)
mod_var = self.pose_lib.pose_markers.active.name
mod_var = self.pose_library.pose_markers.active.name
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
blf.size(font_id, font_size)
blf.draw(font_id, "Previewing pose: " + mod_var)
@ -338,16 +326,16 @@ class DSPL_OT_BrowsePoses(bpy.types.Operator):
if event.value == 'PRESS':
if event.type in {'LEFT_ARROW', 'UP_ARROW'}:
if self.pose_lib.pose_markers.active_index <= 0:
self.pose_lib.pose_markers.active_index = len(self.pose_lib.pose_markers) - 1
if self.pose_library.pose_markers.active_index <= 0:
self.pose_library.pose_markers.active_index = len(self.pose_library.pose_markers) - 1
else:
self.pose_lib.pose_markers.active_index = self.pose_lib.pose_markers.active_index - 1
self.pose_library.pose_markers.active_index = self.pose_library.pose_markers.active_index - 1
bpy.ops.dspl.apply_pose()
elif event.type in {'RIGHT_ARROW', 'DOWN_ARROW'}:
if self.pose_lib.pose_markers.active_index + 1 >= len(self.pose_lib.pose_markers):
self.pose_lib.pose_markers.active_index = 0
if self.pose_library.pose_markers.active_index + 1 >= len(self.pose_library.pose_markers):
self.pose_library.pose_markers.active_index = 0
else:
self.pose_lib.pose_markers.active_index = self.pose_lib.pose_markers.active_index + 1
self.pose_library.pose_markers.active_index = self.pose_library.pose_markers.active_index + 1
bpy.ops.dspl.apply_pose()
if event.type in {'LEFTMOUSE', 'RET', 'NUMPAD_ENTER'}:
@ -356,7 +344,7 @@ class DSPL_OT_BrowsePoses(bpy.types.Operator):
elif event.type in {'RIGHTMOUSE', 'ESC'}:
self.arm_object.pose.backup_restore()
self.pose_lib.pose_markers.active_index = self.backup_index
self.pose_library.pose_markers.active_index = self.backup_index
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
return {'CANCELLED'}
@ -366,15 +354,14 @@ class DSPL_OT_BrowsePoses(bpy.types.Operator):
def invoke(self, context, event):
bpy.context.area.tag_redraw()
self.arm_object = getArmatureObject(context)
self.pose_lib = getPoseLib(context)
if self.pose_lib is None:
self.arm_object, self.pose_library = getArmatureData(context)
if self.pose_library is None:
self.report({'WARNING'}, "DSPL: Pose Library not active")
return {'CANCELLED'}
self.arm_object.pose.backup_create(self.pose_lib)
self.backup_index = self.pose_lib.pose_markers.active_index
self.arm_object.pose.backup_create(self.pose_library)
self.backup_index = self.pose_library.pose_markers.active_index
bpy.ops.dspl.apply_pose()
if context.area.type == 'VIEW_3D':
@ -396,16 +383,15 @@ class DSPL_OT_UnlinkPoseLibrary(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
arm_object = getArmatureObject(context)
pose_library = getPoseLib(context)
arm_object, pose_library = getArmatureData(context)
try:
arm_object.pose_library.use_fake_user = False
arm_object.pose_library = None
# arm_object.pose_library.use_fake_user = False
# if not arm_object.pose_library.name.startswith("del_"):
# arm_object.pose_library.name = "del_{}".format(arm_object.pose_library.name)
except:
pass
arm_object.pose_library = None
return {'FINISHED'}