NDS 3d display list exporter
Posted: Fri May 16, 2008 4:15 am
I have created a very buggy and limited display list exporter for blender. It can only export vertices, normals, and diffuse material colors and has no error checking. Perhaps somebody could improve it? I don't understand what the other material colors do and I think the normals blender gives me are wrong/weird.
Code: Select all
#!BPY
"""
Name: 'test'
Blender: 244
Group: 'Export'
Tooltip: 'test'
"""
import Blender
import bpy
import struct
def writeu32(file,num):
file.write(struct.pack("<I",num))
def writeu16(file,num):
file.write(struct.pack("<H",num))
def floattov16(float):
return int(float * (1 << 12))
def floattocolor(float):
return (int(float * 255) & 0x1F)
def floattov10(float):
if float>.998:
return 0x1FF
return int(float * (1 << 9))
def packnormal(x,y,z):
return ((x) & 0x3FF) | (((y) & 0x3FF) << 10) | ((z) << 20)
def writevert(file,vert):
writeu32(file,0x23)
x = floattov16(vert.co.x)
y = floattov16(vert.co.y)
z = floattov16(vert.co.z)
# print '%f %f %f\n' % (vert.co.x,vert.co.y,vert.co.z)
# print '%i %i %i\n' % (x,y,z)
writeu16(file,x)
writeu16(file,y)
writeu16(file,z)
writeu16(file,0)
def writenormal(file,normal):
writeu32(file,0x21)
print '%f %f %f\n' % (normal.x,normal.y,normal.z)
x = floattov10(normal.x)
y = floattov10(normal.y)
z = floattov10(normal.z)
writeu32(file,packnormal(x,y,z))
def writecolorf(file,r,g,b,upper):
writeu16(file,floattocolor(r) | (floattocolor(g) << 5) | (floattocolor(b) << 10) | upper << 15)
def write(filename):
out = file(filename,'wb')
sce = bpy.data.scenes.active
obj = sce.objects.active
mesh = obj.getData(mesh=1)
writeu32(out,0)
writeu32(out,0x40)
writeu32(out,0)
for face in mesh.faces:
mat = mesh.materials[face.mat]
writeu32(out,0x30)
# print '%f %f %f\n' % (mat.getRGBCol()[0],mat.getRGBCol()[1],mat.getRGBCol()[2])
writecolorf(out,mat.getRGBCol()[0],mat.getRGBCol()[1],mat.getRGBCol()[2],0)
writeu16(out,0)
writenormal(out,face.no)
verts = face.v
if len(verts)==3: #triangle
for vert in verts:
writevert(out,vert)
else: #quad
writevert(out,verts[0])
writevert(out,verts[1])
writevert(out,verts[2])
writevert(out,verts[0])
writevert(out,verts[2])
writevert(out,verts[3])
size = out.tell() - 4
size = size / 4
out.seek(0)
writeu16(out,size)
out.close()
Blender.Window.FileSelector(write, "Export")