-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkeyframing.py
More file actions
266 lines (200 loc) · 10 KB
/
keyframing.py
File metadata and controls
266 lines (200 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#====================== BEGIN GPL LICENSE BLOCK ======================
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#======================= END GPL LICENSE BLOCK ========================
# <pep8 compliant>
import bpy
def getKeyedFrames(rig):
frames = []
if rig.animation_data:
if rig.animation_data.action:
fcus = rig.animation_data.action.fcurves
for fc in fcus:
for kp in fc.keyframe_points:
if kp.co[0] not in frames:
frames.append(kp.co[0])
frames.sort()
return frames
def insertKeyFrame(pb):
pb.keyframe_insert('location')
if pb.rotation_mode == 'QUATERNION':
pb.keyframe_insert('rotation_quaternion')
else:
pb.keyframe_insert('rotation_euler')
class OBJECT_OT_KeepPose(bpy.types.Operator):
"""Insert a keyframe for current pose"""
bl_idname = "mocanim.keep_pose"
bl_label = "Keep Pose"
def execute(self,context):
scn = context.scene
rig = scn.objects.active #bpy.data.objects[scn.MocanimTrgRig]
bpy.ops.object.mode_set(mode = 'POSE')
pbones = rig.pose.bones
# BUG After bake with visual_keying on or after Apply Visual Transform the first ik bones are rescaled
bones_to_rescale = [bone.name for bone in pbones if ('thigh_ik' in bone.name or 'upper_arm_ik' in bone.name)and(bone.bone.select)]
scales = {}
for b in bones_to_rescale:
scales[b] = pbones[b].scale.copy()
# Saving bones rotation
rotations = {}
for pb in pbones:
if pb.rotation_mode == 'QUATERNION':
rotations[pb.name] = pb.rotation_quaternion.copy()
elif pb.rotation_mode == 'AXIS_ANGLE':
rotations[pb.name] = pb.rotation_axis_angle.copy()
else:
rotations[pb.name] = pb.rotation_euler.copy()
# if scn.MocanimForceInsert:
# bpy.ops.anim.keyframe_insert_menu(type='LocRotScale')
# bpy.ops.nla.bake(frame_start=scn.frame_current, frame_end=scn.frame_current, step=1, only_selected=scn.MocanimOnlySelected, visual_keying=False, clear_constraints=not scn.MocanimKeepConstraints, clear_parents=False, use_current_action= not scn.MocanimNewAction, bake_types={'POSE'})
if not scn.MocanimOnlySelected:
bpy.ops.pose.select_all(action='SELECT')
bpy.ops.anim.keyframe_insert_menu(type='BUILTIN_KSI_VisualLocRot')
bpy.ops.anim.keyframe_insert_menu(type='Scaling')
#bpy.ops.nla.bake(frame_start=scn.frame_current, frame_end=scn.frame_current, step=1, only_selected=scn.MocanimOnlySelected, visual_keying=True, clear_constraints=not scn.MocanimKeepConstraints, clear_parents=False, use_current_action= not scn.MocanimNewAction, bake_types={'POSE'})
# bpy.ops.anim.keyframe_insert_menu(type='WholeCharacter')
# for pb in pbones:
# insertKeyFrame(pb)
# if pb.name in bones_to_rescale:
# pb.scale = scales[pb.name]
# bpy.ops.anim.keyframe_insert_menu(type='Scaling')
# insertKeyFrame(pb)
#Correct scales
# for b in bones_to_rescale:
# pbones[b].scale = scales[b]
# scn.update()
# insertKeyFrame(pbones[b])
#
# # Correct Rotations
# for pb in pbones:
# if pb.rotation_mode == 'QUATERNION':
# pb.rotation_quaternion = rotations[pb.name]
# elif pb.rotation_mode == 'AXIS_ANGLE':
# pb.rotation_axis_angle = rotations[pb.name]
# else:
# pb.rotation_euler = rotations[pb.name]
return {'FINISHED'}
class OBJECT_OT_ClearPose(bpy.types.Operator):
"""Delete current pose for selected bone"""
bl_idname = "mocanim.clear_pose"
bl_label = "Clear Pose"
def execute(self, context):
scn = context.scene
rig = bpy.data.objects[scn.MocanimTrgRig]
pbones = rig.pose.bones
fcurves = rig.animation_data.action.fcurves
for pb in pbones:
if pb.bone.select == True:
for curve in fcurves:
if pb.bone.name in curve.data_path:
if len(curve.keyframe_points) == 1:
fcurves.remove(curve)
else:
pb.keyframe_delete(curve.data_path.split('.')[-1])
return {'FINISHED'}
class OBJECT_OT_KeepAction(bpy.types.Operator):
"""Transfer bvh action to target rig """
bl_idname = "mocanim.keep_action"
bl_label = "Keep Action"
def execute(self, context):
scn = context.scene
rig = scn.objects.active #bpy.data.objects[scn.MocanimTrgRig]
bpy.ops.object.mode_set(mode = 'POSE')
pbones = rig.pose.bones
if scn.MocanimNewAction:
newaction = bpy.data.actions.new(rig.animation_data.action.name)
rig.animation_data.action = newaction
if not scn.MocanimOnlySelected:
bpy.ops.pose.select_all(action='SELECT')
for f in range(scn.MocanimStartFrame, scn.MocanimEndFrame+1, scn.MocanimFrameStep):
scn.frame_set(f)
bpy.ops.anim.keyframe_insert_menu(type='BUILTIN_KSI_VisualLocRot')
bpy.ops.anim.keyframe_insert_menu(type='Scaling')
if not scn.MocanimKeepConstraints:
bpy.ops.mocanim.delete_constraints()
return {'FINISHED'}
class OBJECT_OT_ClearAction(bpy.types.Operator):
"""Delete current action for selected bone"""
bl_idname = "mocanim.clear_action"
bl_label = "Clear Action"
def execute(self, context):
scn = context.scene
rig = bpy.data.objects[scn.MocanimTrgRig]
pbones = rig.pose.bones
fcurves=rig.animation_data.action.fcurves
for pb in pbones:
if pb.bone.select:
for fcurve in fcurves:
if pb.name in fcurve.data_path:
fcurves.remove(fcurve)
scn.frame_set(scn.frame_current-1)
scn.frame_set(scn.frame_current+1)
return {'FINISHED'}
class OBJECT_OT_GetFrameRange(bpy.types.Operator):
"""Get start and end frame range"""
bl_idname = "mocanim.get_frame_range"
bl_label = "Get Frame Range"
def execute(self,context):
scn = context.scene
scn.MocanimStartFrame = scn.frame_start
scn.MocanimEndFrame = scn.frame_end
return {'FINISHED'}
class OBJECT_OT_UpdateAction(bpy.types.Operator):
"""Update Inserted keyframes in selected range"""
bl_idname = "mocanim.update_action"
bl_label = "Update Action"
def execute(self, context):
bpy.ops.object.mode_set(mode='POSE')
scn = context.scene
rig = scn.objects.active #bpy.data.objects[scn.MocanimTrgRig]
pbones = rig.pose.bones
fcurves=rig.animation_data.action.fcurves
if not scn.MocanimOnlySelected:
bpy.ops.pose.select_all(action='DESELECT')
for pb in pbones:
for fcurve in fcurves:
if pb.name in fcurve.data_path:
pb.bone.select = True
frames = getKeyedFrames(rig)
for f in frames:
if scn.MocanimStartFrame <= f <= scn.MocanimEndFrame:
scn.frame_set(f)
bpy.ops.anim.keyframe_insert_menu(type='BUILTIN_KSI_VisualLocRot')
bpy.ops.anim.keyframe_insert_menu(type='Scaling')
if not scn.MocanimKeepConstraints:
bpy.ops.mocanim.delete_constraints()
return {'FINISHED'}
def register():
bpy.utils.register_class(OBJECT_OT_KeepPose)
bpy.utils.register_class(OBJECT_OT_KeepAction)
bpy.utils.register_class(OBJECT_OT_ClearPose)
bpy.utils.register_class(OBJECT_OT_ClearAction)
bpy.utils.register_class(OBJECT_OT_UpdateAction)
bpy.utils.register_class(OBJECT_OT_GetFrameRange)
bpy.types.Scene.MocanimOnlySelected = bpy.props.BoolProperty(name="Only Selected", description="Keep Pose on selected bones only", default=True)
bpy.types.Scene.MocanimStartFrame = bpy.props.IntProperty(name="Start Frame", description="First Frame to Bake", default=0, min= 0)
bpy.types.Scene.MocanimEndFrame = bpy.props.IntProperty(name="End Frame", description="Last Frame to Bake", default=0, min= 0)
bpy.types.Scene.MocanimFrameStep = bpy.props.IntProperty(name="Frame Step", description="Bake Frame Step", default=1, min= 1)
bpy.types.Scene.MocanimNewAction = bpy.props.BoolProperty(name="Create New Action", description="Bake bvh mocap into new Action", default=False)
bpy.types.Scene.MocanimKeepConstraints = bpy.props.BoolProperty(name="Keep Constraints", description="Keep Constraints after Bake", default=True)
#bpy.types.Scene.MocanimForceInsert = bpy.props.BoolProperty(name="Force Insert", description="Force keyframe on all channels", default=True)
def unregister():
bpy.utils.unregister_class(OBJECT_OT_KeepPose)
bpy.utils.unregister_class(OBJECT_OT_KeepAction)
bpy.utils.unregister_class(OBJECT_OT_ClearPose)
bpy.utils.unregister_class(OBJECT_OT_ClearAction)
bpy.utils.unregister_class(OBJECT_OT_ClearAction)
bpy.utils.unregister_class(OBJECT_OT_GetFrameRange)