Writing an export script

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

ideasman
Posts: 1000
Joined: Tue Feb 25, 2003 2:37 pm

Writing an export script

Postby ideasman » Sat Jun 28, 2003 11:30 am

Hi I am writing an export script for a company's own format.

I want to know if there is anything that should not be used in new scripts (NMesh?)

cekuhnen
Posts: 303
Joined: Mon Jan 13, 2003 11:04 pm

...

Postby cekuhnen » Sat Jun 28, 2003 6:49 pm

hey

that might be a bit uncommon but could you write a script to export into dxf with 3dfaces? i am on os x and blender 2.27.
all older scripts do not work at all. i only need a click an object and export it. so no gui is needed!

or do you have a doc how to do it? i have mh not realy experience with python.

eicke

ray_theray
Posts: 49
Joined: Wed Oct 16, 2002 3:05 am
Location: NM, US
Contact:

Postby ray_theray » Sat Jun 28, 2003 9:40 pm

If anything, avoid the Mesh module. It was started as NaN went bankrupt, and never really finished and as I understand it is getting scrapped completely. Simply use the NMesh module instead. There are also plenty of export scripts floating around that you can look at (povanim, yable, etc.). Good luck!
-ray_theray

ideasman
Posts: 1000
Joined: Tue Feb 25, 2003 2:37 pm

Re: ...

Postby ideasman » Sun Jun 29, 2003 2:51 am

cekuhnen wrote:hey

that might be a bit uncommon but could you write a script to export into dxf with 3dfaces? i am on os x and blender 2.27.
all older scripts do not work at all. i only need a click an object and export it. so no gui is needed!

or do you have a doc how to do it? i have mh not realy experience with python.

eicke


Hi fellow blender user. A basic export script is not that hard to do, I had never programed in blender before and I got one running an an afternoon.

Im getting paid for this and I have a lot of work at the moment, I would recomend learning python (Love it) and then changing someones export script to write a dxf

If you want I can sent you some basic examples.

Mabe sombody bould give you better advice since I don't know the dxf format.

cekuhnen
Posts: 303
Joined: Mon Jan 13, 2003 11:04 pm

...

Postby cekuhnen » Mon Jul 07, 2003 6:04 pm

that would be very kind if you could send me a basic script.
i looked into few scripts but understood not realy a lot.

dxf or obj import export is basicly all i need. no gui or extras. i only need to transfer the geometry.

there are many scripts but no one runs on the blender version for os x. even on pc only in blender 2.23 most scripts work but tehre is no 2.23 for os x. and that is my problem!


eicke

ideasman
Posts: 1000
Joined: Tue Feb 25, 2003 2:37 pm

Postby ideasman » Tue Jul 08, 2003 12:17 am

# 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)"


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests