def save_bone_delta(skeleton): bond = BoneDelta() for pose_bone in skeleton.pose.bones: if pose_bone.location != Vector(0, 0, 0) or pose_bone.scale != Vector(1, 1, 1) or pose_bone.rotation_quaternion != Quaternion(1, 0, 0, 0): delta = BoneDelta.Delta() delta.position = list(pose_bone.location) delta.scale = list(pose_bone.scale) delta.orientation = list(quat_xyzw(pose_bone.rotation_quaternion)) bond.deltas[FNV32.hash(pose_bone.name)] = delta return bond
def save_bone_delta(skeleton): bond = BoneDelta() for pose_bone in skeleton.pose.bones: if pose_bone.location != Vector(0, 0, 0) or pose_bone.scale != Vector( 1, 1, 1) or pose_bone.rotation_quaternion != Quaternion( 1, 0, 0, 0): delta = BoneDelta.Delta() delta.position = list(pose_bone.location) delta.scale = list(pose_bone.scale) delta.orientation = list(quat_xyzw(pose_bone.rotation_quaternion)) bond.deltas[FNV32.hash(pose_bone.name)] = delta return bond
def save_clip(clip_resource, skeleton, scale=1.0): print(skeleton.name, skeleton.type) clip = clip_resource.clip clip.tracks = [] action = skeleton.animation_data.action clip_resource.actor_name = action.s3py.actor_name clip.name = action.s3py.name clip.source_file_name = action.s3py.source_name clip.max_frame_count = int(action.frame_range[1]) start_time = time.clock() print('Saving CLIP...') track_map = {} used_bones = [] for fcurve in action.fcurves: s = str(fcurve.data_path).split('.') if s[0] != 'pose' or s[1][:5] != 'bones': continue cname = s[1][7:-2] if cname == ROOT_MORPH: continue if cname in skeleton.pose.bones: pose_bone = skeleton.pose.bones[cname] track_key = FNV32.hash(cname) clip_track_key = FNV64.hash( cname) & 0xFFFFFFFF if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH else track_key if not track_key in track_map: track = Track(clip_track_key) used_bones.append(pose_bone) clip.tracks.append(track) track_map[track_key] = track print(list(ub.name for ub in used_bones)) print('%i Frames found in %s' % (int(action.frame_range[1]), action.name)) def write_frame(current_value, track, frame_index): write = False if not any(track.frames): write = True else: last_value = track.frames[-1].data for i in range(len(current_value)): difference = math.fabs(current_value[i] - last_value[i]) if difference > 0.0001: write = True if write: f = Frame() f.frame_index = frame_index f.data = current_value track.frames.append(f) set_context('POSE', skeleton) for frame_index in range(int(action.frame_range[0]), int(action.frame_range[1])): bpy.context.scene.frame_set(frame_index) for pose_bone in used_bones: track_key = FNV32.hash(pose_bone.name) if not track_key in track_map: continue track = track_map[track_key] if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH: if not track.morphs: track.morphs = Curve.create_morph() cur_morph = [pose_bone.morph_value] write_frame(cur_morph, track.morphs, frame_index) continue matrix_parent = Matrix() if not pose_bone.parent else pose_bone.parent.matrix matrix_delta = matrix_parent.inverted() * pose_bone.matrix if not track.orientations: track.orientations = Curve.create_orientation() rotation = quat_xyzw(matrix_delta.to_quaternion()) write_frame(rotation, track.orientations, frame_index) if not track.positions: track.positions = Curve.create_position() translation = matrix_delta.to_translation() # if pose_bone.name == ROOT_BIND: # translation[1] *= scale write_frame(translation, track.positions, frame_index) print('Finished in %.4f sec.' % (time.clock() - start_time))
def save_clip(clip_resource, skeleton, scale=1.0): print(skeleton.name, skeleton.type) clip = clip_resource.clip clip.tracks = [] action = skeleton.animation_data.action clip_resource.actor_name = action.s3py.actor_name clip.name = action.s3py.name clip.source_file_name = action.s3py.source_name clip.max_frame_count = int(action.frame_range[1]) start_time = time.clock() print('Saving CLIP...') track_map = {} used_bones = [] for fcurve in action.fcurves: s = str(fcurve.data_path).split('.') if s[0] != 'pose' or s[1][:5] != 'bones': continue cname = s[1][7:-2] if cname == ROOT_MORPH: continue if cname in skeleton.pose.bones: pose_bone = skeleton.pose.bones[cname] track_key = FNV32.hash(cname) clip_track_key = FNV64.hash( cname ) & 0xFFFFFFFF if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH else track_key if not track_key in track_map: track = Track(clip_track_key) used_bones.append(pose_bone) clip.tracks.append(track) track_map[track_key] = track print(list(ub.name for ub in used_bones)) print('%i Frames found in %s' % (int(action.frame_range[1]), action.name)) def write_frame(current_value, track, frame_index): write = False if not any(track.frames): write = True else: last_value = track.frames[-1].data for i in range(len(current_value)): difference = math.fabs(current_value[i] - last_value[i]) if difference > 0.0001: write = True if write: f = Frame() f.frame_index = frame_index f.data = current_value track.frames.append(f) set_context('POSE', skeleton) for frame_index in range(int(action.frame_range[0]), int(action.frame_range[1])): bpy.context.scene.frame_set(frame_index) for pose_bone in used_bones: track_key = FNV32.hash(pose_bone.name) if not track_key in track_map: continue track = track_map[track_key] if pose_bone.parent and pose_bone.parent.name == ROOT_MORPH: if not track.morphs: track.morphs = Curve.create_morph() cur_morph = [pose_bone.morph_value] write_frame(cur_morph, track.morphs, frame_index) continue matrix_parent = Matrix( ) if not pose_bone.parent else pose_bone.parent.matrix matrix_delta = matrix_parent.inverted() * pose_bone.matrix if not track.orientations: track.orientations = Curve.create_orientation() rotation = quat_xyzw(matrix_delta.to_quaternion()) write_frame(rotation, track.orientations, frame_index) if not track.positions: track.positions = Curve.create_position() translation = matrix_delta.to_translation() # if pose_bone.name == ROOT_BIND: # translation[1] *= scale write_frame(translation, track.positions, frame_index) print('Finished in %.4f sec.' % (time.clock() - start_time))