Ok heres the code for the b3d script,
however I cant get this to load in a b3d viewer
I am (I think) using the specs listed here:
http://www.blitzbasic.com/sdkspecs/sdkspecs/b3dfile_specs.txt
but still no dice
Could someone lend a hand?:
#!BPY
"""
Name: 'B3D Exporter by German Cons'
Blender: 234
Group: 'Export'
Tip: 'Export mesh to the B3D format.'
"""
####[ Consts ]####
TRUE=1
FALSE=0
####[ Imports ]####
import sys, os, os.path, struct, math, string
import Blender
from Blender.BGL import *
from Blender.Draw import *
from Blender.Armature import *
from Blender.Registry import *
# HACK -- it seems that some Blender versions don't define sys.argv,
# which may crash Python if a warning occurs.
if not hasattr(sys, "argv"): sys.argv = ["???"]
# Export this very polygon, and all related information!
def b3dWriteVertex(f,ObjData,Face,v1):
Vert1=Face.v[v1].index # Face vertex reference information...
Coord1=ObjData.verts[Vert1].co # Coord information of this vertex
Normal1=ObjData.verts[Vert1].no # Normal information
UV1=[0,1] #,[1,0],[1,1] # Default values for uv info
UV1=Face.uv[v1] # Grab the uv coords!
#Coord information...X,Y,Z
b3dWriteFloat(f,Coord1[0]) #X,Y,Z coords (reletive to the center)..
b3dWriteFloat(f,Coord1[1])
b3dWriteFloat(f,Coord1[2])
b3dWriteFloat(f,Normal1[0]) #X,Y,Z normal
b3dWriteFloat(f,Normal1[1])
b3dWriteFloat(f,Normal1[2])
#no colors for now
b3dWriteFloat(f,UV1[0]) #U
b3dWriteFloat(f,UV1[1]) #V
def b3dWriteVerts(f,obj):
if obj:
iPolyCount=0
b3dBeginChunk(f,'VRTS')
b3dWriteInt(f,1) #Flags 1 normal values present 2 rgba values present
b3dWriteInt(f,1) #uv
b3dWriteInt(f,2) #uv sets?
ObjData=obj.getData()
if ObjData:
for face in ObjData.faces:
iPolyCount=iPolyCount+1
for v in range(0,2):
b3dWriteVertex(f,ObjData,face,v)
print iPolyCount
#x,y,z
#nx,ny,nz
#r,g,b,a ;not available at the time
#u,v
b3dEndChunk(f)
return iPolyCount
def b3dWriteTris(f,obj,PolyCount):
if obj:
b3dBeginChunk(f,'TRIS')
b3dWriteInt(f,-1) #no brush
#since we dont have sharing indices we just write down the whole polygon count
print PolyCount
for c in range(0,PolyCount):
b3dWriteInt(f,3*c)
b3dWriteInt(f,3*c+1)
b3dWriteInt(f,3*c+2)
b3dEndChunk(f)
def b3dWriteMesh(f,obj):
if obj:
b3dBeginChunk(f,'MESH')
b3dWriteInt(f,-1) #no brush, sorry not at the time
PolyCount=b3dWriteVerts(f,obj)
b3dWriteTris(f,obj,PolyCount)
b3dEndChunk(f)
# This will loop through the mesh and export all the polygons (3 vert and 4 verts)
def ExportObject(f,obj):
if obj:
b3dBeginChunk(f,'NODE')
#f.write(obj.name+chr(0)) # Save the name of this mesh first!
b3dWriteString(f,obj.name) #name
b3dWriteVector(f,0,0,0) #position
b3dWriteVector(f,1,1,1) #escala (this will change in the future)
b3dWriteQuaternion(f,1,0,0,0) #rotation
b3dWriteMesh(f,obj)
b3dEndChunk(f)
#b3d_tos = 0 (deprecated)
b3d_stack = [0]
#new functions for b3d
def b3dWriteByte(f,dat):
f.write(struct.pack('b',dat))
def b3dWriteFloat(f,dat):
f.write(struct.pack('f',dat))
def b3dWriteInt(f,dat):
f.write(struct.pack('i',dat))
def b3dWriteString(f,dat):
l=len(dat)
for c in range(0,l):
f.write(struct.pack('1s',dat[c]))
f.write(struct.pack('b',0))
def b3dWriteVector(f,datx,daty,datz):
b3dWriteFloat(f,datx)
b3dWriteFloat(f,daty)
b3dWriteFloat(f,datz)
def b3dWriteQuaternion(f,datw,datx,daty,datz):
b3dWriteFloat(f,datw)
b3dWriteFloat(f,datx)
b3dWriteFloat(f,daty)
b3dWriteFloat(f,datz)
def b3dBeginChunk(f,tag):
#b3d_tos=b3d_tos+1
b3dWriteString(f,tag)
b3dWriteInt(f,0)
#b3d_stack[b3d_tos]=OutputFile.tell()
b3d_stack.append(f.tell())
#b3d_stack.append(OutputFile.tell())
def b3dEndChunk(f):
n=f.tell()
p=b3d_stack.pop()
f.seek(p-4)
#SeekFile b3d_file,b3d_stack(b3d_tos)-4
b3dWriteInt(f,n-p)
f.seek(n)
#SeekFile b3d_file,n
#b3d_tos=b3d_tos-1 #decrease reference
####[ Main program ]####
def fs_callback(filename):
print ""
print filename
print "Exporting begins."
#If the user has selected a object to work on...
if Blender.Object.GetSelected()[0]:
OutputFile=open(filename,'w+b')
if OutputFile:
b3dBeginChunk(OutputFile,'BB3D')
b3dWriteInt(OutputFile,100) #version
#export selected object
ExportObject(OutputFile,Blender.Object.GetSelected()[0])
b3dEndChunk(OutputFile)
OutputFile.close()
else:
print "Couldn't export file"
print "Exporter ended."
# Main script
#global OutputFile # File pointer to the above file while saving
defaultname = Blender.Get('filename')
if defaultname.endswith(".blend"):
defaultname = defaultname[0:len(defaultname)-len(".blend")] + ".b3d"
Blender.Window.FileSelector(fs_callback, "B3D Export", defaultname)