June 30, 2009

Basic 3DS exporter

Posted in Computer Graphics tagged , , , , , , at 11:51 pm by sagito

Yesterday I began my learning journey in the realm of MaxScript. Today, I have finished my basic exporter! Although this scripting language is vastly documented, this documentation is quite confusing at first. But with some patience and thanks to this post (http://forum.hardware.fr/hfr/Graphisme/Divers-5/maxscript-getfacematid-sujet_17077_1.htm) from Nico5779 I have managed to get my exporter working.

So, if anyone needs to look at this script for reference or use it, be my guest… If you want some explanation about what is being done in the code below, I will be most happy to help you. Please note that texture coordinates are still not being considered. I will do that in a later version of this exporter and I shall post it here again afterwards. 😉

Here is the code:

–Start by selecting every thing
select $*

out_name = ((GetDir #export) + “/test.cmf”)
out_file = createfile out_name

–For every one of them
for obj in $ do
(
actualMesh = snapshotAsMesh obj as TriMesh

–Get the number of faces
format “Name: %\n” obj.name to:out_file

numFaces = actualMesh.numfaces
numVerts = actualMesh.numverts

format “Faces: %\n” numFaces to:out_file

format “<vertices %>\n” numVerts to:out_file
for v = 1 to numVerts do
(
vert = getVert actualMesh v
normal = getNormal actualMesh v
format “% % % % % %\n” vert.x vert.y vert.z normal.x normal.y normal.z to:out_file
)
format “</vertices>\n” to:out_file

print “>>> Obtaining face information!\n”

midold = -1
objMaterials = #()

for f = 1 to numFaces do
(
face = getFace actualMesh f
materialClass = classof obj.material

if(materialClass == Standardmaterial) then
(
matname = obj.material
objMaterial = obj.material

if (findItem(objMaterials objMaterial) == 0) then
(
append objMaterials objMaterial
)
)
else if (materialClass != UndefinedClass) then
(
matid = getFaceMatId actualMesh f
matname = obj.material[matid]
objMaterial = obj.material[matid]

if (findItem(objMaterials objMaterial) == 0) then
(
append objMaterials objMaterial
)
)

if(midold != matname) then
(
format “<material %>\n” matname to:out_file
format “% % % % % % % % %\n” objMaterial.ambient.r objMaterial.ambient.g objMaterial.ambient.b objMaterial.diffuse.r objMaterial.diffuse.g objMaterial.diffuse.b objMaterial.specular.r objMaterial.specular.g objMaterial.specular.b to:out_file

format “<ambientmap> % </ambientmap>\n” objMaterial.ambientMap
format “<diffusemap> % </diffusemap>\n” objMaterial.diffuseMap
format “<specularmap> % </specularmap>\n” objMaterial.specularMap

format “</material>\n” to:out_file
midold = matname
)

face3 = getFaceNormal actualMesh f

x = face.x as integer
y = face.y as integer
z = face.z as integer

x2 = face3.x as integer
y2 = face3.y as integer
z2 = face3.z as integer

format “<face>% % % % % %</face>\n” x y z x2 y2 z2 to:out_file
)

for om = 1 to objMaterials.count do
(
for m = 1 to objMaterials[om].maps.count do
(
if (objMaterials[om].maps[m] != undefined) then
(
format “<map %>\n” objMaterials[om].maps[m] to:out_file
format “Filename: %\n” objMaterials[om].maps[m].filename to:out_file
format “</map>\n” to:out_file
)
)
)
)
close out_file

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: