-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryExportForUI.py
173 lines (129 loc) · 5.74 KB
/
BinaryExportForUI.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
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
# Imports:
import pymel.core as pm
import pymel.core.datatypes as dt
import sys
import struct
# Get corect Hirarchy:
def Hirarchy(node, hirarchy):
for child in node.getChildren():
hirarchy.append(child)
if child.numChildren()>0:
Hirarchy(child, hirarchy)
# Get root in Hierchy:
def ReversedHierarchy(node):
if node.getParent():
tempPerent = node.getParent()
newTempPerent = tempPerent
if tempPerent.getParent():
ReversedHierarchy(tempPerent)
else:
newTempPerent = pm.select(newTempPerent)
return newTempPerent
else:
node = pm.select(node)
return node
# Get Oreantations and Rotations for Sorce:
def PerentRotAndOr(node, orient, jointsRoatations):
if node.getParent():
tempPerent = node.getParent()
orient.append(tempPerent.getOrientation().asMatrix())
rotation = dt.Matrix()
pm.currentTime(0.0)
rot = tempPerent.getRotation()
rotation = dt.EulerRotation(rot).asMatrix()
jointsRoatations.append(rotation)
if tempPerent.getParent():
PerentRotAndOr(tempPerent, orient, jointsRoatations)
# Calculate perent matrix:
def CalculateOriAndRot(orientation, rJoints):
calc = dt.Matrix()
for o, j in zip(orientation, rJoints):
calc *= o * j
return calc
# Hierarchy Calculations:
def Orientations(hirarchy, orent, orentInvert):
for s in hirarchy:
orent.append(s.getOrientation().asMatrix())
orentInvert.append(s.getOrientation().asMatrix().inverse())
# Create a path and name to file that is exported
def CreateFilePath():
newFile = pm.fileDialog2(fm = 0, ds = 2, ff = '*.sb')
if(newFile == None):
sys.stdout.write('Warning: A name or a path for the file should be chosen.')
return
return newFile
# Create binary animation file
def WriteToFile(newFile, hirarchy, rotationsInvert, orentInvert, perentMatrixListInvers, perentMatrixList, orent, frameStart, frameEnd, nrOfframes):
if(newFile[0] == None):
sys.stdout.write('Error: A name or a path for the file was never chosen.')
return
exportFile = open(newFile[0], 'wb')
exportFile.write(struct.pack('ii', nrOfframes, len(hirarchy)))
frameList = list(range(frameStart, (frameEnd + 1)))
for index, h in enumerate(hirarchy):
exportFile.write(struct.pack('i', len(str(h))))
exportFile.write(str(h))
for i in frameList:
fRotation = dt.Matrix()
# Get Final source rotation at this keyframe:
pm.currentTime(i)
fRotation = h.getRotation()
fRotation = dt.EulerRotation(fRotation).asMatrix()
# Isolated rotation:
k = rotationsInvert[index] * fRotation
# World space rotation:
kPrim = orentInvert[index] * perentMatrixListInvers[index] * k * perentMatrixList[index] * orent[index]
# Export every float in matrix in one chunk of data:
exportFile.write(struct.pack('ffffffffffffffff', kPrim.a00, kPrim.a01, kPrim.a02, kPrim.a03, kPrim.a10, kPrim.a11, kPrim.a12, kPrim.a13, kPrim.a20, kPrim.a21, kPrim.a22, kPrim.a23, kPrim.a30, kPrim.a31, kPrim.a32, kPrim.a33))
exportFile.close()
sys.stdout.write('Info: File successfully exported.')
# ========================================================
def HirarchyListCreator(hirarchy, orient, jointsRoatations, perentMatrixList, perentMatrixListInvers, orentations, orentationsInvert, rotations, rotationsInvert):
sorce = pm.ls(sl = True)[0]
if sorce is None:
sys.stdout.write('Error: At least one joint must be selected.')
return
# Makes sure the joint selected is really the root joint:
rootSorce = ReversedHierarchy(sorce)
rootSorce = pm.ls(sl = True)[0]
# Hierarchy Calculations:
# Add root joint to the list first:
hirarchy.append(rootSorce)
# Add all roots children:
Hirarchy(rootSorce, hirarchy)
for s in hirarchy:
tempParentMatrix = dt.Matrix()
# For sorce:
PerentRotAndOr(s, orient, jointsRoatations)
tempParentMatrix = CalculateOriAndRot(orient, jointsRoatations)
perentMatrixList.append(tempParentMatrix)
perentMatrixListInvers.append(tempParentMatrix.inverse())
orient[:] = []
jointsRoatations[:] = []
for h in hirarchy:
pm.currentTime(0.0)
rot = h.getRotation()
rotation = dt.EulerRotation(rot).asMatrix()
rotations.append(rotation)
rotationsInvert.append(rotation.inverse())
Orientations(hirarchy, orentations, orentationsInvert)
sys.stdout.write('Info: Target skeleton hierarchy calculated.')
#================================================================================
# TESTING
#================================================================================
# Variables:
"""hirarchy = []
orient = []
jointsRoatations = []
perentMatrixList = []
perentMatrixListInvers = []
orentations = []
orentationsInvert = []
rotations = []
rotationsInvert = []
jointNameList = []
jointMatrixesList = []
nrOFFramesAndJoints = []
HirarchyListCreator(hirarchy, orient, jointsRoatations, perentMatrixList, perentMatrixListInvers, orentations, orentationsInvert, rotations, rotationsInvert)
filePath = CreateFilePath()
WriteToFile(filePath, hirarchy, rotationsInvert, orentationsInvert, perentMatrixListInvers, perentMatrixList, orentations)"""