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)...'
|
2005-06-13 17:21:30 +00:00
|
|
|
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-12-12 13:42:49 +00:00
|
|
|
__author__ = "Campbell Barton, Jiri Hnidek"
|
2004-11-07 16:31:13 +00:00
|
|
|
__url__ = ["blender", "elysiun"]
|
|
|
|
__version__ = "0.9"
|
|
|
|
|
|
|
|
__bpydoc__ = """\
|
|
|
|
This script is an exporter to OBJ file format.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
Run this script from "File->Export" menu to export all meshes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2004-06-21 12:01:23 +00:00
|
|
|
# --------------------------------------------------------------------------
|
2005-08-01 03:06:24 +00:00
|
|
|
# OBJ Export v0.9b by Campbell Barton (AKA Ideasman)
|
2004-06-21 12:01:23 +00:00
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
# ***** 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):
|
2005-05-30 02:26:40 +00:00
|
|
|
return Get('filename')[: -len(Get('filename').split('.', -1)[-1]) ] + ext
|
|
|
|
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
from Blender import *
|
|
|
|
|
|
|
|
NULL_MAT = '(null)'
|
2005-05-30 02:26:40 +00:00
|
|
|
NULL_IMG = '(null)' # from docs at http://astronomy.swin.edu.au/~pbourke/geomformats/obj/ also could be 'off'
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
def save_mtl(filename):
|
|
|
|
file = open(filename, "w")
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('# Blender MTL File: %s\n' % (Get('filename')))
|
2004-06-21 12:01:23 +00:00
|
|
|
for mat in Material.Get():
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('newmtl %s\n' % (mat.getName())) # Define a new material
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('Ns %s\n' % ((mat.getHardness()-1) * 1.9607843137254901 ) ) # Hardness, convert blenders 1-511 to MTL's
|
|
|
|
file.write('Kd %.6f %.6f %.6f\n' % tuple(mat.getRGBCol())) # Diffuse
|
|
|
|
file.write('Ka %.6f %.6f %.6f\n' % tuple(mat.getMirCol())) # Ambient, uses mirror colour,
|
|
|
|
file.write('Ks %.6f %.6f %.6f\n' % tuple(mat.getSpecCol())) # Specular
|
2005-08-01 03:06:24 +00:00
|
|
|
file.write('Ni %.6f\n' % mat.getIOR()) # Refraction index
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('d %.6f\n' % mat.getAlpha()) # Alpha (obj uses 'd' for dissolve)
|
2004-06-21 12:01:23 +00:00
|
|
|
|
|
|
|
# illum, 0 to disable lightng, 2 is normal.
|
|
|
|
if mat.getMode() & Material.Modes['SHADELESS']:
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('illum 0\n\n') # ignore lighting
|
2004-06-21 12:01:23 +00:00
|
|
|
else:
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('illum 2\n\n') # light normaly
|
2004-06-21 12:01:23 +00:00
|
|
|
file.close()
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
def save_obj(filename):
|
|
|
|
time1 = sys.time()
|
2005-05-30 02:26:40 +00:00
|
|
|
scn = Scene.GetCurrent()
|
2004-09-19 10:41:04 +00:00
|
|
|
# First output all material
|
2005-05-30 02:26:40 +00:00
|
|
|
mtlfilename = '%s.mtl' % '.'.join(filename.split('.')[:-1])
|
2004-09-19 10:41:04 +00:00
|
|
|
save_mtl(mtlfilename)
|
2004-06-21 12:01:23 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
file = open(filename, "w")
|
2005-08-01 03:06:24 +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.
|
2005-05-30 02:26:40 +00:00
|
|
|
file.write('mtllib %s\n' % ( mtlfilename.split('\\')[-1].split('/')[-1] ))
|
2004-09-19 10:41:04 +00:00
|
|
|
|
|
|
|
# Initialize totals, these are updated each object
|
|
|
|
totverts = totuvco = 0
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2005-08-01 03:06:24 +00:00
|
|
|
globalUVCoords = {}
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Get all meshs
|
2005-05-30 02:26:40 +00:00
|
|
|
for ob in scn.getChildren():
|
2004-09-19 10:41:04 +00:00
|
|
|
if ob.getType() != 'Mesh':
|
|
|
|
continue
|
|
|
|
m = NMesh.GetRawFromObject(ob.name)
|
2005-05-30 02:26:40 +00:00
|
|
|
m.transform(ob.matrix)
|
|
|
|
|
|
|
|
if not m.faces: # Make sure there is somthing to write
|
2004-09-19 10:41:04 +00:00
|
|
|
continue #dont bother with this mesh.
|
2005-08-01 03:06:24 +00:00
|
|
|
|
|
|
|
faces = [ f for f in m.faces if len(f) > 2 ]
|
|
|
|
materials = m.materials
|
|
|
|
|
|
|
|
# Sort by Material so we dont over context switch in the obj file.
|
|
|
|
if len(materials) > 1:
|
|
|
|
faces.sort(lambda a,b: cmp(a.mat, b.mat))
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Set the default mat
|
|
|
|
currentMatName = NULL_MAT
|
|
|
|
currentImgName = NULL_IMG
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('o %s_%s\n' % (ob.getName(), m.name)) # Write Object name
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Vert
|
|
|
|
for v in m.verts:
|
2005-06-13 17:21:30 +00:00
|
|
|
file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# UV
|
2005-08-01 03:06:24 +00:00
|
|
|
if m.hasFaceUV():
|
|
|
|
for f in faces:
|
|
|
|
for uv in f.uv:
|
|
|
|
uvKey = '%.6f %.6f' % uv
|
|
|
|
try:
|
|
|
|
dummy = globalUVCoords[uvKey]
|
|
|
|
except KeyError:
|
|
|
|
totuvco +=1 # 1 based index.
|
|
|
|
globalUVCoords[uvKey] = totuvco
|
|
|
|
file.write('vt %s 0.0\n' % uvKey)
|
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# NORMAL
|
2005-08-01 03:06:24 +00:00
|
|
|
for v in m.verts:
|
|
|
|
file.write('vn %.6f %.6f %.6f\n' % tuple(v.no))
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
uvIdx = 0
|
2005-08-01 03:06:24 +00:00
|
|
|
for f in faces:
|
2004-09-19 10:41:04 +00:00
|
|
|
# Check material and change if needed.
|
2005-08-01 03:06:24 +00:00
|
|
|
if len(materials) > 0:
|
|
|
|
if currentMatName != materials[f.mat].getName():
|
|
|
|
currentMatName = materials[f.mat].getName()
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('usemtl %s\n' % (currentMatName))
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
elif currentMatName != NULL_MAT:
|
|
|
|
currentMatName = NULL_MAT
|
|
|
|
file.write('usemtl %s\n' % (currentMatName))
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# 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
|
BPython bug fixes:
- #2646 reported by Campbell: Python/Fileselector (moving from fileselector called by script to another space caused script to hang around open but not accessible)
http://projects.blender.org/tracker/?func=detail&atid=125&aid=2646&group_id=9
- #2676 reported by Wim Van Hoydonck: 2.37 python scripts gui: event 8 ignored (thanks Ton for discussing / pointing what to do, Ken Hughes for also working on a fix)
http://projects.blender.org/tracker/?func=detail&atid=125&aid=2676&group_id=9
- gui-less scripts with calls to progress bar inside fileselector callbacks didn't return to the previous space on exit (staying on Scripts win), requiring an event to do so (mouse movement, for example). Quick fix for now, will rework a little after 2.37a for a better alternative, not needing to move to the Scripts win at all.
- added syntax colors access to Window.Theme module.
Scripts:
- updates by Jean-Michel Soler: svg2obj (svg paths import), tex2uvbaker, fixfromarmature;
- updates by Campbell Barton: obj import / export, console;
- tiny: converted vrml97 export to unix line endings;
- updates in ac3d exporter, help browser, save theme.
Thanks all mentioned above.
2005-06-11 05:30:14 +00:00
|
|
|
file.write( 'usemap %s\n' % currentImgName.split('\\')[-1].split('/')[-1] )
|
2005-05-30 02:26:40 +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
|
2005-08-01 03:06:24 +00:00
|
|
|
# Set a ne w image for all following faces
|
2005-05-30 02:26:40 +00:00
|
|
|
file.write( 'usemap %s\n' % currentImgName) # No splitting needed.
|
2005-08-01 03:06:24 +00:00
|
|
|
|
|
|
|
file.write('f')
|
|
|
|
if m.hasFaceUV():
|
|
|
|
for vi, v in enumerate(f.v):
|
|
|
|
uvIdx = globalUVCoords[ '%.6f %.6f' % f.uv[vi] ]
|
|
|
|
i = v.index + totverts + 1
|
|
|
|
file.write( ' %d/%d/%d' % (i, uvIdx, i)) # vert, uv, normal
|
|
|
|
|
|
|
|
else: # No UV's
|
|
|
|
for v in f.v:
|
|
|
|
file.write( ' %d' % (v.index + totverts+1))
|
2004-09-19 10:41:04 +00:00
|
|
|
file.write('\n')
|
2005-05-30 02:26:40 +00:00
|
|
|
|
2004-09-19 10:41:04 +00:00
|
|
|
# Make the indicies global rather then per mesh
|
|
|
|
totverts += len(m.verts)
|
|
|
|
file.close()
|
2005-06-13 17:21:30 +00:00
|
|
|
print "obj export time: %.2f" % (sys.time() - time1)
|
2004-08-04 06:16:46 +00:00
|
|
|
|
|
|
|
Window.FileSelector(save_obj, 'Export Wavefront OBJ', newFName('obj'))
|