Skip to content

Instantly share code, notes, and snippets.

@Dragorn421
Created February 17, 2022 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dragorn421/a04d9b0c5fc8222a7b626ecf9a1288b9 to your computer and use it in GitHub Desktop.
Save Dragorn421/a04d9b0c5fc8222a7b626ecf9a1288b9 to your computer and use it in GitHub Desktop.
import bpy
import mathutils
from math import pi
import json
anim_file = r"/home/dragorn421/Downloads/0x2330 - clink_demo_doorA_link.anim"
armature_object_name = "skeleton"
bones_names = [f"limb_{limb_index:02}" for limb_index in range(21)]
loc_scale = 1/1000
rot_fac_to_radians = pi / 0x8000
armature_object = bpy.data.objects[armature_object_name]
pose_bones = armature_object.pose.bones
pose_bones_by_index = [pose_bones[bone_name] for bone_name in bones_names]
class Anim:
def __init__(self, frames_amount):
self.frames_amount = frames_amount
self.frames = []
def push_frame(self, frame):
self.frames.append(frame)
def __str__(self):
return "Anim [\n" + "\n".join(str(frame) for frame in self.frames) + "\n]"
class Frame:
def __init__(self):
self.loc = None
self.rots = []
def set_loc(self, loc):
self.loc = loc
def push_rot(self, rot):
self.rots.append(rot)
def __str__(self):
return "Frame [" + str(self.loc) + " " + " ".join(str(rot) for rot in self.rots) + "]"
anims = {}
cur_anim = None
cur_frame = None
with open(anim_file) as f:
for line in f:
line = line.strip()
if line.startswith("#") or line == "":
continue
tokens = line.split()
if tokens[0] == "newanim":
anim_name_repr = tokens[2]
anim_frames_str = tokens[3]
anim_name = json.loads(anim_name_repr)
anim_frames = int(anim_frames_str)
cur_anim = Anim(anim_frames)
anims[anim_name] = cur_anim
elif tokens[0] == "loc":
loc = mathutils.Vector(
tuple(float(token) * loc_scale for token in tokens[1:4])
)
cur_frame = Frame()
cur_anim.push_frame(cur_frame)
cur_frame.set_loc(loc)
elif tokens[0] == "rot":
rot = mathutils.Euler(
tuple(float(token) * rot_fac_to_radians for token in tokens[1:4]),
"XYZ",
)
cur_frame.push_rot(rot)
else:
print("Skip unknown directive", tokens[0])
for anim_name, anim in anims.items():
print(anim_name)
print(anim)
print(anims.keys())
anim_name = "clink_demo_doorA_link"
anim = anims[anim_name]
blender_to_z64 = mathutils.Matrix(
[[1, 0,0],
[0, 0,1],
[0,-1,0]]
)
z64_to_blender = blender_to_z64.transposed()
for frame_index, frame in enumerate(anim.frames):
pose_bone = pose_bones_by_index[0]
pose_bone.location = frame.loc
pose_bone.keyframe_insert(data_path="location", frame=frame_index)
for limb_index, rot in enumerate(frame.rots):
pose_bone = pose_bones_by_index[limb_index]
rot_mat_z64 = (mathutils.Matrix.Identity(3)
@ mathutils.Matrix.Rotation(rot[2], 3, "Z")
@ mathutils.Matrix.Rotation(rot[1], 3, "Y")
@ mathutils.Matrix.Rotation(rot[0], 3, "X")
)
#rot_mat_z64 = rot.to_matrix()
rot_mat_blender = z64_to_blender @ rot_mat_z64 @ blender_to_z64
#rot_mat_blender = blender_to_z64 @ rot_mat_z64 @ z64_to_blender # despair
rot_mat_blender = rot_mat_z64
pose_bone.rotation_mode = "QUATERNION"
pose_bone.rotation_quaternion = rot_mat_blender.to_quaternion()
#pose_bone.rotation_quaternion = rot.to_quaternion()
pose_bone.keyframe_insert(data_path="rotation_quaternion", frame=frame_index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment