2004-06-21 12:01:23 +00:00
|
|
|
#!BPY
|
2004-06-07 01:34:15 +00:00
|
|
|
|
2004-06-21 12:01:23 +00:00
|
|
|
"""
|
|
|
|
Name: 'Wavefront (.obj)...'
|
|
|
|
Blender: 232
|
2004-06-08 04:43:40 +00:00
|
|
|
Group: 'Export'
|
2004-06-21 12:01:23 +00:00
|
|
|
Tooltip: 'Save a Wavefront OBJ File'
|
|
|
|
"""
|
2004-06-07 01:34:15 +00:00
|
|
|
|
2004-06-21 12:01:23 +00:00
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
# OBJ Export v0.9 by Campbell Barton (AKA Ideasman)
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ***** END GPL LICENCE BLOCK *****
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#==================================================#
|
|
|
|
# New name based on old with a different extension #
|
|
|
|
#==================================================#
|
|
|
|
def newFName(ext):
|
|
|
|
return Get('filename')[: -len(Get('filename').split('.', -1)[-1]) ] + ext
|
|
|
|
|
|
|
|
|
|
|
|
#===============================================#
|
|
|
|
# Strips the slashes from the front of a string #
|
|
|
|
#===============================================#
|
|
|
|
def stripPath(path):
|
|
|
|
for CH in range(len(path), 0, -1):
|
|
|
|
if path[CH-1] == "/" or path[CH-1] == "\\":
|
|
|
|
path = path[CH:]
|
2004-09-19 10:41:04 +00:00
|
|
|
break
|
2004-06-21 12:01:23 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
#==================#
|
|
|
|
# Apply Transform #
|
|
|
|
#==================#
|
|
|
|
def apply_transform(vert, matrix):
|
|
|
|
vertCopy = Mathutils.CopyVec(vert)
|
|
|
|
vertCopy.resize4D()
|
|
|
|
return Mathutils.VecMultMat(vertCopy, matrix)
|
|
|
|
|
|
|
|
from Blender import *
|
|
|
|
|
|
|
|
NULL_MAT = '(null)'
|
|
|
|
NULL_IMG = '(null)'
|
|
|
|
|
|
|
|
def save_mtl(filename):
|
|
|
|
file = open(filename, "w")
|
|
|
|
for mat in Material.Get():
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('newmtl %s\n' % (mat.getName())) # Define a new material
|
|
|
|
|
|
|
|
# Hardness, convert blenders 1-511 to MTL's
|
|
|
|
file.write('Ns %s\n' % ((mat.getHardness()-1) * 1.9607843137254901 ) )
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
col = mat.getRGBCol()
|
2004-09-19 10:41:04 +00:00
|
|
|
# Diffuse
|
|
|
|
file.write('Kd %s %s %s\n' % (col[0], col[1], col[2]))
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
col = mat.getMirCol()
|
2004-09-19 10:41:04 +00:00
|
|
|
# Ambient, uses mirror colour,
|
|
|
|
file.write('Ka %s %s %s\n' % (col[0], col[1], col[2]))
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
col = mat.getSpecCol()
|
2004-09-19 10:41:04 +00:00
|
|
|
# Specular
|
|
|
|
file.write('Ks %s %s %s\n' % (col[0],col[1], col[2]))
|
|
|
|
|
|
|
|
# Alpha (dissolve)
|
|
|
|
file.write('d %s\n' % (mat.getAlpha()))
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
# illum, 0 to disable lightng, 2 is normal.
|
|
|
|
if mat.getMode() & Material.Modes['SHADELESS']:
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('illum 0\n') # ignore lighting
|
2004-06-21 12:01:23 +00:00
|
|
|
else:
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('illum 2\n') # light normaly
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
# End OF Mat
|
|
|
|
file.write('\n') # new line
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
def save_obj(filename):
|
|
|
|
time1 = sys.time()
|
|
|
|
# First output all material
|
|
|
|
mtlfilename = filename[:-4] + '.mtl'
|
|
|
|
save_mtl(mtlfilename)
|
2004-06-21 12:01:23 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
file = open(filename, "w")
|
2004-06-21 12:01:23 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Write Header
|
|
|
|
file.write('# Blender OBJ File: %s\n' % (Get('filename')))
|
|
|
|
file.write('# www.blender.org\n')
|
|
|
|
|
|
|
|
# Tell the obj file what material file to use.
|
|
|
|
file.write('mtllib %s\n' % (stripPath(mtlfilename)))
|
|
|
|
|
|
|
|
# Initialize totals, these are updated each object
|
|
|
|
totverts = totuvco = 0
|
|
|
|
|
|
|
|
# Get all meshs
|
|
|
|
for ob in Object.Get():
|
|
|
|
if ob.getType() != 'Mesh':
|
|
|
|
continue
|
|
|
|
m = NMesh.GetRawFromObject(ob.name)
|
|
|
|
|
|
|
|
# remove any edges, is not written back to the mesh so its not going to
|
|
|
|
# modify the open file.
|
|
|
|
for f in m.faces:
|
|
|
|
if len(f.v) < 3:
|
|
|
|
mesh.faces.remove(f)
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
if len(m.faces) == 0: # Make sure there is somthing to write.
|
|
|
|
continue #dont bother with this mesh.
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Set the default mat
|
|
|
|
currentMatName = NULL_MAT
|
|
|
|
currentImgName = NULL_IMG
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
#file.write('o ' + ob.getName() + '_' + m.name + '\n') # Write Object name
|
|
|
|
file.write('o %s_%s\n' % (ob.getName(), m.name)) # Write Object name
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Works 100% Yay
|
|
|
|
matrix = ob.getMatrix('worldspace')
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Vert
|
|
|
|
for v in m.verts:
|
|
|
|
# Transform the vert
|
|
|
|
vTx = apply_transform(v.co, matrix)
|
|
|
|
file.write('v %s %s %s\n' % (vTx[0], vTx[1], vTx[2]))
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# UV
|
|
|
|
for f in m.faces:
|
|
|
|
for uvIdx in range(len(f.v)):
|
|
|
|
if f.uv:
|
|
|
|
file.write('vt %s %s 0.0\n' % (f.uv[uvIdx][0], f.uv[uvIdx][1]))
|
|
|
|
else:
|
|
|
|
file.write('vt 0.0 0.0 0.0\n')
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# NORMAL
|
|
|
|
for f1 in m.faces:
|
|
|
|
for v in f1.v:
|
|
|
|
# Transform the normal
|
|
|
|
noTx = apply_transform(v.no, matrix)
|
|
|
|
noTx.normalize()
|
|
|
|
file.write('vn %s %s %s\n' % (noTx[0], noTx[1], noTx[2]))
|
|
|
|
|
|
|
|
uvIdx = 0
|
|
|
|
for f in m.faces:
|
|
|
|
# Check material and change if needed.
|
|
|
|
if len(m.materials) > f.mat:
|
|
|
|
if currentMatName != m.materials[f.mat].getName():
|
|
|
|
currentMatName = m.materials[f.mat].getName()
|
|
|
|
file.write('usemtl %s\n' % (currentMatName))
|
|
|
|
|
|
|
|
elif currentMatName != NULL_MAT:
|
|
|
|
currentMatName = NULL_MAT
|
|
|
|
file.write('usemtl %s\n' % (currentMatName))
|
|
|
|
|
|
|
|
# UV IMAGE
|
|
|
|
# If the face uses a different image from the one last set then add a usemap line.
|
|
|
|
if f.image:
|
|
|
|
if f.image.filename != currentImgName:
|
|
|
|
currentImgName = f.image.filename
|
|
|
|
# Set a new image for all following faces
|
|
|
|
file.write( 'usemat %s\n' % (stripPath(currentImgName)))
|
2004-08-04 06:16:46 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
elif currentImgName != NULL_IMG: # Not using an image so set to NULL_IMG
|
|
|
|
currentImgName = NULL_IMG
|
|
|
|
# Set a new image for all following faces
|
|
|
|
file.write( 'usemat %s\n' % (stripPath(currentImgName)))
|
|
|
|
|
|
|
|
file.write('f ')
|
|
|
|
for v in f.v:
|
|
|
|
file.write( '%s/%s/%s ' % (m.verts.index(v) + totverts+1, uvIdx+totuvco+1, uvIdx+totuvco+1))
|
|
|
|
|
|
|
|
uvIdx+=1
|
|
|
|
file.write('\n')
|
|
|
|
|
|
|
|
# Make the indicies global rather then per mesh
|
|
|
|
totverts += len(m.verts)
|
|
|
|
totuvco += uvIdx
|
|
|
|
file.close()
|
|
|
|
print "obj export time: ", sys.time() - time1
|
2004-08-04 06:16:46 +00:00
|
|
|
|
|
|
|
Window.FileSelector(save_obj, 'Export Wavefront OBJ', newFName('obj'))
|