this works with the new python api (2.28 or later) and
im successfully using it in 2.30 too...
Johnatan, sorry if this isnt the proper way to upload
stuff..
##########################
import Blender
from Blender import NMesh
from array import array
#from math import pi
print "moo"
def getTabs(tabCount):
# returns tabCount tab characters as a string
tabs = ""
for i in range(0, tabCount):
tabs = tabs + "\t"
return tabs
def numToFloatXML(theFloat):
# returns the number as an XML string
return "<float>" + str(theFloat) + "</float>"
def numToIntXML(theInt):
return "<int>" + str(theInt) + "</int>"
def vertToArrayChunkXML(vert, tabCount, firstIndex):
# converts a vertex to xml
tabs = getTabs(tabCount)
xml = array('c')
xml.fromstring(tabs + "<void index=\&quot;" + str
(firstIndex) + "\&quot;>\n")
xml.fromstring(tabs + "\t" + numToFloatXML(vert[0])
+ "\n")
xml.fromstring(tabs + "</void>\n")
xml.fromstring(tabs + "<void index=\&quot;" + str(firstIndex
+ 1) + "\&quot;>\n")
xml.fromstring(tabs + "\t" + numToFloatXML(vert[1])
+ "\n")
xml.fromstring(tabs + "</void>\n")
xml.fromstring(tabs + "<void index=\&quot;" + str(firstIndex
+ 2) + "\&quot;>\n")
xml.fromstring(tabs + "\t" + numToFloatXML(vert[2])
+ "\n")
xml.fromstring(tabs + "</void>\n")
return xml
def triangleToArrayChunkXML(vert1, vert2, vert3,
tabCount, firstIndex):
# converts three vertices to xml
xml = array('c')
xml.extend(vertToArrayChunkXML(vert1, tabCount,
firstIndex))
xml.extend(vertToArrayChunkXML(vert2, tabCount,
firstIndex + 3))
xml.extend(vertToArrayChunkXML(vert3, tabCount,
firstIndex + 6))
return xml
def facesToTriangleArrayXML(faces, materialIndex,
tabCount):
# converts a list of faces to an array
# returns it in xml format
index = 0
tabs = getTabs(tabCount)
xml = array('c')
xmlBody = array('c')
for face in faces:
if (face.mat == materialIndex) and (len(face) >
2) :
# if there are three of more verices, and
# if it has the correct material index
xmlBody.extend(triangleToArrayChunkXML(face
[0], face[1], face[2], tabCount + 3, index))
index = index + 9
if len(face) > 3 :
# if there are 4 vertices.
# this breaks quadrilaterals into triangles
xmlBody.extend(triangleToArrayChunkXML
(face[0], face[2], face[3], tabCount + 3, index))
index = index + 9
xml.fromstring(tabs + "<object
class=\&quot;javax.media.j3d.TriangleArray\&quot;>\n")
xml.fromstring(tabs + "\t" + numToIntXML(index)
+ "\n")
xml.fromstring(tabs + "\t<object
class=\&quot;javax.media.j3d.GeometryArray\&quot;
field=\&quot;COORDINATES\&quot;></object>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;coordinates\&quot;>\n")
xml.fromstring(tabs + "\t\t" + numToIntXML(0) + "\n")
xml.fromstring(tabs + "\t\t<array class=\&quot;float\&quot;
length=\&quot;" + str(index) + "\&quot;>\n")
xml.extend(xmlBody)
xml.fromstring(tabs + "\t\t</array>\n")
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</object>\n")
return xml
def facesToArrayXML(faces, materialIndex, tabCount):
# converts a list of faces to an array
# returns it in xml format
index = 0
tabs = getTabs(tabCount)
xml = array('c')
xmlBody = array('c')
for face in faces:
if (face.mat == materialIndex) and (len(face) >
2) :
# if there are three of more verices, and
# if it has the correct material index
xmlBody.extend(triangleToArrayChunkXML(face
[0], face[1], face[2], tabCount + 1, index))
index = index + 9
if len(face) > 3 :
# if there are 4 vertices.
# this breaks quadrilaterals into triangles
xmlBody.extend(triangleToArrayChunkXML
(face[0], face[2], face[3], tabCount + 1, index))
index = index + 9
xml.fromstring(tabs + "<array class=\&quot;float\&quot;
length=\&quot;" + str(index) + "\&quot;>\n")
xml.extend(xmlBody)
xml.fromstring(tabs + "</array>\n")
return xml
def meshComponentToTriangleStripArrayXML(mesh,
materialIndex, tabCount):
xml = prepareMeshComponentToTriangleStripArrayXML
(mesh, materialIndex, tabCount)
xml = actualMeshToComponentTriangleStripArrayXML
(mesh, materialIndex, tabCount)
return xml
def actualMeshToComponentTriangleStripArrayXML
(mesh, materialIndex, tabCount):
name = mesh.name
xml = array('c')
xml.fromstring(tabs + "<object idref=\&quot;"+ name + "-
ga" + str(materialIndex) + "\&quot;></object>\n")
return xml
def prepareMeshComponentToTriangleStripArrayXML
(mesh, materialIndex, tabCount):
# creates a TriangleStripArray from the faces in the
mesh
# with the materialIndex specified
tabs = getTabs(tabCount)
xml = array('c')
faces = mesh.faces
name = mesh.name
# initialise the GeometryInfo doover
xml.fromstring(tabs + "<void
class=\&quot;com.sun.j3d.utils.geometry.GeometryInfo\&quot;
id=\&quot;"+ name + "-gi" + str(materialIndex) + "\&quot;>\n")
xml.fromstring(tabs + "\t<object
class=\&quot;com.sun.j3d.utils.geometry.GeometryInfo\&quot;
field=\&quot;TRIANGLE_ARRAY\&quot;></object>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;coordinates\&quot;>\n")
xml.extend(facesToArrayXML(faces, materialIndex,
tabCount + 2))
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</void>\n")
# do the Normal Generation
xml.fromstring(tabs + "<void
class=\&quot;com.sun.j3d.utils.geometry.NormalGenerator\&quot;>\n
")
xml.fromstring(tabs + "\t<void
property=\&quot;creaseAngle\&quot;>\n")
xml.fromstring(tabs + "\t\t<object
class=\&quot;java.lang.Math\&quot; field=\&quot;PI\&quot;></object>\n")
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "\t<void
method=\&quot;generateNormals\&quot;>\n")
xml.fromstring(tabs + "\t\t<object idref=\&quot;"+ name
+ "-gi" + str(materialIndex) + "\&quot;></object>\n")
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</void>\n")
# do the Stripification
xml.fromstring(tabs + "<void
class=\&quot;com.sun.j3d.utils.geometry.Stripifier\&quot;>\n")
xml.fromstring(tabs + "\t<void method=\&quot;stripify\&quot;>\n")
xml.fromstring(tabs + "\t\t<object idref=\&quot;"+ name
+ "-gi" + str(materialIndex) + "\&quot;></object>\n")
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</void>\n")
# return the TriangleStripArray
xml.fromstring(tabs + "<void idref=\&quot;"+ name + "-gi" +
str(materialIndex) + "\&quot;>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;geometryArray\&quot; id=\&quot;"+ name + "-ga" + str
(materialIndex) + "\&quot;></void>\n")
xml.fromstring(tabs + "</void>\n")
return xml
def meshComponentToShape3DXML(mesh,
materialIndex, tabCount):
# creates a Shape3D from all the faces in the mesh
with the
# materialIndex specified
tabs = getTabs(tabCount)
xml = array('c')
#materials = Blender.Object.GetSelected()[0].getData
().materials
materials = mesh.materials
name = mesh.name
xml.extend
(prepareMeshComponentToTriangleStripArrayXML(mesh,
materialIndex, tabCount))
xml.fromstring(tabs + "<object
class=\&quot;javax.media.j3d.Shape3D\&quot;>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;geometry\&quot;>\n")
xml.fromstring(tabs + "\t\t<object idref=\&quot;"+ name
+ "-ga" + str(materialIndex) + "\&quot;></object>\n")
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;appearance\&quot;>\n")
xml.extend(materialToAppearanceXML(materials
[materialIndex], tabCount + 2))
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</object>\n")
return xml
def meshToShape3DXMLList(mesh, tabCount):
# creates a set of Shape3D objects (in XML) stored
in a list
# crea la lista dei componenti per materiale
tabs = getTabs(tabCount)
theList = []
#materials = Blender.Object.GetSelected()[0].getData
().materials
materials = mesh.materials
for materialIndex in range(0, len(materials)):
theList.append(meshComponentToShape3DXML
(mesh, materialIndex, tabCount))
return theList
def colourToColor3fXML(r, g, b, tabCount):
# turns the rgb values into a Color3f object in XML
form
tabs = getTabs(tabCount)
xml = array('c')
xml.fromstring(tabs + "<object
class=\&quot;javax.vecmath.Color3f\&quot;>\n")
xml.fromstring(tabs + "\t" + numToFloatXML(r) + "\n")
xml.fromstring(tabs + "\t" + numToFloatXML(g) + "\n")
xml.fromstring(tabs + "\t" + numToFloatXML(b) + "\n")
xml.fromstring(tabs + "</object>\n")
return xml
def materialToMaterialXML(material, tabCount):
# converts a blender material into a java material
object
# in xml form
tabs = getTabs(tabCount)
xml = array('c')
xml.fromstring(tabs + "<object
class=\&quot;javax.media.j3d.Material\&quot;>\n")
#ambient colour
xml.extend(colourToColor3fXML( material.R *
material.amb,
material.G * material.amb,
material.B * material.amb,
tabCount + 1
))
#emissive colour
xml.extend(colourToColor3fXML( material.emit,
material.emit, material.emit, tabCount + 1))
#diffuse colour
xml.extend(colourToColor3fXML( material.R,
material.G,
material.B,
tabCount + 1
))
xml.extend(colourToColor3fXML( material.specR,
material.specG,
material.specB,
tabCount + 1
))
xml.fromstring(tabs + "\t" + numToFloatXML
(material.spec * 64) + "\n")
xml.fromstring(tabs + "</object>\n")
return xml
def lampsToLights(list):
for lamp in list:
def materialToAppearanceXML(material, tabCount):
# converts the material into a java Appearance object
tabs = getTabs(tabCount)
xml = array('c')
xml.fromstring(tabs + "<object
class=\&quot;javax.media.j3d.Appearance\&quot;>\n")
xml.fromstring(tabs + "\t<void
property=\&quot;material\&quot;>\n")
xml.extend(materialToMaterialXML(material, tabCount
+ 2))
xml.fromstring(tabs + "\t</void>\n")
xml.fromstring(tabs + "</object>\n")
return xml
def outputXML(xml, filename):
# outputs the xml to the file
outXML = array('c', "<?xml version=\&quot;1.0\&quot;
encoding=\&quot;UTF-8\&quot;?>\n<java version=\&quot;1.4.1_01\&quot;
class=\&quot;java.beans.XMLDecoder\&quot;>\n")
outXML.extend(xml)
outXML.fromstring("</java>")
openfile = open(filename, "wb")
outXML.tofile(openfile)
openfile.flush()
openfile.close()
print "wrote " + filename
objList = Blender.Object.GetSelected()
lampList = Blender.Lamp.Get()
lampsToLights(lampList)
for obj in objList :
mesh = obj.getData()
print mesh
moo = meshToShape3DXMLList(mesh, 1)
for i in range(0, len(moo)):
outputXML(moo[i], mesh.materials[i].name + "." +
mesh.name + ".Shape3D.xml")
print("done")