-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_skel.py
114 lines (86 loc) · 3.37 KB
/
basic_skel.py
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
from pxr import Usd, UsdGeom, UsdSkel, Gf, Vt
def create_defaults(stage):
xform = UsdGeom.Xform.Define(stage, "/World")
stage.SetDefaultPrim(xform.GetPrim())
stage.SetStartTimeCode(1.0)
stage.SetEndTimeCode(10.0)
UsdGeom.SetStageMetersPerUnit(stage, 0.01)
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.y)
def create_skel_root(stage):
skel_root = UsdSkel.Root.Define(stage, "/World/Model")
binding_api = UsdSkel.BindingAPI.Apply(skel_root.GetPrim())
return binding_api
def create_skel(stage):
skel = UsdSkel.Skeleton.Define(stage, "/World/Model/Skel")
joints = ["Shoulder", "Shoulder/Elbow", "Shoulder/Elbow/Hand"]
skel.CreateJointsAttr(joints)
skel.CreateBindTransformsAttr([
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)),
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 2, 1)),
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 4, 1))
])
skel.CreateRestTransformsAttr([
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)),
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 2, 1)),
Gf.Matrix4d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 2, 1))
])
return skel
def create_mesh(stage, skel):
mesh = UsdGeom.Mesh.Define(stage, "/World/Model/Arm")
points = [
(0.5, -0.5, 4), (-0.5, -0.5, 4), (0.5, 0.5, 4), (-0.5, 0.5, 4),
(-0.5, -0.5, 0), (0.5, -0.5, 0), (-0.5, 0.5, 0), (0.5, 0.5, 0),
(-0.5, 0.5, 2), (0.5, 0.5, 2), (0.5, -0.5, 2), (-0.5, -0.5, 2)
]
mesh.CreatePointsAttr(points)
face_vertex_indices = [
2, 3, 1, 0,
6, 7, 5, 4,
8, 9, 7, 6,
3, 2, 9, 8,
10, 11, 4, 5,
0, 1, 11, 10,
7, 9, 10, 5,
9, 2, 0, 10,
3, 8, 11, 1,
8, 6, 4, 11
]
mesh.CreateFaceVertexIndicesAttr(face_vertex_indices)
face_vertex_counts = [4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
mesh.CreateFaceVertexCountsAttr(face_vertex_counts)
binding_api = UsdSkel.BindingAPI.Apply(mesh.GetPrim())
binding_api.CreateSkeletonRel().SetTargets([skel.GetPath()])
binding_api.CreateJointIndicesAttr([
2,2,2,2, 0,0,0,0, 1,1,1,1
])
binding_api.CreateJointIndicesPrimvar(False, 1)
binding_api.CreateJointWeightsAttr([
1,1,1,1, 1,1,1,1, 1,1,1,1
])
binding_api.CreateJointWeightsPrimvar(False, 1)
identity = Gf.Matrix4d().SetIdentity()
binding_api.CreateGeomBindTransformAttr(identity)
def create_anim(stage, skel):
anim = UsdSkel.Animation.Define(stage, skel.GetPath().AppendPath("Anim"))
anim.CreateJointsAttr(["Shoulder/Elbow"])
anim.CreateTranslationsAttr([(0,0,2)])
anim.CreateScalesAttr([(1,1,1)])
rotations = [
Gf.Quatf(1, 0, 0, 0),
Gf.Quatf(0.7071, 0.7071, 0, 0)
]
times = [1, 10]
# Set the rotation timesamples on the animation.
rotations_attr = anim.GetRotationsAttr()
for i, time in enumerate(times):
rotations_attr.Set(Vt.QuatfArray([rotations[i]]), time)
return anim
if __name__ == "__main__":
stage = Usd.Stage.CreateNew("test-arm.usda")
create_defaults(stage)
skel_root_binding_api = create_skel_root(stage)
skel = create_skel(stage)
create_mesh(stage, skel)
anim = create_anim(stage, skel)
skel_root_binding_api.CreateAnimationSourceRel().SetTargets([anim.GetPath()])
stage.Save()