~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to release/scripts/obj_export.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!BPY
2
 
 
3
 
"""
4
 
Name: 'Wavefront (.obj)...'
5
 
Blender: 232
6
 
Group: 'Export'
7
 
Tooltip: 'Save a Wavefront OBJ File'
8
 
"""
9
 
 
10
 
__author__ = "Campbell Barton, Jiri Hnidek"
11
 
__url__ = ["blender", "elysiun"]
12
 
__version__ = "1.0"
13
 
 
14
 
__bpydoc__ = """\
15
 
This script is an exporter to OBJ file format.
16
 
 
17
 
Usage:
18
 
 
19
 
Run this script from "File->Export" menu to export all meshes.
20
 
"""
21
 
 
22
 
 
23
 
# --------------------------------------------------------------------------
24
 
# OBJ Export v1.0 by Campbell Barton (AKA Ideasman)
25
 
# --------------------------------------------------------------------------
26
 
# ***** BEGIN GPL LICENSE BLOCK *****
27
 
#
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.
32
 
#
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.
37
 
#
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.
41
 
#
42
 
# ***** END GPL LICENCE BLOCK *****
43
 
# --------------------------------------------------------------------------
44
 
 
45
 
 
46
 
import Blender
47
 
from Blender import Mesh, Scene, Window, sys, Image, Draw
48
 
import BPyMesh
49
 
 
50
 
# Returns a tuple - path,extension.
51
 
# 'hello.obj' >  ('hello', '.obj')
52
 
def splitExt(path):
53
 
        dotidx = path.rfind('.')
54
 
        if dotidx == -1:
55
 
                return path, ''
56
 
        else:
57
 
                return path[:dotidx], path[dotidx:] 
58
 
 
59
 
def fixName(name):
60
 
        if name == None:
61
 
                return 'None'
62
 
        else:
63
 
                return name.replace(' ', '_')
64
 
 
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, '_')
69
 
        return name
70
 
 
71
 
def sortPair(a,b):
72
 
        return min(a,b), max(a,b)
73
 
 
74
 
global MTL_DICT
75
 
 
76
 
# A Dict of Materials
77
 
# (material.name, image.name):matname_imagename # matname_imagename has gaps removed.
78
 
MTL_DICT = {} 
79
 
 
80
 
def write_mtl(filename):
81
 
        global MTL_DICT
82
 
        
83
 
        world = Blender.World.GetCurrent()
84
 
        if world:
85
 
                worldAmb = world.getAmb()
86
 
        else:
87
 
                worldAmb = (0,0,0) # Default value
88
 
        
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():
94
 
                
95
 
                # Get the Blender data for the material and the image.
96
 
                # Having an image named None will make a bug, dont do it :)
97
 
                
98
 
                file.write('newmtl %s\n' % mtl_mat_name) # Define a new material: matname_imgname
99
 
                
100
 
                if key[0] == None:
101
 
                        #write a dummy material here?
102
 
                        file.write('Ns 0\n')
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 
108
 
                else:
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)
116
 
                        
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.
122
 
                        else:
123
 
                                file.write('illum 2\n') # light normaly 
124
 
                
125
 
                
126
 
                # Write images!
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                 
130
 
                
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:
134
 
                                        try:
135
 
                                                filename = mtex.tex.image.filename.split('\\')[-1].split('/')[-1]
136
 
                                                file.write('map_Kd %s\n' % filename) # Diffuse mapping image
137
 
                                                break
138
 
                                        except:
139
 
                                                # Texture has no image though its an image type, best ignore.
140
 
                                                pass
141
 
                
142
 
                file.write('\n\n')
143
 
        
144
 
        file.close()
145
 
 
146
 
def copy_file(source, dest):
147
 
        file = open(source, 'rb')
148
 
        data = file.read()
149
 
        file.close()
150
 
        
151
 
        file = open(dest, 'wb')
152
 
        file.write(data)
153
 
        file.close()
154
 
 
155
 
 
156
 
def copy_images(dest_dir):
157
 
        if dest_dir[-1] != sys.sep:
158
 
                dest_dir += sys.sep
159
 
        
160
 
        # Get unique image names
161
 
        uniqueImages = {}
162
 
        for matname, imagename in MTL_DICT.iterkeys(): # Only use image name
163
 
                # Get Texface images
164
 
                if imagename != None:
