4
Name: 'Wavefront (.obj)...'
7
Tooltip: 'Save a Wavefront OBJ File'
10
__author__ = "Campbell Barton, Jiri Hnidek"
11
__url__ = ["blender", "elysiun"]
15
This script is an exporter to OBJ file format.
19
Run this script from "File->Export" menu to export all meshes.
23
# --------------------------------------------------------------------------
24
# OBJ Export v1.0 by Campbell Barton (AKA Ideasman)
25
# --------------------------------------------------------------------------
26
# ***** BEGIN GPL LICENSE BLOCK *****
28
# This program is free software; you can redistribute it and/or
29
# modify it under the terms of the GNU General Public License
30
# as published by the Free Software Foundation; either version 2
31
# of the License, or (at your option) any later version.
33
# This program is distributed in the hope that it will be useful,
34
# but WITHOUT ANY WARRANTY; without even the implied warranty of
35
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36
# GNU General Public License for more details.
38
# You should have received a copy of the GNU General Public License
39
# along with this program; if not, write to the Free Software Foundation,
40
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42
# ***** END GPL LICENCE BLOCK *****
43
# --------------------------------------------------------------------------
47
from Blender import Mesh, Scene, Window, sys, Image, Draw
50
# Returns a tuple - path,extension.
51
# 'hello.obj' > ('hello', '.obj')
53
dotidx = path.rfind('.')
57
return path[:dotidx], path[dotidx:]
63
return name.replace(' ', '_')
65
# Used to add the scene name into the filename without using odd chars
66
def saneFilechars(name):
67
for ch in ' /\\~!@#$%^&*()+=[];\':",./<>?\t\r\n':
68
name = name.replace(ch, '_')
72
return min(a,b), max(a,b)
77
# (material.name, image.name):matname_imagename # matname_imagename has gaps removed.
80
def write_mtl(filename):
83
world = Blender.World.GetCurrent()
85
worldAmb = world.getAmb()
87
worldAmb = (0,0,0) # Default value
89
file = open(filename, "w")
90
file.write('# Blender3D MTL File: %s\n' % Blender.Get('filename').split('\\')[-1].split('/')[-1])
91
file.write('# Material Count: %i\n' % len(MTL_DICT))
92
# Write material/image combinations we have used.
93
for key, mtl_mat_name in MTL_DICT.iteritems():
95
# Get the Blender data for the material and the image.
96
# Having an image named None will make a bug, dont do it :)
98
file.write('newmtl %s\n' % mtl_mat_name) # Define a new material: matname_imgname
101
#write a dummy material here?
103
file.write('Ka %.6f %.6f %.6f\n' % tuple([c for c in worldAmb]) ) # Ambient, uses mirror colour,
104
file.write('Kd 0.8 0.8 0.8\n')
105
file.write('Ks 0.8 0.8 0.8\n')
106
file.write('d 1\n') # No alpha
107
file.write('illum 2\n') # light normaly
109
mat = Blender.Material.Get(key[0])
110
file.write('Ns %.6f\n' % ((mat.getHardness()-1) * 1.9607843137254901) ) # Hardness, convert blenders 1-511 to MTL's
111
file.write('Ka %.6f %.6f %.6f\n' % tuple([c*mat.getAmb() for c in worldAmb]) ) # Ambient, uses mirror colour,
112
file.write('Kd %.6f %.6f %.6f\n' % tuple([c*mat.getRef() for c in mat.getRGBCol()]) ) # Diffuse
113
file.write('Ks %.6f %.6f %.6f\n' % tuple([c*mat.getSpec() for c in mat.getSpecCol()]) ) # Specular
114
file.write('Ni %.6f\n' % mat.getIOR()) # Refraction index
115
file.write('d %.6f\n' % mat.getAlpha()) # Alpha (obj uses 'd' for dissolve)
117
# 0 to disable lighting, 1 for ambient & diffuse only (specular color set to black), 2 for full lighting.
118
if mat.getMode() & Blender.Material.Modes['SHADELESS']:
119
file.write('illum 0\n') # ignore lighting
120
elif mat.getSpec() == 0:
121
file.write('illum 1\n') # no specular.
123
file.write('illum 2\n') # light normaly
127
if key[1] != None: # We have an image on the face!
128
img = Image.Get(key[1])
129
file.write('map_Kd %s\n' % img.filename.split('\\')[-1].split('/')[-1]) # Diffuse mapping image
131
elif key[0] != None: # No face image. if we havea material search for MTex image.
132
for mtex in mat.getTextures():
133
if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
135
filename = mtex.tex.image.filename.split('\\')[-1].split('/')[-1]
136
file.write('map_Kd %s\n' % filename) # Diffuse mapping image
139
# Texture has no image though its an image type, best ignore.
146
def copy_file(source, dest):
147
file = open(source, 'rb')
151
file = open(dest, 'wb')
156
def copy_images(dest_dir):
157
if dest_dir[-1] != sys.sep:
160
# Get unique image names
162
for matname, imagename in MTL_DICT.iterkeys(): # Only use image name
164
if imagename != None:
165
uniqueImages[imagename] = None # Should use sets here. wait until Python 2.4 is default.
169
mat= Material.Get(matname)
170
for mtex in mat.getTextures():
171
if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
173
uniqueImages[mtex.tex.image.name] = None
180
for imageName in uniqueImages.iterkeys():
182
bImage = Image.Get(imageName)
183
image_path = sys.expandpath(bImage.filename)
184
if sys.exists(image_path):
185
# Make a name for the target path.
186
dest_image_path = dest_dir + image_path.split('\\')[-1].split('/')[-1]
187
if not sys.exists(dest_image_path): # Image isnt alredy there
188
print '\tCopying "%s" > "%s"' % (image_path, dest_image_path)
189
copy_file(image_path, dest_image_path)
191
print '\tCopied %d images' % copyCount
194
return round(v.x, 6), round(v.y, 6), round(v.z, 6)
197
return round(v.x, 6), round(v.y, 6)
199
def write(filename, objects,\
200
EXPORT_TRI=False, EXPORT_EDGES=False, EXPORT_NORMALS=False, EXPORT_NORMALS_HQ=False,\
201
EXPORT_UV=True, EXPORT_MTL=True, EXPORT_COPY_IMAGES=False,\
202
EXPORT_APPLY_MODIFIERS=True, EXPORT_BLEN_OBS=True,\
203
EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False):
205
Basic write function. The context and options must be alredy set
206
This can be accessed externaly
208
write( 'c:\\test\\foobar.obj', Blender.Object.GetSelected() ) # Using default options.
210
print 'OBJ Export path: "%s"' % filename
212
temp_mesh_name = '~tmp-mesh'
215
scn = Scene.GetCurrent()
217
file = open(filename, "w")
220
file.write('# Blender v%s OBJ File: %s\n' % (Blender.Get('version'), Blender.Get('filename').split('/')[-1].split('\\')[-1] ))
221
file.write('# www.blender3d.org\n')
223
# Tell the obj file what material file to use.
224
mtlfilename = '%s.mtl' % '.'.join(filename.split('.')[:-1])
225
file.write('mtllib %s\n' % ( mtlfilename.split('\\')[-1].split('/')[-1] ))
227
# Get the container mesh. - used for applying modifiers and non mesh objects.
228
containerMesh = meshName = tempMesh = None
229
for meshName in Blender.NMesh.GetNames():
230
if meshName.startswith(temp_mesh_name):
231
tempMesh = Mesh.Get(meshName)
232
if not tempMesh.users:
233
containerMesh = tempMesh
234
if not containerMesh:
235
containerMesh = Mesh.New(temp_mesh_name)
239
# Initialize totals, these are updated each object
240
totverts = totuvco = totno = 1
248
# Will work for non meshes now! :)
249
# getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=True, scn=None)
250
m= BPyMesh.getMeshFromObject(ob, containerMesh, True, False, scn)
255
# We have a valid mesh
257
# Add a dummy object to it.
258
oldmode = Mesh.Mode()
259
Mesh.Mode(Mesh.SelectModes['FACE'])
267
tempob = Blender.Object.New('Mesh')
270
m.quadToTriangle(0) # more=0 shortest length
271
oldmode = Mesh.Mode(oldmode)
275
faces = [ f for f in m.faces ]
277
edges = [ ed for ed in m.edges ]
281
if not (len(faces)+len(edges)): # Make sure there is somthing to write
282
continue # dont bother with this mesh.
284
m.transform(ob.matrix)
286
# High Quality Normals
287
if EXPORT_NORMALS and EXPORT_NORMALS_HQ:
288
BPyMesh.meshCalcNormals(m)
291
#materials = m.getMaterials(1) # 1 == will return None in the list.
292
materials = m.materials
296
for mat in materials:
298
materialNames.append(mat.name)
300
materialNames.append(None)
301
# Cant use LC because some materials are None.
302
# materialNames = map(lambda mat: mat.name, materials) # Bug Blender, dosent account for null materials, still broken.
304
# Possible there null materials, will mess up indicies
305
# but at least it will export, wait until Blender gets fixed.
306
materialNames.extend((16-len(materialNames)) * [None])
309
# Sort by Material, then images
310
# so we dont over context switch in the obj file.
311
if m.faceUV and EXPORT_UV:
312
faces.sort(lambda a,b: cmp((a.mat, a.image, a.smooth), (b.mat, b.image, b.smooth)))
314
faces.sort(lambda a,b: cmp((a.mat, a.smooth), (b.mat, b.smooth)))
317
# Set the default mat to no material and no image.
318
contextMat = (0, 0) # Can never be this, so we will label a new material teh first chance we get.
319
contextSmooth = None # Will either be true or false, set bad to force initialization switch.
321
if EXPORT_BLEN_OBS or EXPORT_GROUP_BY_OB:
322
obnamestring = '%s_%s' % (fixName(ob.name), fixName(ob.getData(1)))
324
file.write('o %s\n' % obnamestring) # Write Object name
325
else: # if EXPORT_GROUP_BY_OB:
326
file.write('g %s\n' % obnamestring)
330
file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
333
if m.faceUV and EXPORT_UV:
336
uvKey = veckey2d(uvKey)
337
if not globalUVCoords.has_key(uvKey):
338
globalUVCoords[uvKey] = totuvco
340
file.write('vt %.6f %.6f 0.0\n' % uvKey)
342
# NORMAL, Smooth/Non smoothed.
347
noKey = veckey3d(v.no)
348
if not globalNormals.has_key( noKey ):
349
globalNormals[noKey] = totno
351
file.write('vn %.6f %.6f %.6f\n' % noKey)
353
# Hard, 1 normal from the face.
354
noKey = veckey3d(f.no)
355
if not globalNormals.has_key( noKey ):
356
globalNormals[noKey] = totno
358
file.write('vn %.6f %.6f %.6f\n' % noKey)
365
if EXPORT_UV and m.faceUV and f.image: # Object is always true.
366
key = materialNames[min(f.mat,len(materialNames)-1)], f.image.name
367
#key = materialNames[f.mat], f.image.name
369
key = materialNames[min(f.mat,len(materialNames)-1)], None # No image, use None instead.
370
#key = materialNames[f.mat], None # No image, use None instead.
373
# CHECK FOR CONTEXT SWITCH
374
if key == contextMat:
375
pass # Context alredy switched, dont do anythoing
377
if key[0] == None and key[1] == None:
378
# Write a null material, since we know the context has changed.
380
file.write('usemtl (null)\n') # mat, image
383
try: # Faster to try then 2x dict lookups.
384
# We have the material, just need to write the context switch,
385
matstring = MTL_DICT[key]
389
# First add to global dict so we can export to mtl
392
# Make a new names from the mat and image name,
393
# converting any spaces to underscores with fixName.
395
# If none image dont bother adding it to the name
397
matstring = MTL_DICT[key] ='%s' % fixName(key[0])
399
matstring = MTL_DICT[key] = '%s_%s' % (fixName(key[0]), fixName(key[1]))
401
if EXPORT_GROUP_BY_MAT:
402
file.write('g %s_%s_%s\n' % (fixName(ob.name), fixName(ob.getData(1)), matstring) ) # can be mat_image or (null)
403
file.write('usemtl %s\n' % matstring) # can be mat_image or (null)
407
if f.smooth != contextSmooth:
411
file.write('s off\n')
412
contextSmooth = f.smooth
415
if m.faceUV and EXPORT_UV:
417
if f.smooth: # Smoothed, use vertex normals
418
for vi, v in enumerate(f_v):
419
file.write( ' %d/%d/%d' % (\
421
globalUVCoords[ veckey2d(f.uv[vi]) ],\
422
globalNormals[ veckey3d(v.no) ])) # vert, uv, normal
423
else: # No smoothing, face normals
424
no = globalNormals[ veckey3d(f.no) ]
425
for vi, v in enumerate(f_v):
426
file.write( ' %d/%d/%d' % (\
428
globalUVCoords[ veckey2d(f.uv[vi]) ],\
429
no)) # vert, uv, normal
432
for vi, v in enumerate(f_v):
433
file.write( ' %d/%d' % (\
435
globalUVCoords[ veckey2d(f.uv[vi])])) # vert, uv
440
if f.smooth: # Smoothed, use vertex normals
442
file.write( ' %d//%d' % (\
444
globalNormals[ veckey3d(v.no) ]))
445
else: # No smoothing, face normals
446
no = globalNormals[ veckey3d(f.no) ]
448
file.write( ' %d//%d' % (\
453
file.write( ' %d' % (\
462
for i in xrange(len(f_v)):
463
faceEdgeVKey = sortPair(f_v[i].index, f_v[i-1].index)
465
# We dont realy need to keep count. Just that a face uses it
467
edgeUsers[faceEdgeVKey] = 1
470
edgeVKey = sortPair(ed.v1.index, ed.v2.index)
471
if not edgeUsers.has_key(edgeVKey): # No users? Write the edge.
472
file.write('f %d %d\n' % (edgeVKey[0]+totverts, edgeVKey[1]+totverts))
474
# Make the indicies global rather then per mesh
475
totverts += len(m.verts)
480
# Now we have all our materials, save them
482
write_mtl(mtlfilename)
483
if EXPORT_COPY_IMAGES:
485
# Remove chars until we are just the path.
486
while dest_dir and dest_dir[-1] not in '\\/':
487
dest_dir = dest_dir[:-1]
489
copy_images(dest_dir)
491
print '\tError: "%s" could not be used as a base for an image path.' % filename
493
print "OBJ Export time: %.2f" % (sys.time() - time1)
497
def write_ui(filename):
499
for s in Window.GetScreenInfo():
500
Window.QHandle(s['id'])
502
EXPORT_APPLY_MODIFIERS = Draw.Create(1)
503
EXPORT_TRI = Draw.Create(0)
504
EXPORT_EDGES = Draw.Create(0)
505
EXPORT_NORMALS = Draw.Create(0)
506
EXPORT_NORMALS_HQ = Draw.Create(0)
507
EXPORT_UV = Draw.Create(1)
508
EXPORT_MTL = Draw.Create(1)
509
EXPORT_SEL_ONLY = Draw.Create(1)
510
EXPORT_ALL_SCENES = Draw.Create(0)
511
EXPORT_ANIMATION = Draw.Create(0)
512
EXPORT_COPY_IMAGES = Draw.Create(0)
513
EXPORT_BLEN_OBS = Draw.Create(1)
514
EXPORT_GROUP_BY_OB = Draw.Create(0)
515
EXPORT_GROUP_BY_MAT = Draw.Create(0)
520
('Mesh Options...'),\
521
('Apply Modifiers', EXPORT_APPLY_MODIFIERS, 'Use transformed mesh data from each object. May break vert order for morph targets.'),\
522
('Triangulate', EXPORT_TRI, 'Triangulate quadsModifiers.'),\
523
('Edges', EXPORT_EDGES, 'Edges not connected to faces.'),\
524
('Normals', EXPORT_NORMALS, 'Export vertex normal data (Ignored on import).'),\
525
('High Quality Normals', EXPORT_NORMALS_HQ, 'Calculate high quality normals for rendering.'),\
526
('UVs', EXPORT_UV, 'Export texface UV coords.'),\
527
('Materials', EXPORT_MTL, 'Write a separate MTL file with the OBJ.'),\
529
('Selection Only', EXPORT_SEL_ONLY, 'Only export objects in visible selection. Else export whole scene.'),\
530
('All Scenes', EXPORT_ALL_SCENES, 'Each scene as a seperate OBJ file.'),\
531
('Animation', EXPORT_ANIMATION, 'Each frame as a numbered OBJ file.'),\
532
('Copy Images', EXPORT_COPY_IMAGES, 'Copy image files to the export directory, never overwrite.'),\
534
('Objects', EXPORT_BLEN_OBS, 'Export blender objects as "OBJ objects".'),\
535
('Object Groups', EXPORT_GROUP_BY_OB, 'Export blender objects as "OBJ Groups".'),\
536
('Material Groups', EXPORT_GROUP_BY_MAT, 'Group by materials.'),\
539
if not Draw.PupBlock('Export...', pup_block):
544
EXPORT_APPLY_MODIFIERS = EXPORT_APPLY_MODIFIERS.val
545
EXPORT_TRI = EXPORT_TRI.val
546
EXPORT_EDGES = EXPORT_EDGES.val
547
EXPORT_NORMALS = EXPORT_NORMALS.val
548
EXPORT_NORMALS_HQ = EXPORT_NORMALS_HQ.val
549
EXPORT_UV = EXPORT_UV.val
550
EXPORT_MTL = EXPORT_MTL.val
551
EXPORT_SEL_ONLY = EXPORT_SEL_ONLY.val
552
EXPORT_ALL_SCENES = EXPORT_ALL_SCENES.val
553
EXPORT_ANIMATION = EXPORT_ANIMATION.val
554
EXPORT_COPY_IMAGES = EXPORT_COPY_IMAGES.val
555
EXPORT_BLEN_OBS = EXPORT_BLEN_OBS.val
556
EXPORT_GROUP_BY_OB = EXPORT_GROUP_BY_OB.val
557
EXPORT_GROUP_BY_MAT = EXPORT_GROUP_BY_MAT.val
561
base_name, ext = splitExt(filename)
562
context_name = [base_name, '', '', ext] # basename, scene_name, framenumber, extension
564
# Use the options to export the data using write()
565
# def write(filename, objects, EXPORT_EDGES=False, EXPORT_NORMALS=False, EXPORT_MTL=True, EXPORT_COPY_IMAGES=False, EXPORT_APPLY_MODIFIERS=True):
566
orig_scene = Scene.GetCurrent()
567
if EXPORT_ALL_SCENES:
568
export_scenes = Scene.Get()
570
export_scenes = [orig_scene]
573
for scn in export_scenes:
574
scn.makeCurrent() # If alredy current, this is not slow.
575
context = scn.getRenderingContext()
576
orig_frame = Blender.Get('curframe')
578
if EXPORT_ALL_SCENES: # Add scene name into the context_name
579
context_name[1] = '_%s' % saneFilechars(scn.name) # WARNING, its possible that this could cause a collision. we could fix if were feeling parranoied.
581
# Export an animation?
583
scene_frames = range(context.startFrame(), context.endFrame()+1) # up to and including the end frame.
585
scene_frames = [orig_frame] # Dont export an animation.
587
# Loop through all frames in the scene and export.
588
for frame in scene_frames:
589
if EXPORT_ANIMATION: # Add frame to the filename.
590
context_name[2] = '_%.6d' % frame
592
Blender.Set('curframe', frame)
594
export_objects = Blender.Object.GetSelected() # Export Context
596
export_objects = scn.getChildren()
599
write(''.join(context_name), export_objects,\
600
EXPORT_TRI, EXPORT_EDGES, EXPORT_NORMALS,\
601
EXPORT_NORMALS_HQ, EXPORT_UV, EXPORT_MTL,\
602
EXPORT_COPY_IMAGES, EXPORT_APPLY_MODIFIERS,\
603
EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT)
605
Blender.Set('curframe', orig_frame)
607
# Restore old active scene.
608
orig_scene.makeCurrent()
612
if __name__ == '__main__':
613
Window.FileSelector(write_ui, 'Export Wavefront OBJ', sys.makename(ext='.obj'))