# Hi, Here is an export script I wrote
# Basicly the output of 'print' is written to a file. so print is like- add a # line to the file.
# I have used
print "x", 45, "someword"
# To print many variables onto one line and...
print ''
# or
print '\n'
# To print a new line
# Just find some docs about the fileformat you want to export to and try to export a single mesh.
# You could rip the guts out of my script and use it if you like.
# Hope this helps
# FIXME Unique verts for each face.
# FIXME add TEXTYRE wrapU, wrapV, Backfacing
#
# MYFORMAT Format
#
# MYFORMAT
# NumOfImages NumOfMaterials
# Image.bmp UWrap VWrap DoubleSided
# Image2.bmp UWrap VWrap DoubleSided
# ''' Materials '''
# AmbientRed aGreen aBlue DifuseRed dG dB specRed sG sB EmitR eG eB Shinyness Alpha
# Geo
print "Starting the MYFORMAT export process"
import Blender
import math, sys
from Blender import NMesh, Object, Material
from math import *
allObjects = Object.getSelected()
filename = "newfile.MYFORMAT"
file = open(filename, "w")
std=sys.stdout
sys.stdout=file
########################
# Variables for report #
########################
meshCount = 0
vertCount = 0
polyCount = 0
materialCount = 0
textureCount = 0
#############################################
# Turns floats to 6 precission and no chars #
#############################################
def saneFloat(float):
return '%f' % float # 10 fp
###################
# Apply Transform #
###################
def apply_transform(verts, matrix):
x, y, z = verts
xloc, yloc, zloc = matrix[3][0], matrix[3][1], matrix[3][2]
xcomponent = x*matrix[0][0] + y*matrix[1][0] + z*matrix[2][0] + xloc
ycomponent = x*matrix[0][1] + y*matrix[1][1] + z*matrix[2][1] + yloc
zcomponent = x*matrix[0][2] + y*matrix[1][2] + z*matrix[2][2] + zloc
verts = [xcomponent, ycomponent, zcomponent]
return verts
## example
#for i in range(len(mesh.verts)):
# x, y, z = apply_transform(mesh.verts[i].co, matrix)
#########################################
# Make a list images used for uvmapping #
#########################################
imageCast=[]
for object in allObjects:
if object.getType() == "Mesh":
meshCount += 1 # Count the meshes
mesh = NMesh.GetRaw(object.data.name)
for _face_ in mesh.faces: # loop faces
if _face_.image != None: # Must have an image before adding to cast.
if not str(_face_.image)[7:-1] in imageCast: # Don't double up on images
imageCast.append(str(_face_.image)[7:-1]) #Add image of the current face into image cast
textureCount = len(imageCast) # Count image for reporting
###########################################
# Make a list of material names (strings) #
###########################################
# WARNING! DOUBLE OR UNUSED MATERIALS WILL NOT BE REMOVED
materialCast = [] # A list fo material names (string)
# Search scene for materials.
# Find all textures in scene
for material in Material.get():
materialCast.append(str(material)[10:-1])
# Exit if over 255 materials
if len(materialCast) > 255:
sys.stdout=std
file.close()
print "You have over 255 materials, please remove some materilas before exporting to the MYFORMAT format."
materialCount = len(materialCast)
#####################################################
# Print Header and Number of textures and materials #
#####################################################
print "MYFORMATV"
print len(imageCast), len(materialCast),
##############################################
# Print all the textures to load into memory #
##############################################
for image in imageCast:
print "\n" + image, 1, 1, 0, # FIXME- Hard coded UWrap VWrap and double sided.
###########################################
# Print all materials and ther properties #
###########################################
# AmbientRed aGreen aBlue DifuseRed dG dB specRed sG sB EmitR eG eB Shinyness Alpha
for material in Material.get():
print '' # Formatting
print 0,0,0, # Ambient light governed globaly by world settings ,must find a fix.
print saneFloat(material.rgbCol[0]), #Diffuse light colour RGB
print saneFloat(material.rgbCol[1]),
print saneFloat(material.rgbCol[2]),
print saneFloat(material.specCol[0]),
print saneFloat(material.specCol[1]),
print saneFloat(material.specCol[2]),
print saneFloat(material.mirCol[0]), # Emit RGB - Use Mirror
print saneFloat(material.mirCol[1]),
print saneFloat(material.mirCol[2]),
print int(material.hard / 2.125 ) + 10, # Shinyness - hard # FIXME, this is between 10 and 120 a rough figure from docs.
print saneFloat(material.alpha),
###############################################
# We neet a parent where each mesh is a child #
###############################################
print '\nParent ', meshCount,
#############################################
# Verts and faces - a lot of properties too #
#############################################
for object in allObjects:
if object.getType() == "Mesh": # Only mesh objects
mesh = NMesh.GetRaw(object.data.name)
#######################################
# Count 3 sided faces and their verts #
#######################################
meshFaceCount = 0
meshVertCount = 0
for _face_ in mesh.faces:
if len(_face_.v) == 3:
meshFaceCount += 1
meshVertCount +=3
##########################################################################
# Geometry node, count faces and Verts (assume 'Geometry 0' NO CHILDREN) #
##########################################################################
print '\nGeometry 0 ', meshVertCount, meshFaceCount,
for _face_ in mesh.faces:
######################################
# Check face sanity (Only triangles) #
######################################
# 247 1928
#if len(_face_.v) == 4: # If a quad tell user and exit
# print "WARNING QUAD"
# print object.data.name
# print 3 / 0
if len(_face_.v) == 3: # Only use 3 vert faces
polyCount +=1 # Count the polygons
for vertIndex in range(len(_face_.v)): # Loop verts in face
print "" # New line for next vert
vertCount +=1 # Count the verts
if len(_face_.v) != 3:
print object.data.name
print 3 / 0
# This is outputting the mush with out transforming it (no size rot)
#print saneFloat(_face_.v[vertIndex].co[0]), # X
#print saneFloat(_face_.v[vertIndex].co[1]), # Y
#print saneFloat(_face_.v[vertIndex].co[2]), # Z
################################################
#Apply transfotmation while printing the verts #
################################################
transformadVert = apply_transform(_face_.v[vertIndex].co, object.matrix)
print saneFloat(transformadVert[0]),
print saneFloat(transformadVert[1]),
print saneFloat(transformadVert[2]),
# UV, if no uv then uv=0 0 else use real uv.
if _face_.uv != []:
print saneFloat(_face_.uv[vertIndex][0]),
print saneFloat(_face_.uv[vertIndex][1]),
else:
print 0, 0,
# RGB - If no colours then use defaults
if _face_.col != []:
print _face_.col[vertIndex].r, _face_.col[vertIndex].g, _face_.col[vertIndex].b, # Print rgb
else: # OK Make up a colour
print 255, 255, 255,
# export normals XYZ
print saneFloat(_face_.v[vertIndex].no[0]),
print saneFloat(_face_.v[vertIndex].no[1]),
print saneFloat(_face_.v[vertIndex].no[2]),
# Material INDEX
# This is tricky. We need to give the materialCast index for the current face...
# But we can only ask the face for its materialIndex within its mesh.
#print mesh.materials # _face_.materialIndex
#~ # Should not need to use a for loop in this, could fix later.
defMat = 0 # Use if none on vertex
matIndex = 0
for mat in mesh.materials:
if matIndex == _face_.materialIndex:
# Yes we have a match!!
defMat = 1
print materialCast.index(str(mat)[10:-1]), # Print the materialCast index for the current face
matIndex += 1
if defMat == 0: # Ok, no mat on vert, just use first mat. FIXME could have no first mat.
print 0,
# Now the faces
vertIndex = 0
for _face_ in mesh.faces:
if len(_face_.v) == 3: # Only use 3 vert faces
print ""
if _face_.image == None:
print -1,
else:
print imageCast.index(str(_face_.image)[7:-1]),
# IR MAT IT (Infra red data) FIXME - Hard coded, could use some other var
print 0,
print vertIndex,
vertIndex +=1
print vertIndex,
vertIndex +=1
print vertIndex,
vertIndex +=1
# Add a newline at the end of the file
print ''
# Close file
sys.stdout=std
file.close()
################
# Print report #
################
print "MYFORMAT file written with:"
print meshCount, " Mesh objects"
print vertCount, " Verts"
print polyCount, " Polygons"
print materialCount, "Materials"
print textureCount, "textures (Image files)"