Page 1 of 1

Blender cmod export script

Posted: 21.08.2006, 13:33
by cartrite
Hi all,
A while back I wote a script that exports model created in blender to cmod format. I had a few problems with it that involved removing doubles with blender. Doing so created a segmentation fault. I since rewote it to get around that problem. This should work with models that have been converted to triangles. Blender creates models with quads by default. I would recommend if you convert to triangles that it be saved under another name because I had problems converting back to quads. I've only tested this with planetary spheres. If anyone wants to give it a try here is the script I use for mars.

Code: Select all

#!BPY

"""
Name: 'CMOD-norm-spec-mars1 (.cmod)...'
Blender: 240
Group: 'Export'
Tooltip: 'cmod exporter'
"""
import Blender
from Blender import *
import sys
import math

global MTL_DICT

MTL_DICT = {}


def write_obj(filepath):
        out = file(filepath, 'w')
        object = Blender.Object.GetSelected()[0]
        #mesh = object.getData()
        mesh = NMesh.GetRawFromObject(object.name)
        mesh.transform(object.matrix)
        a = 0
        b = 0
        c = 0
        d = 0

        # file header for materials
        out.write('#celmodel__ascii\n\n')
        out.write('material\n')
        out.write('diffuse 0.878431 0.878431 0.878431\n')
        out.write('specular 0.6 0.4 0.25\n')
        out.write('specpower 1\n')
        out.write('opacity 1\n')
        out.write('texture0 "mars.png"\n')
        out.write('normalmap "norm.png"\n')
        out.write('specularmap "spec.png"\n')
        out.write('end_material\n\n')
        out.write('mesh\n')
        out.write('vertexdesc\n')
        out.write('position f3\n')
        out.write('normal f3\n')
        out.write('texcoord0 f2\n')
        out.write('end_vertexdesc\n\n')
       
       
        #counter for vertice desc
        #This counts the number of vertices.
        for face in mesh.faces:
            for vert in face.v:
                   a +=1   
        out.write( 'vertices %i\n' % (a))

        # writes a line in the vertex pool.
        for face in mesh.faces:
            d = 0
            for vert in face.v:
               out.write( '%f %f %f %f %f %f' % (face.v[d].co.x, face.v[d].co.y, face.v[d].co.z, face.v[d].no.x, face.v[d].no.z, face.v[d].no.y,))
               out.write( ' %f %f\n' % face.uv[d])
               d +=1
        out.write( '\n\ntrilist %i %i\n' % (b, a))
        #write the triangle list 12 per line
        while b < a:
            if c <= 12:
               if b <= a:
                  out.write( '%i ' % (b))
                  b +=1
                  c +=1
               else:
                  out.write( '\n' )
            else:
                out.write( '\n' )
                c = 0
        out.write( '\n\nend_mesh' )               
        out.close()
Blender.Window.FileSelector(write_obj, "Export", Blender.sys.makename(ext='.cmod' ))


This script can be edited to make the materials suite your needs or the cmod file can be edited. Also blender mirrors everything. Those that use it may already know this. The exported cmod file needs to be run though cmodfix to generate normals to get it to work properly with Celestia. Also in UVimage editor the UV's need to be rotated on the x axis to get the texture to appear right side up. Multible meshes are treated as a single mesh so each mesh would have to be exported seperatly. I assign each mesh a group # so they can be seperated for exporting. In edit mode a group can be selected or deselected.

cartrite