Welcome to mirror list, hosted at ThFree Co, Russian Federation.

export_mdd.py « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81c45c7ab12ee4476c7901ac1955f49de752c750 (plain)
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
#!BPY

"""
 Name: 'Save Mesh as MDD'
 Blender: 242
 Group: 'Export'
 Tooltip: 'Animated mesh to MDD vertex keyframe file.'
"""

__author__ = "Bill L.Nieuwendorp"
__bpydoc__ = """\
This script Exports Lightwaves MotionDesigner format.

The .mdd format has become quite a popular Pipeline format<br>
for moving animations from package to package.

Be sure not to use modifiers that change the number or order of verts in the mesh
"""
#Please send any fixes,updates,bugs to Slow67_at_Gmail.com or cbarton_at_metavr.com
#Bill Niewuendorp

import Blender
from Blender import *
import BPyMessages
try:
	from struct import pack
except:
	pack = None


def zero_file(filepath):
	'''
	If a file fails, this replaces it with 1 char, better not remove it?
	'''
	file = open(filepath, 'w')
	file.write('\n') # aparently macosx needs some data in a blank file?
	file.close()

def mdd_export(filepath, ob, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS):
	
	Window.EditMode(0)
	Blender.Window.WaitCursor(1)
	mesh_orig = ob.getData(mesh=1)
	
	#Flip y and z
	'''
	mat = Mathutils.Matrix()
	mat[2][2] = -1
	rotmat = Mathutils.RotationMatrix(90, 4, 'x')
	mat_flip = mat*rotmat
	'''
	# Above results in this matrix
	mat_flip= Mathutils.Matrix(\
	[1.0, 0.0, 0.0, 0.0],\
	[0.0, 0.0, 1.0, 0.0],\
	[0.0, 1.0, 0.0, 0.0],\
	[0.0, 0.0, 0.0, 1.0],\
	)
	
	me_tmp = Mesh.New() # container mesh

	numverts = len(mesh_orig.verts)
	numframes = PREF_ENDFRAME-PREF_STARTFRAME+1
	PREF_FPS= float(PREF_FPS)
	f = open(filepath, 'wb') #no Errors yet:Safe to create file
	
	# Write the header
	f.write(pack(">2i", numframes-1, numverts))
	
	# Write the frame times (should we use the time IPO??)
	f.write( pack(">%df" % (numframes-1), *[frame/PREF_FPS for frame in xrange(numframes-1)]) ) # seconds
	
	Blender.Set('curframe', PREF_STARTFRAME)
	for frame in xrange(PREF_STARTFRAME,PREF_ENDFRAME+1):#in order to start at desired frame
		Blender.Set('curframe', frame)
		# Blender.Window.RedrawAll() # not needed
		me_tmp.getFromObject(ob.name)
		
		if len(me_tmp.verts) != numverts:
			Blender.Draw.PupMenu('Error%t|Number of verts has changed during animation|cannot export')
			Blender.Window.WaitCursor(0)
			f.close() # should we zero?
			zero_file(filepath)
			return
		
		me_tmp.transform(ob.matrixWorld * mat_flip)
		
		# Write the vertex data
		f.write(pack(">%df" % (numverts*3), *[axis for v in me_tmp.verts for axis in v.co]))
	
	me_tmp.verts= None
	f.close()
	
	print'MDD Exported: %s frames:%d\n'% (filepath, numframes-1)  
	Blender.Window.WaitCursor(0)


def mdd_export_ui(filepath):
	# Dont overwrite
	if not BPyMessages.Warning_SaveOver(filepath):
		return
	
	scn= Scene.GetCurrent()
	ob_act= scn.objects.active
	if not ob_act or ob_act.type != 'Mesh':
		BPyMessages.Error_NoMeshActive()
	
	ctx = scn.getRenderingContext()
	orig_frame = Blender.Get('curframe')
	PREF_STARTFRAME= Blender.Draw.Create(int(ctx.startFrame()))
	PREF_ENDFRAME= Blender.Draw.Create(int(ctx.endFrame()))
	PREF_FPS= Blender.Draw.Create(ctx.fps)

	block = [\
	("Start Frame: ", PREF_STARTFRAME, 1, 30000, "Start Bake from what frame?: Default 1"),\
	("End Frame: ", PREF_ENDFRAME, 1, 30000, "End Bake on what Frame?"),\
	("FPS: ", PREF_FPS, 1, 100, "Frames per second")\
	]
	
	if not Blender.Draw.PupBlock("Export MDD", block):
		return
	
	PREF_STARTFRAME, PREF_ENDFRAME=\
		min(PREF_STARTFRAME.val, PREF_ENDFRAME.val),\
		max(PREF_STARTFRAME.val, PREF_ENDFRAME.val)
	
	print (filepath, ob_act, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS.val)
	mdd_export(filepath, ob_act, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS.val)
	Blender.Set('curframe', orig_frame)
	
if __name__=='__main__':
	if not pack:
		Draw.PupMenu('Error%t|This script requires a full python install')
	
	Blender.Window.FileSelector(mdd_export_ui, 'EXPORT MDD', sys.makename(ext='.mdd'))