Files
DamnSimplePoseLibrary/common.py

222 lines
8.9 KiB
Python

import bpy
import mathutils
def getArmatureObject(context):
obj = context.active_object
if obj:
if obj.type == "ARMATURE":
return obj
elif obj.parent is not None and obj.parent.type == "ARMATURE":
return obj.parent
else:
return None
def getLegacyPoseLibrary(context):
arm = getArmatureObject(context)
return getattr(arm, "pose_library", None)
def getArmatureAction(context):
arm = getArmatureObject(context)
return getattr(arm.animation_data, "action", None)
def getDsplAction(context):
arm = getArmatureObject(context)
return getattr(arm.dspl, "pose_library", None)
def getPoseLib(context):
arm = getArmatureObject(context)
pose_library_dspl = getDsplAction(context)
pose_library_action = getArmatureAction(context)
pose_library_legacy = getLegacyPoseLibrary(context)
if pose_library_dspl:
return pose_library_dspl
elif pose_library_action and not pose_library_dspl:
return pose_library_action
elif pose_library_legacy and not pose_library_dspl:
return pose_library_legacy
else:
return None
def searchPoseMarker(context, posename, type):
try:
pose_library = getPoseLib(context)
if type == "marker":
return pose_library.pose_markers.get(posename, None)
if type == "frame":
return pose_library.pose_markers.get(posename, None).frame
if type == "index":
return pose_library.pose_markers.find(posename)
except:
pass
def findFcurve(context, bone_name, transform, index_int):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
active_frame = pose_markers.active.frame
fcurve_object = action_object.fcurves.find(
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int)
if hasattr(fcurve_object, 'evaluate'):
return fcurve_object.evaluate(active_frame)
else:
return None
def createFcurve(context, bone_name, transform, index_int):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
try:
action_object.fcurves.new(
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int, action_group=bone_name)
return
except:
pass
def createKeyframe(context, bone_name, transform, index_int, new_marker, loc):
arm_object = getArmatureObject(context)
action_object = getPoseLib(context)
pose_markers = action_object.pose_markers
try:
action_object.fcurves.find(
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int).keyframe_points.insert(new_marker, loc)
return
except:
pass
def setKeyframesFromBones(context, arm_object, new_marker):
none_selected = True
for bone in arm_object.pose.bones:
if bone.bone.select:
none_selected = False
for bone in arm_object.pose.bones:
if bone.bone.select or none_selected == True:
bone_name = bone.name
if bone.rotation_mode == "XYZ":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "YZX":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "ZXY":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "QUATERNION":
rot_mode = "rotation_quaternion"
else:
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
rot_mode = None
loc_x = bone.location[0]
loc_y = bone.location[1]
loc_z = bone.location[2]
createFcurve(context, bone_name, "location", 0)
createFcurve(context, bone_name, "location", 1)
createFcurve(context, bone_name, "location", 2)
createKeyframe(context, bone_name, "location", 0, new_marker, loc_x)
createKeyframe(context, bone_name, "location", 1, new_marker, loc_y)
createKeyframe(context, bone_name, "location", 2, new_marker, loc_z)
if rot_mode == "rotation_quaternion":
rot_w = bone.rotation_quaternion[0]
rot_x = bone.rotation_quaternion[1]
rot_y = bone.rotation_quaternion[2]
rot_z = bone.rotation_quaternion[3]
createFcurve(context, bone_name, rot_mode, 0)
createFcurve(context, bone_name, rot_mode, 1)
createFcurve(context, bone_name, rot_mode, 2)
createFcurve(context, bone_name, rot_mode, 3)
createKeyframe(context, bone_name, rot_mode, 0, new_marker, rot_w)
createKeyframe(context, bone_name, rot_mode, 1, new_marker, rot_x)
createKeyframe(context, bone_name, rot_mode, 2, new_marker, rot_y)
createKeyframe(context, bone_name, rot_mode, 3, new_marker, rot_z)
elif rot_mode == "rotation_euler":
rot_x = bone.rotation_euler[0]
rot_y = bone.rotation_euler[1]
rot_z = bone.rotation_euler[2]
createFcurve(context, bone_name, rot_mode, 0)
createFcurve(context, bone_name, rot_mode, 1)
createFcurve(context, bone_name, rot_mode, 2)
createKeyframe(context, bone_name, rot_mode, 0, new_marker, rot_x)
createKeyframe(context, bone_name, rot_mode, 1, new_marker, rot_y)
createKeyframe(context, bone_name, rot_mode, 2, new_marker, rot_z)
scl_x = bone.scale[0]
scl_y = bone.scale[1]
scl_z = bone.scale[2]
createFcurve(context, bone_name, "scale", 0)
createFcurve(context, bone_name, "scale", 1)
createFcurve(context, bone_name, "scale", 2)
createKeyframe(context, bone_name, "scale", 0, new_marker, scl_x)
createKeyframe(context, bone_name, "scale", 1, new_marker, scl_y)
createKeyframe(context, bone_name, "scale", 2, new_marker, scl_z)
def setBonesfromKeyframes(context, arm_object, active_marker):
none_selected = True
for bone in arm_object.pose.bones:
if bone.bone.select:
none_selected = False
for bone in arm_object.pose.bones:
if bone.bone.select or none_selected == True:
bone_name = bone.name
if bone.rotation_mode == "XYZ":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "YZX":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "ZXY":
rot_mode = "rotation_euler"
elif bone.rotation_mode == "QUATERNION":
rot_mode = "rotation_quaternion"
else:
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
rot_mode = None
loc_x = findFcurve(context, bone_name, "location", 0) or 0.0
loc_y = findFcurve(context, bone_name, "location", 1) or 0.0
loc_z = findFcurve(context, bone_name, "location", 2) or 0.0
if rot_mode == "rotation_quaternion":
rot_w = findFcurve(context, bone_name, rot_mode, 0) or 1.0
rot_x = findFcurve(context, bone_name, rot_mode, 1) or 0.0
rot_y = findFcurve(context, bone_name, rot_mode, 2) or 0.0
rot_z = findFcurve(context, bone_name, rot_mode, 3) or 0.0
elif rot_mode == "rotation_euler":
rot_x = findFcurve(context, bone_name, rot_mode, 0) or 0.0
rot_y = findFcurve(context, bone_name, rot_mode, 1) or 0.0
rot_z = findFcurve(context, bone_name, rot_mode, 2) or 0.0
scl_x = findFcurve(context, bone_name, "scale", 0) or 1.0
scl_y = findFcurve(context, bone_name, "scale", 1) or 1.0
scl_z = findFcurve(context, bone_name, "scale", 2) or 1.0
bone.location = mathutils.Vector((loc_x, loc_y, loc_z))
if bone.rotation_mode == "XYZ":
bone.rotation_euler = mathutils.Euler(
(rot_x, rot_y, rot_z))
elif bone.rotation_mode == "YZX":
bone.rotation_euler = mathutils.Euler(
(rot_x, rot_y, rot_z))
elif bone.rotation_mode == "ZXY":
bone.rotation_euler = mathutils.Euler(
(rot_z, rot_x, rot_y))
elif bone.rotation_mode == "YXZ":
bone.rotation_euler = mathutils.Euler(
(rot_y, rot_x, rot_z))
elif bone.rotation_mode == "XZY":
bone.rotation_euler = mathutils.Euler(
(rot_x, rot_z, rot_y))
elif rot_mode == "rotation_quaternion":
bone.rotation_quaternion = mathutils.Quaternion(
(rot_w, rot_x, rot_y, rot_z))
bone.scale = mathutils.Vector((scl_x, scl_y, scl_z))