165
 
                        uniqueImages[imagename] = None # Should use sets here. wait until Python 2.4 is default.
166
 
                
167
 
                # Get MTex images
168
 
                if matname != None:
169
 
                        mat= Material.Get(matname)
170
 
                        for mtex in mat.getTextures():
171
 
                                if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
172
 
                                        try:
173
 
                                                uniqueImages[mtex.tex.image.name] = None
174
 
                                        except:
175
 
                                                pass
176
 
        
177
 
        # Now copy images
178
 
        copyCount = 0
179
 
        
180
 
        for imageName in uniqueImages.iterkeys():
181
 
                print imageName
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)
190
 
                                copyCount+=1
191
 
        print '\tCopied %d images' % copyCount
192
 
 
193
 
def veckey3d(v):
194
 
        return round(v.x, 6), round(v.y, 6), round(v.z, 6)
195
 
 
196
 
def veckey2d(v):
197
 
        return round(v.x, 6), round(v.y, 6)
198
 
 
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):
204
 
        '''
205
 
        Basic write function. The context and options must be alredy set
206
 
        This can be accessed externaly
207
 
        eg.
208
 
        write( 'c:\\test\\foobar.obj', Blender.Object.GetSelected() ) # Using default options.
209
 
        '''
210
 
        print 'OBJ Export path: "%s"' % filename
211
 
        global MTL_DICT
212
 
        temp_mesh_name = '~tmp-mesh'
213
 
 
214
 
        time1 = sys.time()
215
 
        scn = Scene.GetCurrent()
216
 
 
217
 
        file = open(filename, "w")
218
 
        
219
 
        # Write Header
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')
222
 
 
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] ))
226
 
        
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)
236
 
        del meshName
237
 
        del tempMesh    
238
 
        
239
 
        # Initialize totals, these are updated each object
240
 
        totverts = totuvco = totno = 1
241
 
        
242
 
        globalUVCoords = {}
243
 
        globalNormals = {}
244
 
        
245
 
        # Get all meshs
246
 
        for ob in objects:
247
 
                
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)
251
 
                
252
 
                if not m:
253
 
                        continue
254
 
                
255
 
                # We have a valid mesh
256
 
                if EXPORT_TRI:
257
 
                        # Add a dummy object to it.
258
 
                        oldmode = Mesh.Mode()
259
 
                        Mesh.Mode(Mesh.SelectModes['FACE'])
260
 
                        quadcount = 0
261
 
                        for f in m.faces:
262
 
                                if len(f) == 4:
263
 
                                        f.sel = True
264
 
                                        quadcount +=1
265
 
                        
266
 
                        if quadcount:
267
 
                                tempob = Blender.Object.New('Mesh')
268
 
                                tempob.link(m)
269
 
                                scn.link(tempob)
270
 
                                m.quadToTriangle(0) # more=0 shortest length
271
 
                                oldmode = Mesh.Mode(oldmode)
272
 
                                scn.unlink(tempob)
273
 
                        Mesh.Mode(oldmode)
274
 
                
275
 
                faces = [ f for f in m.faces ]
276
 
                if EXPORT_EDGES:
277
 
                        edges = [ ed for ed in m.edges ]
278
 
                else:
279
 
                        edges = []
280
 
                        
281
 
                if not (len(faces)+len(edges)): # Make sure there is somthing to write
282
 
                        continue # dont bother with this mesh.
283
 
                
284
 
                m.transform(ob.matrix)
285
 
                
286
 
                # High Quality Normals
287
 
                if EXPORT_NORMALS and EXPORT_NORMALS_HQ:
288
 
                        BPyMesh.meshCalcNormals(m)
289
 
                
290
 
                # # Crash Blender
291
 
                #materials = m.getMaterials(1) # 1 == will return None in the list.
292
 
                materials = m.materials
293
 
                
294
 
                materialNames = []
295
 
                if materials:
296
 
                        for mat in materials:
297
 
                                if mat: # !=None
298
 
                                        materialNames.append(mat.name)
299
 
                                else:
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.  
303
 
                
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])
307
 
                
308
 
                
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)))
313
 
                else:
314
 
                        faces.sort(lambda a,b: cmp((a.mat, a.smooth), (b.mat, b.smooth)))
315
 
                
316
 
                
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.
320
 
                
321
 
                if EXPORT_BLEN_OBS or EXPORT_GROUP_BY_OB:
