File: m3dexport.py

package info (click to toggle)
antigrav 0.0.3-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,728 kB
  • sloc: cpp: 7,409; xml: 5,603; sh: 3,366; python: 150; makefile: 44
file content (198 lines) | stat: -rw-r--r-- 5,066 bytes parent folder | download | duplicates (8)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!BPY
"""
Name: 'm3d mesh exporter'
Blender: 237
Group: 'Export'
Tooltip: 'Export meshes to .xml'
"""

import Blender
from Blender import NMesh, Object, Scene, Lamp

import math
from math import sin, cos

import os

global indentation
indentation = 0

def indent(file):
	if indentation < 1:
		return
	
	for i in range(indentation):
		file.write("\t");
		
def setIndent(d):
	global indentation
	indentation += d

def toQuaternion(fRoll, fYaw, fPitch):
	fSinPitch = sin(fPitch*0.5)
	fCosPitch = cos(fPitch*0.5)
	fSinYaw = sin(fYaw*0.5)
	fCosYaw = cos(fYaw*0.5)
	fSinRoll = sin(fRoll*0.5)
	fCosRoll = cos(fRoll*0.5)
	fCosPitchCosYaw = fCosPitch*fCosYaw
	fSinPitchSinYaw = fSinPitch*fSinYaw
	X = fSinRoll * fCosPitchCosYaw     - fCosRoll * fSinPitchSinYaw
	Y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw
	Z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw
	W = fCosRoll * fCosPitchCosYaw     + fSinRoll * fSinPitchSinYaw
	return [W, X, Y, Z]

def export(obj, file):
	print "Export %s\n" %obj.getName()
	
	locX = obj.LocX
	locY = obj.LocY
	locZ = obj.LocZ
	rotX = obj.RotX
	rotY = obj.RotY
	rotZ = obj.RotZ
	sizeX = obj.SizeX
	sizeY = obj.SizeY
	sizeZ = obj.SizeZ
	
	parent = obj.getParent()
	while parent:
		locX -= parent.LocX
		locY -= parent.LocY
		locZ -= parent.LocZ
		rotX -= parent.RotX
		rotY -= parent.RotY
		rotZ -= parent.RotZ
		sizeX /= parent.SizeX
		sizeY /= parent.SizeY
		sizeZ /= parent.SizeZ
		parent = parent.getParent()
		
	loc = [locX, locY, locZ]
	size = [sizeX, sizeY, sizeZ]
	quat = toQuaternion(rotX, -rotY, rotZ)
	
	if obj.getType() == "Mesh":
		exportMesh(obj, file, loc, size, quat)
	
def exportMaterial(mat, file):
	indent(file)
	file.write("<Material name=\"%s\" " % mat.getName())
	col = mat.getRGBCol()
	file.write("diffuseR=\"%1.4f\" diffuseG=\"%1.4f\" diffuseB=\"%1.4f\" " % (col[0], col[1], col[2]))
	amb = mat.getAmb()
	col[0] *= amb
	col[1] *= amb
	col[2] *= amb
	file.write("ambientR=\"%1.4f\" ambientG=\"%1.4f\" ambientB=\"%1.4f\" " % (col[0], col[1], col[2]))
	col = mat.getSpecCol()
	file.write("specularR=\"%1.4f\" specularG=\"%1.4f\" specularB=\"%1.4f\" " % (col[0], col[1], col[2]))
	file.write("shininess=\"%1.4f\"" % (mat.getSpec()))
	file.write("/>\n")
	
def exportTexture(tex, file):
	indent(file)
	file.write("<Texture name=\"%s\" units=\"1\">\n" % tex.getName())
	indent(file)
	file.write("\t<Image filename=\"%s\"/>\n" % os.path.basename(tex.getFilename()))
	indent(file)
	file.write("</Texture>\n")

def exportMesh(obj, file, loc, size, quat):
	mesh = NMesh.GetRawFromObject(obj.getName())
	
	indent(file)
	file.write("<Mesh name=\"%s\" numVertices=\"%d\" numFaces=\"%d\" " %(obj.getName(), len(mesh.verts), len(mesh.faces)))
	file.write("x=\"%3.5f\" y=\"%3.5f\" z=\"%3.5f\" " %(loc[0], loc[2], -loc[1]))
	file.write("sx=\"%3.5f\" sy=\"%3.5f\" sz=\"%3.5f\" " %(size[0], size[1], size[2]))
	file.write("qw=\"%3.5f\" qx=\"%3.5f\" qy=\"%3.5f\" qz=\"%3.5f\"" %(quat[0], quat[1], quat[2], quat[3]))
	file.write(">\n")
	setIndent(1)
	
	for mat in mesh.materials:
		exportMaterial(mat, file)
	if len(mesh.materials) > 0:
		file.write("\n")
		
	textures = []
	if mesh.hasFaceUV():
		for face in mesh.faces:
			if not face.image:
				continue;
				
			if textures.count(face.image) == 0:
				textures.append(face.image)
				
	for tex in textures:
		exportTexture(tex, file)
	if len(textures) > 0:
		file.write("\n")

	for vert in mesh.verts:
		indent(file)
		file.write("<Vertex x=\"%3.5f\" y=\"%3.5f\" z=\"%3.5f\" " %(vert.co[0], vert.co[2], -vert.co[1]))
		file.write("nx=\"%3.5f\" ny=\"%3.5f\" nz=\"%3.5f\"/>\n" %(vert.no[0], vert.no[2], -vert.no[1]))
	
	file.write("\n")
	
	for face in mesh.faces:
		indent(file)
		file.write("<Face smooth=\"%d\" " %face.smooth)
		file.write("nx=\"%3.5f\" ny=\"%3.5f\" nz=\"%3.5f\" " %(face.normal[0], face.normal[2], -face.normal[1]))
		
		if face.image:
			file.write("texture=\"%d\" " %(textures.index(face.image)))
		else:
			file.write("texture=\"-1\" ")
		
		mat = face.materialIndex
		if mat >= len(mesh.materials):
			mat = -1
		
		file.write("material=\"%d\">\n" %(mat))
		
		setIndent(1)
		for i in range(3):
			indent(file);
			file.write("<Vertex index=\"%d\" " %face.v[i].index )
			
			if mesh.hasFaceUV() and face.image:
				file.write("u=\"%1.3f\" v=\"%1.3f\"" %(face.uv[i][0], 1.0-face.uv[i][1]))
			
			file.write("/>\n")

		setIndent(-1)
		
#		file.write(" vertex1=\"%d\" vertex2=\"%d\" vertex3=\"%d\"/>\n" %(face.v[0].index, face.v[1].index, face.v[2].index))
		indent(file)
		file.write("</Face>\n")
	
	Object.GetSelected().remove(obj)
	for child in Object.GetSelected():
		if child.parent == obj:
			export(child, file)
			Object.GetSelected().remove(child)
	
	setIndent(-1)
	indent(file)
	file.write("</Mesh>\n\n")

def main():
	selected = Object.GetSelected()
	
	if len(selected) == 0:
		print "Nothing selected"
		return
	
	for obj in selected:
		if obj.getType() != "Mesh":
			continue
		
		file = open("%s.xml" % (obj.getName()), "w")
		file.write("<? xml version=\"1.0\" ?>\n")
		export(obj, file)
		file.close()
	
	
main()