Function to select bones in pose
This commit is contained in:
433
common.py
433
common.py
@ -1,212 +1,223 @@
|
|||||||
import bpy
|
import bpy
|
||||||
import mathutils
|
import mathutils
|
||||||
|
|
||||||
|
|
||||||
def getArmatureData(context):
|
def getArmatureData(context):
|
||||||
try:
|
try:
|
||||||
arm_object = context.active_object
|
arm_object = context.active_object
|
||||||
if arm_object and arm_object.type == "ARMATURE":
|
if arm_object and arm_object.type == "ARMATURE":
|
||||||
return arm_object, getattr(arm_object, "pose_library", None)
|
return arm_object, getattr(arm_object, "pose_library", None)
|
||||||
elif arm_object and arm_object.parent and arm_object.parent.type == "ARMATURE":
|
elif arm_object and arm_object.parent and arm_object.parent.type == "ARMATURE":
|
||||||
return arm_object.parent, getattr(arm_object.parent, "pose_library", None)
|
return arm_object.parent, getattr(arm_object.parent, "pose_library", None)
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
except:
|
except:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
def getArmatureAction(context):
|
def getArmatureAction(context):
|
||||||
try:
|
try:
|
||||||
arm_object, pose_library = getArmatureData(context)
|
arm_object, pose_library = getArmatureData(context)
|
||||||
if getattr(arm_object.animation_data.action, "pose_markers", None):
|
if getattr(arm_object.animation_data.action, "pose_markers", None):
|
||||||
return getattr(arm_object.animation_data, "action", None)
|
return getattr(arm_object.animation_data, "action", None)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def searchPoseMarker(context, posename, type):
|
def searchPoseMarker(context, posename, type):
|
||||||
try:
|
try:
|
||||||
arm_object, pose_library = getArmatureData(context)
|
arm_object, pose_library = getArmatureData(context)
|
||||||
if type == "marker":
|
if type == "marker":
|
||||||
return pose_library.pose_markers.get(posename, None)
|
return pose_library.pose_markers.get(posename, None)
|
||||||
if type == "frame":
|
if type == "frame":
|
||||||
return pose_library.pose_markers.get(posename, None).frame
|
return pose_library.pose_markers.get(posename, None).frame
|
||||||
if type == "index":
|
if type == "index":
|
||||||
return pose_library.pose_markers.find(posename)
|
return pose_library.pose_markers.find(posename)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def findFcurve(context, bone_name, transform, index_int):
|
def selectBonesinPose(context, posename, active_marker):
|
||||||
arm_object, pose_library = getArmatureData(context)
|
try:
|
||||||
pose_markers = pose_library.pose_markers
|
arm_object, pose_library = getArmatureData(context)
|
||||||
active_frame = pose_markers.active.frame
|
for bone in arm_object.pose.bones:
|
||||||
|
bone.bone.select = False
|
||||||
fcurve_object = pose_library.fcurves.find(
|
if findKeyframe(context, bone, active_marker.frame):
|
||||||
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int)
|
bone.bone.select = True
|
||||||
if hasattr(fcurve_object, 'evaluate'):
|
except:
|
||||||
return fcurve_object.evaluate(active_frame)
|
pass
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
def findFcurve(context, bone_name, transform, index_int):
|
||||||
|
arm_object, pose_library = getArmatureData(context)
|
||||||
def createFcurve(context, bone_name, transform, index_int):
|
pose_markers = pose_library.pose_markers
|
||||||
arm_object, pose_library = getArmatureData(context)
|
active_frame = pose_markers.active.frame
|
||||||
pose_markers = pose_library.pose_markers
|
|
||||||
|
fcurve_object = pose_library.fcurves.find(
|
||||||
try:
|
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int)
|
||||||
pose_library.fcurves.new(
|
if hasattr(fcurve_object, 'evaluate'):
|
||||||
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int, action_group=bone_name)
|
return fcurve_object.evaluate(active_frame)
|
||||||
return
|
else:
|
||||||
except:
|
return None
|
||||||
pass
|
|
||||||
|
|
||||||
|
def createFcurve(context, bone_name, transform, index_int):
|
||||||
def findKeyframe(context, bone, active_frame):
|
arm_object, pose_library = getArmatureData(context)
|
||||||
arm_object, pose_library = getArmatureData(context)
|
pose_markers = pose_library.pose_markers
|
||||||
|
|
||||||
for fcu in pose_library.fcurves:
|
try:
|
||||||
if fcu.data_path.startswith('pose.bones["'+bone.name+'"]'):
|
pose_library.fcurves.new(
|
||||||
for kp in fcu.keyframe_points:
|
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int, action_group=bone_name)
|
||||||
if kp.co.x == active_frame:
|
return
|
||||||
return fcu.data_path
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
def createKeyframe(context, bone_name, transform, index_int, new_marker, loc):
|
|
||||||
arm_object, pose_library = getArmatureData(context)
|
def findKeyframe(context, bone, active_frame):
|
||||||
pose_markers = pose_library.pose_markers
|
arm_object, pose_library = getArmatureData(context)
|
||||||
|
|
||||||
try:
|
for fcu in pose_library.fcurves:
|
||||||
pose_library.fcurves.find(
|
if fcu.data_path.startswith('pose.bones["'+bone.name+'"]'):
|
||||||
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int).keyframe_points.insert(new_marker, loc)
|
for kp in fcu.keyframe_points:
|
||||||
return
|
if kp.co.x == active_frame:
|
||||||
except:
|
return fcu.data_path
|
||||||
pass
|
|
||||||
|
|
||||||
|
def createKeyframe(context, bone_name, transform, index_int, new_marker, loc):
|
||||||
def setKeyframesFromBones(context, arm_object, new_marker):
|
arm_object, pose_library = getArmatureData(context)
|
||||||
none_selected = True
|
pose_markers = pose_library.pose_markers
|
||||||
for bone in arm_object.pose.bones:
|
|
||||||
if bone.bone.select:
|
try:
|
||||||
none_selected = False
|
pose_library.fcurves.find(
|
||||||
|
'pose.bones["'+bone_name+'"].'+transform+'', index=index_int).keyframe_points.insert(new_marker, loc)
|
||||||
for bone in arm_object.pose.bones:
|
return
|
||||||
if bone.bone.select or none_selected == True:
|
except:
|
||||||
bone_name = bone.name
|
pass
|
||||||
|
|
||||||
if bone.rotation_mode == "XYZ":
|
|
||||||
rot_mode = "rotation_euler"
|
def setKeyframesFromBones(context, arm_object, new_marker):
|
||||||
elif bone.rotation_mode == "YZX":
|
none_selected = True
|
||||||
rot_mode = "rotation_euler"
|
for bone in arm_object.pose.bones:
|
||||||
elif bone.rotation_mode == "ZXY":
|
if bone.bone.select:
|
||||||
rot_mode = "rotation_euler"
|
none_selected = False
|
||||||
elif bone.rotation_mode == "QUATERNION":
|
|
||||||
rot_mode = "rotation_quaternion"
|
for bone in arm_object.pose.bones:
|
||||||
else:
|
if bone.bone.select or none_selected == True:
|
||||||
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
|
bone_name = bone.name
|
||||||
rot_mode = None
|
|
||||||
|
if bone.rotation_mode == "XYZ":
|
||||||
loc_x = bone.location[0]
|
rot_mode = "rotation_euler"
|
||||||
loc_y = bone.location[1]
|
elif bone.rotation_mode == "YZX":
|
||||||
loc_z = bone.location[2]
|
rot_mode = "rotation_euler"
|
||||||
createFcurve(context, bone_name, "location", 0)
|
elif bone.rotation_mode == "ZXY":
|
||||||
createFcurve(context, bone_name, "location", 1)
|
rot_mode = "rotation_euler"
|
||||||
createFcurve(context, bone_name, "location", 2)
|
elif bone.rotation_mode == "QUATERNION":
|
||||||
createKeyframe(context, bone_name, "location", 0, new_marker, loc_x)
|
rot_mode = "rotation_quaternion"
|
||||||
createKeyframe(context, bone_name, "location", 1, new_marker, loc_y)
|
else:
|
||||||
createKeyframe(context, bone_name, "location", 2, new_marker, loc_z)
|
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
|
||||||
if rot_mode == "rotation_quaternion":
|
rot_mode = None
|
||||||
rot_w = bone.rotation_quaternion[0]
|
|
||||||
rot_x = bone.rotation_quaternion[1]
|
loc_x = bone.location[0]
|
||||||
rot_y = bone.rotation_quaternion[2]
|
loc_y = bone.location[1]
|
||||||
rot_z = bone.rotation_quaternion[3]
|
loc_z = bone.location[2]
|
||||||
createFcurve(context, bone_name, rot_mode, 0)
|
createFcurve(context, bone_name, "location", 0)
|
||||||
createFcurve(context, bone_name, rot_mode, 1)
|
createFcurve(context, bone_name, "location", 1)
|
||||||
createFcurve(context, bone_name, rot_mode, 2)
|
createFcurve(context, bone_name, "location", 2)
|
||||||
createFcurve(context, bone_name, rot_mode, 3)
|
createKeyframe(context, bone_name, "location", 0, new_marker, loc_x)
|
||||||
createKeyframe(context, bone_name, rot_mode, 0, new_marker, rot_w)
|
createKeyframe(context, bone_name, "location", 1, new_marker, loc_y)
|
||||||
createKeyframe(context, bone_name, rot_mode, 1, new_marker, rot_x)
|
createKeyframe(context, bone_name, "location", 2, new_marker, loc_z)
|
||||||
createKeyframe(context, bone_name, rot_mode, 2, new_marker, rot_y)
|
if rot_mode == "rotation_quaternion":
|
||||||
createKeyframe(context, bone_name, rot_mode, 3, new_marker, rot_z)
|
rot_w = bone.rotation_quaternion[0]
|
||||||
elif rot_mode == "rotation_euler":
|
rot_x = bone.rotation_quaternion[1]
|
||||||
rot_x = bone.rotation_euler[0]
|
rot_y = bone.rotation_quaternion[2]
|
||||||
rot_y = bone.rotation_euler[1]
|
rot_z = bone.rotation_quaternion[3]
|
||||||
rot_z = bone.rotation_euler[2]
|
createFcurve(context, bone_name, rot_mode, 0)
|
||||||
createFcurve(context, bone_name, rot_mode, 0)
|
createFcurve(context, bone_name, rot_mode, 1)
|
||||||
createFcurve(context, bone_name, rot_mode, 1)
|
createFcurve(context, bone_name, rot_mode, 2)
|
||||||
createFcurve(context, bone_name, rot_mode, 2)
|
createFcurve(context, bone_name, rot_mode, 3)
|
||||||
createKeyframe(context, bone_name, rot_mode, 0, new_marker, rot_x)
|
createKeyframe(context, bone_name, rot_mode, 0, new_marker, rot_w)
|
||||||
createKeyframe(context, bone_name, rot_mode, 1, new_marker, rot_y)
|
createKeyframe(context, bone_name, rot_mode, 1, new_marker, rot_x)
|
||||||
createKeyframe(context, bone_name, rot_mode, 2, new_marker, rot_z)
|
createKeyframe(context, bone_name, rot_mode, 2, new_marker, rot_y)
|
||||||
scl_x = bone.scale[0]
|
createKeyframe(context, bone_name, rot_mode, 3, new_marker, rot_z)
|
||||||
scl_y = bone.scale[1]
|
elif rot_mode == "rotation_euler":
|
||||||
scl_z = bone.scale[2]
|
rot_x = bone.rotation_euler[0]
|
||||||
createFcurve(context, bone_name, "scale", 0)
|
rot_y = bone.rotation_euler[1]
|
||||||
createFcurve(context, bone_name, "scale", 1)
|
rot_z = bone.rotation_euler[2]
|
||||||
createFcurve(context, bone_name, "scale", 2)
|
createFcurve(context, bone_name, rot_mode, 0)
|
||||||
createKeyframe(context, bone_name, "scale", 0, new_marker, scl_x)
|
createFcurve(context, bone_name, rot_mode, 1)
|
||||||
createKeyframe(context, bone_name, "scale", 1, new_marker, scl_y)
|
createFcurve(context, bone_name, rot_mode, 2)
|
||||||
createKeyframe(context, bone_name, "scale", 2, new_marker, scl_z)
|
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)
|
||||||
def setBonesfromKeyframes(context, arm_object, active_marker):
|
scl_x = bone.scale[0]
|
||||||
none_selected = True
|
scl_y = bone.scale[1]
|
||||||
for bone in arm_object.pose.bones:
|
scl_z = bone.scale[2]
|
||||||
if bone.bone.select:
|
createFcurve(context, bone_name, "scale", 0)
|
||||||
none_selected = False
|
createFcurve(context, bone_name, "scale", 1)
|
||||||
|
createFcurve(context, bone_name, "scale", 2)
|
||||||
for bone in arm_object.pose.bones:
|
createKeyframe(context, bone_name, "scale", 0, new_marker, scl_x)
|
||||||
if bone.bone.select or none_selected == True:
|
createKeyframe(context, bone_name, "scale", 1, new_marker, scl_y)
|
||||||
bone_name = bone.name
|
createKeyframe(context, bone_name, "scale", 2, new_marker, scl_z)
|
||||||
|
|
||||||
if findKeyframe(context, bone, active_marker.frame) is None:
|
|
||||||
continue
|
def setBonesfromKeyframes(context, arm_object, active_marker):
|
||||||
|
none_selected = True
|
||||||
if bone.rotation_mode == "XYZ":
|
for bone in arm_object.pose.bones:
|
||||||
rot_mode = "rotation_euler"
|
if bone.bone.select:
|
||||||
elif bone.rotation_mode == "YZX":
|
none_selected = False
|
||||||
rot_mode = "rotation_euler"
|
|
||||||
elif bone.rotation_mode == "ZXY":
|
for bone in arm_object.pose.bones:
|
||||||
rot_mode = "rotation_euler"
|
if bone.bone.select or none_selected == True:
|
||||||
elif bone.rotation_mode == "QUATERNION":
|
bone_name = bone.name
|
||||||
rot_mode = "rotation_quaternion"
|
|
||||||
else:
|
if findKeyframe(context, bone, active_marker.frame) is None:
|
||||||
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
|
continue
|
||||||
rot_mode = None
|
|
||||||
|
if bone.rotation_mode == "XYZ":
|
||||||
loc_x = findFcurve(context, bone_name, "location", 0) or 0.0
|
rot_mode = "rotation_euler"
|
||||||
loc_y = findFcurve(context, bone_name, "location", 1) or 0.0
|
elif bone.rotation_mode == "YZX":
|
||||||
loc_z = findFcurve(context, bone_name, "location", 2) or 0.0
|
rot_mode = "rotation_euler"
|
||||||
if rot_mode == "rotation_quaternion":
|
elif bone.rotation_mode == "ZXY":
|
||||||
rot_w = findFcurve(context, bone_name, rot_mode, 0) or 1.0
|
rot_mode = "rotation_euler"
|
||||||
rot_x = findFcurve(context, bone_name, rot_mode, 1) or 0.0
|
elif bone.rotation_mode == "QUATERNION":
|
||||||
rot_y = findFcurve(context, bone_name, rot_mode, 2) or 0.0
|
rot_mode = "rotation_quaternion"
|
||||||
rot_z = findFcurve(context, bone_name, rot_mode, 3) or 0.0
|
else:
|
||||||
elif rot_mode == "rotation_euler":
|
self.report({'WARNING'}, "DSPL: Unsupported bone: " + bone.name + ": " + bone.rotation_mode)
|
||||||
rot_x = findFcurve(context, bone_name, rot_mode, 0) or 0.0
|
rot_mode = None
|
||||||
rot_y = findFcurve(context, bone_name, rot_mode, 1) or 0.0
|
|
||||||
rot_z = findFcurve(context, bone_name, rot_mode, 2) or 0.0
|
loc_x = findFcurve(context, bone_name, "location", 0) or 0.0
|
||||||
scl_x = findFcurve(context, bone_name, "scale", 0) or 1.0
|
loc_y = findFcurve(context, bone_name, "location", 1) or 0.0
|
||||||
scl_y = findFcurve(context, bone_name, "scale", 1) or 1.0
|
loc_z = findFcurve(context, bone_name, "location", 2) or 0.0
|
||||||
scl_z = findFcurve(context, bone_name, "scale", 2) or 1.0
|
if rot_mode == "rotation_quaternion":
|
||||||
|
rot_w = findFcurve(context, bone_name, rot_mode, 0) or 1.0
|
||||||
bone.location = mathutils.Vector((loc_x, loc_y, loc_z))
|
rot_x = findFcurve(context, bone_name, rot_mode, 1) or 0.0
|
||||||
if bone.rotation_mode == "XYZ":
|
rot_y = findFcurve(context, bone_name, rot_mode, 2) or 0.0
|
||||||
bone.rotation_euler = mathutils.Euler(
|
rot_z = findFcurve(context, bone_name, rot_mode, 3) or 0.0
|
||||||
(rot_x, rot_y, rot_z))
|
elif rot_mode == "rotation_euler":
|
||||||
elif bone.rotation_mode == "YZX":
|
rot_x = findFcurve(context, bone_name, rot_mode, 0) or 0.0
|
||||||
bone.rotation_euler = mathutils.Euler(
|
rot_y = findFcurve(context, bone_name, rot_mode, 1) or 0.0
|
||||||
(rot_x, rot_y, rot_z))
|
rot_z = findFcurve(context, bone_name, rot_mode, 2) or 0.0
|
||||||
elif bone.rotation_mode == "ZXY":
|
scl_x = findFcurve(context, bone_name, "scale", 0) or 1.0
|
||||||
bone.rotation_euler = mathutils.Euler(
|
scl_y = findFcurve(context, bone_name, "scale", 1) or 1.0
|
||||||
(rot_z, rot_x, rot_y))
|
scl_z = findFcurve(context, bone_name, "scale", 2) or 1.0
|
||||||
elif bone.rotation_mode == "YXZ":
|
|
||||||
bone.rotation_euler = mathutils.Euler(
|
bone.location = mathutils.Vector((loc_x, loc_y, loc_z))
|
||||||
(rot_y, rot_x, rot_z))
|
if bone.rotation_mode == "XYZ":
|
||||||
elif bone.rotation_mode == "XZY":
|
bone.rotation_euler = mathutils.Euler(
|
||||||
bone.rotation_euler = mathutils.Euler(
|
(rot_x, rot_y, rot_z))
|
||||||
(rot_x, rot_z, rot_y))
|
elif bone.rotation_mode == "YZX":
|
||||||
elif rot_mode == "rotation_quaternion":
|
bone.rotation_euler = mathutils.Euler(
|
||||||
bone.rotation_quaternion = mathutils.Quaternion(
|
(rot_x, rot_y, rot_z))
|
||||||
(rot_w, 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))
|
bone.scale = mathutils.Vector((scl_x, scl_y, scl_z))
|
13
operators.py
13
operators.py
@ -254,7 +254,7 @@ class DSPL_OT_MovePose(bpy.types.Operator):
|
|||||||
class DSPL_OT_ApplyPose(bpy.types.Operator):
|
class DSPL_OT_ApplyPose(bpy.types.Operator):
|
||||||
bl_idname = "dspl.apply_pose"
|
bl_idname = "dspl.apply_pose"
|
||||||
bl_label = "Apply Pose"
|
bl_label = "Apply Pose"
|
||||||
bl_description = "Apply Pose (Ctrl+Click to select, Shift+Click to rename, Alt+Click to remove)"
|
bl_description = "Apply Pose (Ctrl+Click to select bones, Shift+Click to rename, Alt+Click to remove)"
|
||||||
bl_options = {'REGISTER', 'UNDO'}
|
bl_options = {'REGISTER', 'UNDO'}
|
||||||
|
|
||||||
posename: bpy.props.StringProperty()
|
posename: bpy.props.StringProperty()
|
||||||
@ -283,9 +283,16 @@ class DSPL_OT_ApplyPose(bpy.types.Operator):
|
|||||||
|
|
||||||
def invoke(self, context, event):
|
def invoke(self, context, event):
|
||||||
if event.ctrl:
|
if event.ctrl:
|
||||||
# Select
|
# Select bones
|
||||||
arm_object, pose_library = getArmatureData(context)
|
arm_object, pose_library = getArmatureData(context)
|
||||||
pose_library.pose_markers.active_index = searchPoseMarker(context, posename=self.posename, type="index")
|
active_marker = searchPoseMarker(context, posename=self.posename, type="marker")
|
||||||
|
|
||||||
|
arm_object.select = True
|
||||||
|
bpy.context.view_layer.objects.active = arm_object
|
||||||
|
bpy.ops.object.mode_set(mode='POSE')
|
||||||
|
selectBonesinPose(context, self.posename, active_marker)
|
||||||
|
|
||||||
|
self.execute(context)
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
elif event.alt:
|
elif event.alt:
|
||||||
# Remove
|
# Remove
|
||||||
|
Reference in New Issue
Block a user