322
 
                        obnamestring = '%s_%s' % (fixName(ob.name), fixName(ob.getData(1)))
323
 
                        if EXPORT_BLEN_OBS:
324
 
                                file.write('o %s\n' % obnamestring) # Write Object name
325
 
                        else: # if EXPORT_GROUP_BY_OB:
326
 
                                file.write('g %s\n' % obnamestring)
327
 
                        
328
 
                # Vert
329
 
                for v in m.verts:
330
 
                        file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
331
 
                
332
 
                # UV
333
 
                if m.faceUV and EXPORT_UV:
334
 
                        for f in faces:
335
 
                                for uvKey in f.uv:
336
 
                                        uvKey = veckey2d(uvKey)
337
 
                                        if not globalUVCoords.has_key(uvKey):
338
 
                                                globalUVCoords[uvKey] = totuvco
339
 
                                                totuvco +=1
340
 
                                                file.write('vt %.6f %.6f 0.0\n' % uvKey)
341
 
                
342
 
                # NORMAL, Smooth/Non smoothed.
343
 
                if EXPORT_NORMALS:
344
 
                        for f in faces:
345
 
                                if f.smooth:
346
 
                                        for v in f.v:
347
 
                                                noKey = veckey3d(v.no)
348
 
                                                if not globalNormals.has_key( noKey ):
349
 
                                                        globalNormals[noKey] = totno
350
 
                                                        totno +=1
351
 
                                                        file.write('vn %.6f %.6f %.6f\n' % noKey)
352
 
                                else:
353
 
                                        # Hard, 1 normal from the face.
354
 
                                        noKey = veckey3d(f.no)
355
 
                                        if not globalNormals.has_key( noKey ):
356
 
                                                globalNormals[noKey] = totno
357
 
                                                totno +=1
358
 
                                                file.write('vn %.6f %.6f %.6f\n' % noKey)
359
 
                
360
 
                
361
 
                uvIdx = 0
362
 
                for f in faces:
363
 
                        f_v= f.v
364
 
                        # MAKE KEY
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
368
 
                        else:
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.
371
 
                                
372
 
                        
373
 
                        # CHECK FOR CONTEXT SWITCH
374
 
                        if key == contextMat:
375
 
                                pass # Context alredy switched, dont do anythoing
376
 
                        else:
377
 
                                if key[0] == None and key[1] == None:
378
 
                                        # Write a null material, since we know the context has changed.
379
 
                                        matstring = '(null)'
380
 
                                        file.write('usemtl (null)\n') # mat, image
381
 
                                        
382
 
                                else:
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]
386
 
                                                
387
 
                                                
388
 
                                        except KeyError:
389
 
                                                # First add to global dict so we can export to mtl
390
 
                                                # Then write mtl
391
 
                                                
392
 
                                                # Make a new names from the mat and image name,
393
 
                                                # converting any spaces to underscores with fixName.
394
 
                                                
395
 
                                                # If none image dont bother adding it to the name
396
 
                                                if key[1] == None:
397
 
                                                        matstring = MTL_DICT[key] ='%s' % fixName(key[0])
398
 
                                                else:
399
 
                                                        matstring = MTL_DICT[key] = '%s_%s' % (fixName(key[0]), fixName(key[1]))
400
 
                                
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)
404
 
                                
405
 
                        contextMat = key
406
 
                        
407
 
                        if f.smooth != contextSmooth:
408
 
                                if f.smooth:
409
 
                                        file.write('s 1\n')
410
 
                                else:
411
 
                                        file.write('s off\n')
412
 
                                contextSmooth = f.smooth
413
 
                        
414
 
                        file.write('f')
415
 
                        if m.faceUV and EXPORT_UV:
416
 
                                if EXPORT_NORMALS:
417
 
                                        if f.smooth: # Smoothed, use vertex normals
418
 
                                                for vi, v in enumerate(f_v):
419
 
                                                        file.write( ' %d/%d/%d' % (\
420
 
                                                          v.index+totverts,\
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' % (\
427
 
                                                          v.index+totverts,\
428
 
                                                          globalUVCoords[ veckey2d(f.uv[vi]) ],\
429
 
                                                          no)) # vert, uv, normal
430
 
                                
431
 
                                else: # No Normals
432
 
                                        for vi, v in enumerate(f_v):
433
 
                                                file.write( ' %d/%d' % (\
434
 
                                                  v.index+totverts,\
435
 
                                                  globalUVCoords[ veckey2d(f.uv[vi])])) # vert, uv
