-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvancedfx_export_bvh.py
129 lines (94 loc) · 3.02 KB
/
advancedfx_export_bvh.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
# Copyright (c) advancedfx.org
#
# Last changes:
# 2016-06-28 by dominik.matrixstorm.com
#
# First changes:
# 2009-09-03 by dominik.matrixstorm.com
# 57.29577951308232087679815481410...
RAD2DEG = 57.2957795130823208767981548141
import sfm;
import sfmUtils;
import sfmApp;
from PySide import QtGui
def SetError(error):
print 'ERROR:', error
QtGui.QMessageBox.warning( None, "ERROR:", error )
# <summary> Formats a float value to be suitable for bvh output </summary>
def FloatToBvhString(value):
return "{0:f}".format(value)
def WriteHeader(file, frames, frameTime):
file.write("HIERARCHY\n")
file.write("ROOT MdtCam\n")
file.write("{\n")
file.write("\tOFFSET 0.00 0.00 0.00\n")
file.write("\tCHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation\n")
file.write("\tEnd Site\n")
file.write("\t{\n")
file.write("\t\tOFFSET 0.00 0.00 -1.00\n")
file.write("\t}\n")
file.write("}\n")
file.write("MOTION\n")
file.write("Frames: "+str(frames)+"\n")
file.write("Frame Time: "+FloatToBvhString(frameTime)+"\n")
def LimDeg(val):
return val
def WriteFile(fileName, scale):
shot = sfm.GetCurrentShot()
animSet = sfm.GetCurrentAnimationSet()
dag = sfm.FindDag("transform")
if dag == None:
SetError("Selected animation set does not have transform DAG node.")
return False
curFrame = 0
fps = sfmApp.GetFramesPerSecond()
frameTime = fps
if not 0 == frameTime:
frameTime = 1.0/float(frameTime)
frameCount = shot.GetDuration().CurrentFrame(vs.DmeFramerate_t(fps))
file = open(fileName, 'wb')
if not file:
SetError('Could not open file '+fileName+' for writing')
return False
oldFrame = sfm.GetCurrentFrame()
try:
WriteHeader(file, frameCount, frameTime)
while curFrame<frameCount:
sfm.SetCurrentFrame(curFrame)
loc = sfm.GetPosition("transform", space="World")
rot = sfm.GetRotation("transform", space="World")
X = -loc[1] *scale
Y = loc[2] *scale
Z = -loc[0] *scale
ZR = -rot[0] #*RAD2DEG
XR = -rot[1] #*RAD2DEG
YR = rot[2] #*RAD2DEG
ZR = LimDeg(ZR)
XR = LimDeg(XR)
YR = LimDeg(YR)
S = "" +FloatToBvhString(X) +" " +FloatToBvhString(Y) +" " +FloatToBvhString(Z) +" " +FloatToBvhString(ZR) +" " +FloatToBvhString(XR) +" " +FloatToBvhString(YR) + "\n"
file.write(S)
curFrame += 1
finally:
file.close()
sfm.SetCurrentFrame(oldFrame)
if not curFrame == frameCount:
SetError("Could not write all frames.")
return False
return True
def ExportCamera():
#value, ok = QtGui.QInputDialog.getDouble(None, "Enter export FPS", "Frames Per Second", 60, 0.001, 1000000, 3)
#if not ok:
# return
fileName, _ = QtGui.QFileDialog.getSaveFileName(None, "Save HLAE BVH File", "", "HLAE BVH (*.bvh)")
if not 0 < len(fileName):
return
sfm.SetOperationMode( "Play" )
try:
if WriteFile(fileName, 1.0):
print 'Done.'
else:
print 'FAILED'
finally:
sfm.SetOperationMode( "Pass" )
ExportCamera()