-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv_to_animation.py
More file actions
63 lines (50 loc) · 1.86 KB
/
csv_to_animation.py
File metadata and controls
63 lines (50 loc) · 1.86 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
import os
import bpy
import pandas as pd
from mathutils import Matrix
LOC_OR_GLOB = ["Local", "Global"]
OBJECT_NAME = "model"
ARMATURE_NAME = "Arm"
CSV_FILE = ".\\data\\sample.csv"
MATRIX_LOC_OR_GLOB = LOC_OR_GLOB[1]
def csv_to_animation2(obj, arm, csv_file, loc_or_glob):
source_armature = bpy.data.objects[arm]
object = bpy.data.objects[obj]
blend_file = bpy.data.filepath
blend_path = os.path.dirname(blend_file)
df = pd.read_csv(os.path.join(blend_path, csv_file))
frames = df['Frame_Order'].values
frame_min, frame_max = frames.min(), frames.max()
bone_names = df['Anchor_Name'].values
cols = [f"{loc_or_glob}_Transform_col{col_idx}_{axis}"
for axis in "xyzw"
for col_idx in range(4)]
tuples = list(zip(*(df[col] for col in cols)))
matrixes = [Matrix(tuple(t[i:i+4] for i in range(0, len(t), 4))) for t in tuples]
matrix = list(zip(frames, bone_names, matrixes))
bpy.context.scene.frame_start = int(frame_min)
bpy.context.scene.frame_end = int(frame_max)
for obj_modi in object.modifiers:
if obj_modi.type == "ARMATURE":
obj_modi.show_viewport = False
for i in range(len(frames)):
frame, bone_name, matrix_bone = matrix[i]
bpy.context.scene.frame_set(frame)
bone = source_armature.pose.bones[bone_name]
if bone_name != "root":
bone.matrix = matrix_bone
bone.location = (0, 0, 0)
else:
bone.matrix = matrix_bone
bone.keyframe_insert(data_path="location")
bone.keyframe_insert(data_path="rotation_quaternion")
for obj_modi in object.modifiers:
if obj_modi.type == "ARMATURE":
obj_modi.show_viewport = True
#### RUN FUNCTION ####
csv_to_animation2(
OBJECT_NAME,
ARMATURE_NAME,
CSV_FILE,
MATRIX_LOC_OR_GLOB
)