436
 
                                        
437
 
                                        
438
 
                        else: # No UV's
439
 
                                if EXPORT_NORMALS:
440
 
                                        if f.smooth: # Smoothed, use vertex normals
441
 
                                                for v in f_v:
442
 
                                                        file.write( ' %d//%d' % (\
443
 
                                                          v.index+totverts,\
444
 
                                                          globalNormals[ veckey3d(v.no) ]))
445
 
                                        else: # No smoothing, face normals
446
 
                                                no = globalNormals[ veckey3d(f.no) ]
447
 
                                                for v in f_v:
448
 
                                                        file.write( ' %d//%d' % (\
449
 
                                                          v.index+totverts,\
450
 
                                                          no))
451
 
                                else: # No Normals
452
 
                                        for v in f_v:
453
 
                                                file.write( ' %d' % (\
454
 
                                                  v.index+totverts))
455
 
                                        
456
 
                        file.write('\n')
457
 
                
458
 
                # Write edges.
459
 
                if EXPORT_EDGES:
460
 
                        edgeUsers = {}
461
 
                        for f in faces:
462
 
                                for i in xrange(len(f_v)):
463
 
                                        faceEdgeVKey = sortPair(f_v[i].index, f_v[i-1].index)
464
 
                                        
465
 
                                        # We dont realy need to keep count. Just that a face uses it 
466
 
                                        # so dont export.
467
 
                                        edgeUsers[faceEdgeVKey] = 1 
468
 
                                
469
 
                        for ed in edges:
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))
473
 
                
474
 
                # Make the indicies global rather then per mesh
475
 
                totverts += len(m.verts)
476
 
                m.verts= None
477
 
        file.close()
478
 
        
479
 
        
480
 
        # Now we have all our materials, save them
481
 
        if EXPORT_MTL:
482
 
                write_mtl(mtlfilename)
483
 
        if EXPORT_COPY_IMAGES:
484
 
                dest_dir = filename
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]
488
 
                if dest_dir:
489
 
                        copy_images(dest_dir)
490
 
                else:
491
 
                        print '\tError: "%s" could not be used as a base for an image path.' % filename
492
 
        
493
 
        print "OBJ Export time: %.2f" % (sys.time() - time1)
494
 
        
495
 
        
496
 
 
497
 
def write_ui(filename):
498
 
        
499
 
        for s in Window.GetScreenInfo():
500
 
                Window.QHandle(s['id'])
501
 
        
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)
516
 
        
517
 
        
518
 
        # Get USER Options
519
 
        pup_block = [\
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.'),\
528
 
        ('Context...'),\
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.'),\
533
 
        ('Grouping...'),\
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.'),\
537
 
        ]
538
 
        
539
 
        if not Draw.PupBlock('Export...', pup_block):
540
 
                return
541
 
        
542
 
        Window.WaitCursor(1)
543
 
        
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
558
 
        
559
 
        
560
 
        
561
 
        base_name, ext = splitExt(filename)
562
 
        context_name = [base_name, '', '', ext] # basename, scene_name, framenumber, extension
563
 
        
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()
569
 
        else:
570
 
                export_scenes = [orig_scene]
571
 
        
572
 
        # Export all scenes.
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')
577
 
                
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.
580
 
                
581
 
                # Export an animation?
582
 
                if EXPORT_ANIMATION:
583
 
                        scene_frames = range(context.startFrame(), context.endFrame()+1) # up to and including the end frame.
584
 
                else:
585
 
                        scene_frames = [orig_frame] # Dont export an animation.
586
 
                
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
591
 
                        
592
 
                        Blender.Set('curframe', frame)
593
 
                        if EXPORT_SEL_ONLY:
594
 
                                export_objects = Blender.Object.GetSelected() # Export Context
595
 
                        else:   
596
 
                                export_objects = scn.getChildren()
597
 
                        
598
 
                        # EXPORT THE FILE.
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)
604
 
                
605
 
                Blender.Set('curframe', orig_frame)
606
 
        
607
 
        # Restore old active scene.
608
 
        orig_scene.makeCurrent()
609
 
        Window.WaitCursor(0)
610
 
 
611
 
 
612
 
if __name__ == '__main__':
613
 
        Window.FileSelector(write_ui, 'Export Wavefront OBJ', sys.makename(ext='.obj'))