Merge from 2.5 rev 20515 through 20647.
[blender.git] / release / scripts / export_obj-2.5.py
1 #!BPY
2
3 """
4 Name: 'Wavefront (.obj)...'
5 Blender: 248
6 Group: 'Export'
7 Tooltip: 'Save a Wavefront OBJ File'
8 """
9
10 __author__ = "Campbell Barton, Jiri Hnidek"
11 __url__ = ['http://wiki.blender.org/index.php/Scripts/Manual/Export/wavefront_obj', 'www.blender.org', 'blenderartists.org']
12 __version__ = "1.2"
13
14 __bpydoc__ = """\
15 This script is an exporter to OBJ file format.
16
17 Usage:
18
19 Select the objects you wish to export and run this script from "File->Export" menu.
20 Selecting the default options from the popup box will be good in most cases.
21 All objects that can be represented as a mesh (mesh, curve, metaball, surface, text3d)
22 will be exported as mesh data.
23 """
24
25
26 # --------------------------------------------------------------------------
27 # OBJ Export v1.1 by Campbell Barton (AKA Ideasman)
28 # --------------------------------------------------------------------------
29 # ***** BEGIN GPL LICENSE BLOCK *****
30 #
31 # This program is free software; you can redistribute it and/or
32 # modify it under the terms of the GNU General Public License
33 # as published by the Free Software Foundation; either version 2
34 # of the License, or (at your option) any later version.
35 #
36 # This program is distributed in the hope that it will be useful,
37 # but WITHOUT ANY WARRANTY; without even the implied warranty of
38 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 # GNU General Public License for more details.
40 #
41 # You should have received a copy of the GNU General Public License
42 # along with this program; if not, write to the Free Software Foundation,
43 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
44 #
45 # ***** END GPL LICENCE BLOCK *****
46 # --------------------------------------------------------------------------
47
48
49 import bpy
50 import BPySys
51
52 # import Blender
53 # from Blender import Mesh, Scene, Window, sys, Image, Draw
54 # import BPyMesh
55 # import BPyObject
56 # import BPySys
57 # import BPyMessages
58
59 # Returns a tuple - path,extension.
60 # 'hello.obj' >  ('hello', '.obj')
61 def splitExt(path):
62         dotidx = path.rfind('.')
63         if dotidx == -1:
64                 return path, ''
65         else:
66                 return path[:dotidx], path[dotidx:] 
67
68 def fixName(name):
69         if name == None:
70                 return 'None'
71         else:
72                 return name.replace(' ', '_')
73
74 # A Dict of Materials
75 # (material.name, image.name):matname_imagename # matname_imagename has gaps removed.
76 MTL_DICT = {} 
77
78 def write_mtl(filename):
79         
80         world = Blender.World.GetCurrent()
81         if world:
82                 worldAmb = world.getAmb()
83         else:
84                 worldAmb = (0,0,0) # Default value
85         
86         file = open(filename, "w")
87         file.write('# Blender3D MTL File: %s\n' % Blender.Get('filename').split('\\')[-1].split('/')[-1])
88         file.write('# Material Count: %i\n' % len(MTL_DICT))
89         # Write material/image combinations we have used.
90         for key, (mtl_mat_name, mat, img) in MTL_DICT.iteritems():
91                 
92                 # Get the Blender data for the material and the image.
93                 # Having an image named None will make a bug, dont do it :)
94                 
95                 file.write('newmtl %s\n' % mtl_mat_name) # Define a new material: matname_imgname
96                 
97                 if mat:
98                         file.write('Ns %.6f\n' % ((mat.getHardness()-1) * 1.9607843137254901) ) # Hardness, convert blenders 1-511 to MTL's 
99                         file.write('Ka %.6f %.6f %.6f\n' %      tuple([c*mat.amb for c in worldAmb])  ) # Ambient, uses mirror colour,
100                         file.write('Kd %.6f %.6f %.6f\n' % tuple([c*mat.ref for c in mat.rgbCol]) ) # Diffuse
101                         file.write('Ks %.6f %.6f %.6f\n' % tuple([c*mat.spec for c in mat.specCol]) ) # Specular
102                         file.write('Ni %.6f\n' % mat.IOR) # Refraction index
103                         file.write('d %.6f\n' % mat.alpha) # Alpha (obj uses 'd' for dissolve)
104                         
105                         # 0 to disable lighting, 1 for ambient & diffuse only (specular color set to black), 2 for full lighting.
106                         if mat.getMode() & Blender.Material.Modes['SHADELESS']:
107                                 file.write('illum 0\n') # ignore lighting
108                         elif mat.getSpec() == 0:
109                                 file.write('illum 1\n') # no specular.
110                         else:
111                                 file.write('illum 2\n') # light normaly 
112                 
113                 else:
114                         #write a dummy material here?
115                         file.write('Ns 0\n')
116                         file.write('Ka %.6f %.6f %.6f\n' %      tuple([c for c in worldAmb])  ) # Ambient, uses mirror colour,
117                         file.write('Kd 0.8 0.8 0.8\n')
118                         file.write('Ks 0.8 0.8 0.8\n')
119                         file.write('d 1\n') # No alpha
120                         file.write('illum 2\n') # light normaly
121                 
122                 # Write images!
123                 if img:  # We have an image on the face!
124                         file.write('map_Kd %s\n' % img.filename.split('\\')[-1].split('/')[-1]) # Diffuse mapping image                 
125                 
126                 elif mat: # No face image. if we havea material search for MTex image.
127                         for mtex in mat.getTextures():
128                                 if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
129                                         try:
130                                                 filename = mtex.tex.image.filename.split('\\')[-1].split('/')[-1]
131                                                 file.write('map_Kd %s\n' % filename) # Diffuse mapping image
132                                                 break
133                                         except:
134                                                 # Texture has no image though its an image type, best ignore.
135                                                 pass
136                 
137                 file.write('\n\n')
138         
139         file.close()
140
141 def copy_file(source, dest):
142         file = open(source, 'rb')
143         data = file.read()
144         file.close()
145         
146         file = open(dest, 'wb')
147         file.write(data)
148         file.close()
149
150
151 def copy_images(dest_dir):
152         if dest_dir[-1] != sys.sep:
153                 dest_dir += sys.sep
154         
155         # Get unique image names
156         uniqueImages = {}
157         for matname, mat, image in MTL_DICT.itervalues(): # Only use image name
158                 # Get Texface images
159                 if image:
160                         uniqueImages[image] = image # Should use sets here. wait until Python 2.4 is default.
161                 
162                 # Get MTex images
163                 if mat:
164                         for mtex in mat.getTextures():
165                                 if mtex and mtex.tex.type == Blender.Texture.Types.IMAGE:
166                                         image_tex = mtex.tex.image
167                                         if image_tex:
168                                                 try:
169                                                         uniqueImages[image_tex] = image_tex
170                                                 except:
171                                                         pass
172         
173         # Now copy images
174         copyCount = 0
175         
176         for bImage in uniqueImages.itervalues():
177                 image_path = sys.expandpath(bImage.filename)
178                 if sys.exists(image_path):
179                         # Make a name for the target path.
180                         dest_image_path = dest_dir + image_path.split('\\')[-1].split('/')[-1]
181                         if not sys.exists(dest_image_path): # Image isnt alredy there
182                                 print '\tCopying "%s" > "%s"' % (image_path, dest_image_path)
183                                 copy_file(image_path, dest_image_path)
184                                 copyCount+=1
185         print '\tCopied %d images' % copyCount
186
187 def write(filename, objects,
188                   EXPORT_TRI=False,      EXPORT_EDGES=False,
189                   EXPORT_NORMALS=False,  EXPORT_NORMALS_HQ=False,
190                   EXPORT_UV=True,  EXPORT_MTL=True,      EXPORT_COPY_IMAGES=False,
191                   EXPORT_APPLY_MODIFIERS=True, EXPORT_ROTX90=True, EXPORT_BLEN_OBS=True,
192                   EXPORT_GROUP_BY_OB=False,      EXPORT_GROUP_BY_MAT=False, EXPORT_KEEP_VERT_ORDER=False):
193         '''
194         Basic write function. The context and options must be alredy set
195         This can be accessed externaly
196         eg.
197         write( 'c:\\test\\foobar.obj', Blender.Object.GetSelected() ) # Using default options.
198         '''
199         
200         def veckey3d(v):
201                 return round(v.x, 6), round(v.y, 6), round(v.z, 6)
202                 
203         def veckey2d(v):
204                 return round(v.x, 6), round(v.y, 6)
205         
206         print 'OBJ Export path: "%s"' % filename
207         temp_mesh_name = '~tmp-mesh'
208
209         time1 = sys.time()
210         scn = Scene.GetCurrent()
211
212         file = open(filename, "w")
213         
214         # Write Header
215         file.write('# Blender3D v%s OBJ File: %s\n' % (Blender.Get('version'), Blender.Get('filename').split('/')[-1].split('\\')[-1] ))
216         file.write('# www.blender3d.org\n')
217
218         # Tell the obj file what material file to use.
219         if EXPORT_MTL:
220                 mtlfilename = '%s.mtl' % '.'.join(filename.split('.')[:-1])
221                 file.write('mtllib %s\n' % ( mtlfilename.split('\\')[-1].split('/')[-1] ))
222         
223         # Get the container mesh. - used for applying modifiers and non mesh objects.
224         containerMesh = meshName = tempMesh = None
225         for meshName in Blender.NMesh.GetNames():
226                 if meshName.startswith(temp_mesh_name):
227                         tempMesh = Mesh.Get(meshName)
228                         if not tempMesh.users:
229                                 containerMesh = tempMesh
230         if not containerMesh:
231                 containerMesh = Mesh.New(temp_mesh_name)
232         
233         if EXPORT_ROTX90:
234                 mat_xrot90= Blender.Mathutils.RotationMatrix(-90, 4, 'x')
235                 
236         del meshName
237         del tempMesh
238         
239         # Initialize totals, these are updated each object
240         totverts = totuvco = totno = 1
241         
242         face_vert_index = 1
243         
244         globalNormals = {}
245         
246         # Get all meshs
247         for ob_main in objects:
248                 for ob, ob_mat in BPyObject.getDerivedObjects(ob_main):
249                         # Will work for non meshes now! :)
250                         # getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=True, scn=None)
251                         me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, False, scn)
252                         if not me:
253                                 continue
254                         
255                         if EXPORT_UV:
256                                 faceuv= me.faceUV
257                         else:
258                                 faceuv = False
259                         
260                         # We have a valid mesh
261                         if EXPORT_TRI and me.faces:
262                                 # Add a dummy object to it.
263                                 has_quads = False
264                                 for f in me.faces:
265                                         if len(f) == 4:
266                                                 has_quads = True
267                                                 break
268                                 
269                                 if has_quads:
270                                         oldmode = Mesh.Mode()
271                                         Mesh.Mode(Mesh.SelectModes['FACE'])
272                                         
273                                         me.sel = True
274                                         tempob = scn.objects.new(me)
275                                         me.quadToTriangle(0) # more=0 shortest length
276                                         oldmode = Mesh.Mode(oldmode)
277                                         scn.objects.unlink(tempob)
278                                         
279                                         Mesh.Mode(oldmode)
280                         
281                         # Make our own list so it can be sorted to reduce context switching
282                         faces = [ f for f in me.faces ]
283                         
284                         if EXPORT_EDGES:
285                                 edges = me.edges
286                         else:
287                                 edges = []
288                         
289                         if not (len(faces)+len(edges)+len(me.verts)): # Make sure there is somthing to write
290                                 continue # dont bother with this mesh.
291                         
292                         if EXPORT_ROTX90:
293                                 me.transform(ob_mat*mat_xrot90)
294                         else:
295                                 me.transform(ob_mat)
296                         
297                         # High Quality Normals
298                         if EXPORT_NORMALS and faces:
299                                 if EXPORT_NORMALS_HQ:
300                                         BPyMesh.meshCalcNormals(me)
301                                 else:
302                                         # transforming normals is incorrect
303                                         # when the matrix is scaled,
304                                         # better to recalculate them
305                                         me.calcNormals()
306                         
307                         # # Crash Blender
308                         #materials = me.getMaterials(1) # 1 == will return None in the list.
309                         materials = me.materials
310                         
311                         materialNames = []
312                         materialItems = materials[:]
313                         if materials:
314                                 for mat in materials:
315                                         if mat: # !=None
316                                                 materialNames.append(mat.name)
317                                         else:
318                                                 materialNames.append(None)
319                                 # Cant use LC because some materials are None.
320                                 # materialNames = map(lambda mat: mat.name, materials) # Bug Blender, dosent account for null materials, still broken.  
321                         
322                         # Possible there null materials, will mess up indicies
323                         # but at least it will export, wait until Blender gets fixed.
324                         materialNames.extend((16-len(materialNames)) * [None])
325                         materialItems.extend((16-len(materialItems)) * [None])
326                         
327                         # Sort by Material, then images
328                         # so we dont over context switch in the obj file.
329                         if EXPORT_KEEP_VERT_ORDER:
330                                 pass
331                         elif faceuv:
332                                 try:    faces.sort(key = lambda a: (a.mat, a.image, a.smooth))
333                                 except: faces.sort(lambda a,b: cmp((a.mat, a.image, a.smooth), (b.mat, b.image, b.smooth)))
334                         elif len(materials) > 1:
335                                 try:    faces.sort(key = lambda a: (a.mat, a.smooth))
336                                 except: faces.sort(lambda a,b: cmp((a.mat, a.smooth), (b.mat, b.smooth)))
337                         else:
338                                 # no materials
339                                 try:    faces.sort(key = lambda a: a.smooth)
340                                 except: faces.sort(lambda a,b: cmp(a.smooth, b.smooth))
341                         
342                         # Set the default mat to no material and no image.
343                         contextMat = (0, 0) # Can never be this, so we will label a new material teh first chance we get.
344                         contextSmooth = None # Will either be true or false,  set bad to force initialization switch.
345                         
346                         if EXPORT_BLEN_OBS or EXPORT_GROUP_BY_OB:
347                                 name1 = ob.name
348                                 name2 = ob.getData(1)
349                                 if name1 == name2:
350                                         obnamestring = fixName(name1)
351                                 else:
352                                         obnamestring = '%s_%s' % (fixName(name1), fixName(name2))
353                                 
354                                 if EXPORT_BLEN_OBS:
355                                         file.write('o %s\n' % obnamestring) # Write Object name
356                                 else: # if EXPORT_GROUP_BY_OB:
357                                         file.write('g %s\n' % obnamestring)
358                         
359                         
360                         # Vert
361                         for v in me.verts:
362                                 file.write('v %.6f %.6f %.6f\n' % tuple(v.co))
363                         
364                         # UV
365                         if faceuv:
366                                 uv_face_mapping = [[0,0,0,0] for f in faces] # a bit of a waste for tri's :/
367                                 
368                                 uv_dict = {} # could use a set() here
369                                 for f_index, f in enumerate(faces):
370                                         
371                                         for uv_index, uv in enumerate(f.uv):
372                                                 uvkey = veckey2d(uv)
373                                                 try:
374                                                         uv_face_mapping[f_index][uv_index] = uv_dict[uvkey]
375                                                 except:
376                                                         uv_face_mapping[f_index][uv_index] = uv_dict[uvkey] = len(uv_dict)
377                                                         file.write('vt %.6f %.6f\n' % tuple(uv))
378                                 
379                                 uv_unique_count = len(uv_dict)
380                                 del uv, uvkey, uv_dict, f_index, uv_index
381                                 # Only need uv_unique_count and uv_face_mapping
382                         
383                         # NORMAL, Smooth/Non smoothed.
384                         if EXPORT_NORMALS:
385                                 for f in faces:
386                                         if f.smooth:
387                                                 for v in f:
388                                                         noKey = veckey3d(v.no)
389                                                         if not globalNormals.has_key( noKey ):
390                                                                 globalNormals[noKey] = totno
391                                                                 totno +=1
392                                                                 file.write('vn %.6f %.6f %.6f\n' % noKey)
393                                         else:
394                                                 # Hard, 1 normal from the face.
395                                                 noKey = veckey3d(f.no)
396                                                 if not globalNormals.has_key( noKey ):
397                                                         globalNormals[noKey] = totno
398                                                         totno +=1
399                                                         file.write('vn %.6f %.6f %.6f\n' % noKey)
400                         
401                         if not faceuv:
402                                 f_image = None
403                         
404                         for f_index, f in enumerate(faces):
405                                 f_v= f.v
406                                 f_smooth= f.smooth
407                                 f_mat = min(f.mat, len(materialNames)-1)
408                                 if faceuv:
409                                         f_image = f.image
410                                         f_uv= f.uv
411                                 
412                                 # MAKE KEY
413                                 if faceuv and f_image: # Object is always true.
414                                         key = materialNames[f_mat],      f_image.name
415                                 else:
416                                         key = materialNames[f_mat],      None # No image, use None instead.
417                                 
418                                 # CHECK FOR CONTEXT SWITCH
419                                 if key == contextMat:
420                                         pass # Context alredy switched, dont do anythoing
421                                 else:
422                                         if key[0] == None and key[1] == None:
423                                                 # Write a null material, since we know the context has changed.
424                                                 if EXPORT_GROUP_BY_MAT:
425                                                         file.write('g %s_%s\n' % (fixName(ob.name), fixName(ob.getData(1))) ) # can be mat_image or (null)
426                                                 file.write('usemtl (null)\n') # mat, image
427                                                 
428                                         else:
429                                                 mat_data= MTL_DICT.get(key)
430                                                 if not mat_data:
431                                                         # First add to global dict so we can export to mtl
432                                                         # Then write mtl
433                                                         
434                                                         # Make a new names from the mat and image name,
435                                                         # converting any spaces to underscores with fixName.
436                                                         
437                                                         # If none image dont bother adding it to the name
438                                                         if key[1] == None:
439                                                                 mat_data = MTL_DICT[key] = ('%s'%fixName(key[0])), materialItems[f_mat], f_image
440                                                         else:
441                                                                 mat_data = MTL_DICT[key] = ('%s_%s' % (fixName(key[0]), fixName(key[1]))), materialItems[f_mat], f_image
442                                                 
443                                                 if EXPORT_GROUP_BY_MAT:
444                                                         file.write('g %s_%s_%s\n' % (fixName(ob.name), fixName(ob.getData(1)), mat_data[0]) ) # can be mat_image or (null)
445                                                 file.write('usemtl %s\n' % mat_data[0]) # can be mat_image or (null)
446                                         
447                                 contextMat = key
448                                 if f_smooth != contextSmooth:
449                                         if f_smooth: # on now off
450                                                 file.write('s 1\n')
451                                                 contextSmooth = f_smooth
452                                         else: # was off now on
453                                                 file.write('s off\n')
454                                                 contextSmooth = f_smooth
455                                 
456                                 file.write('f')
457                                 if faceuv:
458                                         if EXPORT_NORMALS:
459                                                 if f_smooth: # Smoothed, use vertex normals
460                                                         for vi, v in enumerate(f_v):
461                                                                 file.write( ' %d/%d/%d' % (\
462                                                                   v.index+totverts,\
463                                                                   totuvco + uv_face_mapping[f_index][vi],\
464                                                                   globalNormals[ veckey3d(v.no) ])) # vert, uv, normal
465                                                         
466                                                 else: # No smoothing, face normals
467                                                         no = globalNormals[ veckey3d(f.no) ]
468                                                         for vi, v in enumerate(f_v):
469                                                                 file.write( ' %d/%d/%d' % (\
470                                                                   v.index+totverts,\
471                                                                   totuvco + uv_face_mapping[f_index][vi],\
472                                                                   no)) # vert, uv, normal
473                                         
474                                         else: # No Normals
475                                                 for vi, v in enumerate(f_v):
476                                                         file.write( ' %d/%d' % (\
477                                                           v.index+totverts,\
478                                                           totuvco + uv_face_mapping[f_index][vi])) # vert, uv
479                                         
480                                         face_vert_index += len(f_v)
481                                 
482                                 else: # No UV's
483                                         if EXPORT_NORMALS:
484                                                 if f_smooth: # Smoothed, use vertex normals
485                                                         for v in f_v:
486                                                                 file.write( ' %d//%d' % (\
487                                                                   v.index+totverts,\
488                                                                   globalNormals[ veckey3d(v.no) ]))
489                                                 else: # No smoothing, face normals
490                                                         no = globalNormals[ veckey3d(f.no) ]
491                                                         for v in f_v:
492                                                                 file.write( ' %d//%d' % (\
493                                                                   v.index+totverts,\
494                                                                   no))
495                                         else: # No Normals
496                                                 for v in f_v:
497                                                         file.write( ' %d' % (\
498                                                           v.index+totverts))
499                                                 
500                                 file.write('\n')
501                         
502                         # Write edges.
503                         if EXPORT_EDGES:
504                                 LOOSE= Mesh.EdgeFlags.LOOSE
505                                 for ed in edges:
506                                         if ed.flag & LOOSE:
507                                                 file.write('f %d %d\n' % (ed.v1.index+totverts, ed.v2.index+totverts))
508                                 
509                         # Make the indicies global rather then per mesh
510                         totverts += len(me.verts)
511                         if faceuv:
512                                 totuvco += uv_unique_count
513                         me.verts= None
514         file.close()
515         
516         
517         # Now we have all our materials, save them
518         if EXPORT_MTL:
519                 write_mtl(mtlfilename)
520         if EXPORT_COPY_IMAGES:
521                 dest_dir = filename
522                 # Remove chars until we are just the path.
523                 while dest_dir and dest_dir[-1] not in '\\/':
524                         dest_dir = dest_dir[:-1]
525                 if dest_dir:
526                         copy_images(dest_dir)
527                 else:
528                         print '\tError: "%s" could not be used as a base for an image path.' % filename
529         
530         print "OBJ Export time: %.2f" % (sys.time() - time1)
531         
532
533 # replaced by do_export
534 def write_ui(filename):
535         
536         if not filename.lower().endswith('.obj'):
537                 filename += '.obj'
538         
539         if not BPyMessages.Warning_SaveOver(filename):
540                 return
541         
542         global EXPORT_APPLY_MODIFIERS, EXPORT_ROTX90, EXPORT_TRI, EXPORT_EDGES,\
543                 EXPORT_NORMALS, EXPORT_NORMALS_HQ, EXPORT_UV,\
544                 EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
545                 EXPORT_ANIMATION, EXPORT_COPY_IMAGES, EXPORT_BLEN_OBS,\
546                 EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER
547         
548         EXPORT_APPLY_MODIFIERS = Draw.Create(0)
549         EXPORT_ROTX90 = Draw.Create(1)
550         EXPORT_TRI = Draw.Create(0)
551         EXPORT_EDGES = Draw.Create(1)
552         EXPORT_NORMALS = Draw.Create(0)
553         EXPORT_NORMALS_HQ = Draw.Create(0)
554         EXPORT_UV = Draw.Create(1)
555         EXPORT_MTL = Draw.Create(1)
556         EXPORT_SEL_ONLY = Draw.Create(1)
557         EXPORT_ALL_SCENES = Draw.Create(0)
558         EXPORT_ANIMATION = Draw.Create(0)
559         EXPORT_COPY_IMAGES = Draw.Create(0)
560         EXPORT_BLEN_OBS = Draw.Create(0)
561         EXPORT_GROUP_BY_OB = Draw.Create(0)
562         EXPORT_GROUP_BY_MAT = Draw.Create(0)
563         EXPORT_KEEP_VERT_ORDER = Draw.Create(1)
564         
565         # Old UI
566         '''
567         # removed too many options are bad!
568         
569         # Get USER Options
570         pup_block = [\
571         ('Context...'),\
572         ('Selection Only', EXPORT_SEL_ONLY, 'Only export objects in visible selection. Else export whole scene.'),\
573         ('All Scenes', EXPORT_ALL_SCENES, 'Each scene as a separate OBJ file.'),\
574         ('Animation', EXPORT_ANIMATION, 'Each frame as a numbered OBJ file.'),\
575         ('Object Prefs...'),\
576         ('Apply Modifiers', EXPORT_APPLY_MODIFIERS, 'Use transformed mesh data from each object. May break vert order for morph targets.'),\
577         ('Rotate X90', EXPORT_ROTX90 , 'Rotate on export so Blenders UP is translated into OBJs UP'),\
578         ('Keep Vert Order', EXPORT_KEEP_VERT_ORDER, 'Keep vert and face order, disables some other options.'),\
579         ('Extra Data...'),\
580         ('Edges', EXPORT_EDGES, 'Edges not connected to faces.'),\
581         ('Normals', EXPORT_NORMALS, 'Export vertex normal data (Ignored on import).'),\
582         ('High Quality Normals', EXPORT_NORMALS_HQ, 'Calculate high quality normals for rendering.'),\
583         ('UVs', EXPORT_UV, 'Export texface UV coords.'),\
584         ('Materials', EXPORT_MTL, 'Write a separate MTL file with the OBJ.'),\
585         ('Copy Images', EXPORT_COPY_IMAGES, 'Copy image files to the export directory, never overwrite.'),\
586         ('Triangulate', EXPORT_TRI, 'Triangulate quads.'),\
587         ('Grouping...'),\
588         ('Objects', EXPORT_BLEN_OBS, 'Export blender objects as "OBJ objects".'),\
589         ('Object Groups', EXPORT_GROUP_BY_OB, 'Export blender objects as "OBJ Groups".'),\
590         ('Material Groups', EXPORT_GROUP_BY_MAT, 'Group by materials.'),\
591         ]
592         
593         if not Draw.PupBlock('Export...', pup_block):
594                 return
595         '''
596         
597         # BEGIN ALTERNATIVE UI *******************
598         if True: 
599                 
600                 EVENT_NONE = 0
601                 EVENT_EXIT = 1
602                 EVENT_REDRAW = 2
603                 EVENT_EXPORT = 3
604                 
605                 GLOBALS = {}
606                 GLOBALS['EVENT'] = EVENT_REDRAW
607                 #GLOBALS['MOUSE'] = Window.GetMouseCoords()
608                 GLOBALS['MOUSE'] = [i/2 for i in Window.GetScreenSize()]
609                 
610                 def obj_ui_set_event(e,v):
611                         GLOBALS['EVENT'] = e
612                 
613                 def do_split(e,v):
614                         global EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_APPLY_MODIFIERS, KEEP_VERT_ORDER
615                         if EXPORT_BLEN_OBS.val or EXPORT_GROUP_BY_OB.val or EXPORT_GROUP_BY_MAT.val or EXPORT_APPLY_MODIFIERS.val:
616                                 EXPORT_KEEP_VERT_ORDER.val = 0
617                         else:
618                                 EXPORT_KEEP_VERT_ORDER.val = 1
619                         
620                 def do_vertorder(e,v):
621                         global EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_APPLY_MODIFIERS, KEEP_VERT_ORDER
622                         if EXPORT_KEEP_VERT_ORDER.val:
623                                 EXPORT_BLEN_OBS.val = EXPORT_GROUP_BY_OB.val = EXPORT_GROUP_BY_MAT.val = EXPORT_APPLY_MODIFIERS.val = 0
624                         else:
625                                 if not (EXPORT_BLEN_OBS.val or EXPORT_GROUP_BY_OB.val or EXPORT_GROUP_BY_MAT.val or EXPORT_APPLY_MODIFIERS.val):
626                                         EXPORT_KEEP_VERT_ORDER.val = 1
627                         
628                 def do_help(e,v):
629                         url = __url__[0]
630                         print 'Trying to open web browser with documentation at this address...'
631                         print '\t' + url
632                         
633                         try:
634                                 import webbrowser
635                                 webbrowser.open(url)
636                         except:
637                                 print '...could not open a browser window.'
638                 
639                 def obj_ui():
640                         ui_x, ui_y = GLOBALS['MOUSE']
641                         
642                         # Center based on overall pup size
643                         ui_x -= 165
644                         ui_y -= 110
645                         
646                         global EXPORT_APPLY_MODIFIERS, EXPORT_ROTX90, EXPORT_TRI, EXPORT_EDGES,\
647                                 EXPORT_NORMALS, EXPORT_NORMALS_HQ, EXPORT_UV,\
648                                 EXPORT_MTL, EXPORT_SEL_ONLY, EXPORT_ALL_SCENES,\
649                                 EXPORT_ANIMATION, EXPORT_COPY_IMAGES, EXPORT_BLEN_OBS,\
650                                 EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER
651
652                         Draw.Label('Context...', ui_x+9, ui_y+209, 220, 20)
653                         Draw.BeginAlign()
654                         EXPORT_SEL_ONLY = Draw.Toggle('Selection Only', EVENT_NONE, ui_x+9, ui_y+189, 110, 20, EXPORT_SEL_ONLY.val, 'Only export objects in visible selection. Else export whole scene.')
655                         EXPORT_ALL_SCENES = Draw.Toggle('All Scenes', EVENT_NONE, ui_x+119, ui_y+189, 110, 20, EXPORT_ALL_SCENES.val, 'Each scene as a separate OBJ file.')
656                         EXPORT_ANIMATION = Draw.Toggle('Animation', EVENT_NONE, ui_x+229, ui_y+189, 110, 20, EXPORT_ANIMATION.val, 'Each frame as a numbered OBJ file.')
657                         Draw.EndAlign()
658                         
659                         
660                         Draw.Label('Output Options...', ui_x+9, ui_y+159, 220, 20)
661                         Draw.BeginAlign()
662                         EXPORT_APPLY_MODIFIERS = Draw.Toggle('Apply Modifiers', EVENT_REDRAW, ui_x+9, ui_y+140, 110, 20, EXPORT_APPLY_MODIFIERS.val, 'Use transformed mesh data from each object. May break vert order for morph targets.', do_split)
663                         EXPORT_ROTX90 = Draw.Toggle('Rotate X90', EVENT_NONE, ui_x+119, ui_y+140, 110, 20, EXPORT_ROTX90.val, 'Rotate on export so Blenders UP is translated into OBJs UP')
664                         EXPORT_COPY_IMAGES = Draw.Toggle('Copy Images', EVENT_NONE, ui_x+229, ui_y+140, 110, 20, EXPORT_COPY_IMAGES.val, 'Copy image files to the export directory, never overwrite.')
665                         Draw.EndAlign()
666                         
667                         
668                         Draw.Label('Export...', ui_x+9, ui_y+109, 220, 20)
669                         Draw.BeginAlign()
670                         EXPORT_EDGES = Draw.Toggle('Edges', EVENT_NONE, ui_x+9, ui_y+90, 50, 20, EXPORT_EDGES.val, 'Edges not connected to faces.')
671                         EXPORT_TRI = Draw.Toggle('Triangulate', EVENT_NONE, ui_x+59, ui_y+90, 70, 20, EXPORT_TRI.val, 'Triangulate quads.')
672                         Draw.EndAlign()
673                         Draw.BeginAlign()
674                         EXPORT_MTL = Draw.Toggle('Materials', EVENT_NONE, ui_x+139, ui_y+90, 70, 20, EXPORT_MTL.val, 'Write a separate MTL file with the OBJ.')
675                         EXPORT_UV = Draw.Toggle('UVs', EVENT_NONE, ui_x+209, ui_y+90, 31, 20, EXPORT_UV.val, 'Export texface UV coords.')
676                         Draw.EndAlign()
677                         Draw.BeginAlign()
678                         EXPORT_NORMALS = Draw.Toggle('Normals', EVENT_NONE, ui_x+250, ui_y+90, 59, 20, EXPORT_NORMALS.val, 'Export vertex normal data (Ignored on import).')
679                         EXPORT_NORMALS_HQ = Draw.Toggle('HQ', EVENT_NONE, ui_x+309, ui_y+90, 31, 20, EXPORT_NORMALS_HQ.val, 'Calculate high quality normals for rendering.')
680                         Draw.EndAlign()
681                         
682                         
683                         Draw.Label('Blender Objects as OBJ:', ui_x+9, ui_y+59, 220, 20)
684                         Draw.BeginAlign()
685                         EXPORT_BLEN_OBS = Draw.Toggle('Objects', EVENT_REDRAW, ui_x+9, ui_y+39, 60, 20, EXPORT_BLEN_OBS.val, 'Export blender objects as "OBJ objects".', do_split)
686                         EXPORT_GROUP_BY_OB = Draw.Toggle('Groups', EVENT_REDRAW, ui_x+69, ui_y+39, 60, 20, EXPORT_GROUP_BY_OB.val, 'Export blender objects as "OBJ Groups".', do_split)
687                         EXPORT_GROUP_BY_MAT = Draw.Toggle('Material Groups', EVENT_REDRAW, ui_x+129, ui_y+39, 100, 20, EXPORT_GROUP_BY_MAT.val, 'Group by materials.', do_split)
688                         Draw.EndAlign()
689                         
690                         EXPORT_KEEP_VERT_ORDER = Draw.Toggle('Keep Vert Order', EVENT_REDRAW, ui_x+239, ui_y+39, 100, 20, EXPORT_KEEP_VERT_ORDER.val, 'Keep vert and face order, disables some other options. Use for morph targets.', do_vertorder)
691                         
692                         Draw.BeginAlign()
693                         Draw.PushButton('Online Help', EVENT_REDRAW, ui_x+9, ui_y+9, 110, 20, 'Load the wiki page for this script', do_help)
694                         Draw.PushButton('Cancel', EVENT_EXIT, ui_x+119, ui_y+9, 110, 20, '', obj_ui_set_event)
695                         Draw.PushButton('Export', EVENT_EXPORT, ui_x+229, ui_y+9, 110, 20, 'Export with these settings', obj_ui_set_event)
696                         Draw.EndAlign()
697
698                 
699                 # hack so the toggle buttons redraw. this is not nice at all
700                 while GLOBALS['EVENT'] not in (EVENT_EXIT, EVENT_EXPORT):
701                         Draw.UIBlock(obj_ui, 0)
702                 
703                 if GLOBALS['EVENT'] != EVENT_EXPORT:
704                         return
705                 
706         # END ALTERNATIVE UI *********************
707         
708         
709         if EXPORT_KEEP_VERT_ORDER.val:
710                 EXPORT_BLEN_OBS.val = False
711                 EXPORT_GROUP_BY_OB.val = False
712                 EXPORT_GROUP_BY_MAT.val = False
713                 EXPORT_APPLY_MODIFIERS.val = False
714         
715         Window.EditMode(0)
716         Window.WaitCursor(1)
717         
718         EXPORT_APPLY_MODIFIERS = EXPORT_APPLY_MODIFIERS.val
719         EXPORT_ROTX90 = EXPORT_ROTX90.val
720         EXPORT_TRI = EXPORT_TRI.val
721         EXPORT_EDGES = EXPORT_EDGES.val
722         EXPORT_NORMALS = EXPORT_NORMALS.val
723         EXPORT_NORMALS_HQ = EXPORT_NORMALS_HQ.val
724         EXPORT_UV = EXPORT_UV.val
725         EXPORT_MTL = EXPORT_MTL.val
726         EXPORT_SEL_ONLY = EXPORT_SEL_ONLY.val
727         EXPORT_ALL_SCENES = EXPORT_ALL_SCENES.val
728         EXPORT_ANIMATION = EXPORT_ANIMATION.val
729         EXPORT_COPY_IMAGES = EXPORT_COPY_IMAGES.val
730         EXPORT_BLEN_OBS = EXPORT_BLEN_OBS.val
731         EXPORT_GROUP_BY_OB = EXPORT_GROUP_BY_OB.val
732         EXPORT_GROUP_BY_MAT = EXPORT_GROUP_BY_MAT.val
733         EXPORT_KEEP_VERT_ORDER = EXPORT_KEEP_VERT_ORDER.val
734         
735         
736         
737         base_name, ext = splitExt(filename)
738         context_name = [base_name, '', '', ext] # basename, scene_name, framenumber, extension
739         
740         # Use the options to export the data using write()
741         # def write(filename, objects, EXPORT_EDGES=False, EXPORT_NORMALS=False, EXPORT_MTL=True, EXPORT_COPY_IMAGES=False, EXPORT_APPLY_MODIFIERS=True):
742         orig_scene = Scene.GetCurrent()
743         if EXPORT_ALL_SCENES:
744                 export_scenes = Scene.Get()
745         else:
746                 export_scenes = [orig_scene]
747         
748         # Export all scenes.
749         for scn in export_scenes:
750                 scn.makeCurrent() # If alredy current, this is not slow.
751                 context = scn.getRenderingContext()
752                 orig_frame = Blender.Get('curframe')
753                 
754                 if EXPORT_ALL_SCENES: # Add scene name into the context_name
755                         context_name[1] = '_%s' % BPySys.cleanName(scn.name) # WARNING, its possible that this could cause a collision. we could fix if were feeling parranoied.
756                 
757                 # Export an animation?
758                 if EXPORT_ANIMATION:
759                         scene_frames = xrange(context.startFrame(), context.endFrame()+1) # up to and including the end frame.
760                 else:
761                         scene_frames = [orig_frame] # Dont export an animation.
762                 
763                 # Loop through all frames in the scene and export.
764                 for frame in scene_frames:
765                         if EXPORT_ANIMATION: # Add frame to the filename.
766                                 context_name[2] = '_%.6d' % frame
767                         
768                         Blender.Set('curframe', frame)
769                         if EXPORT_SEL_ONLY:
770                                 export_objects = scn.objects.context
771                         else:   
772                                 export_objects = scn.objects
773                         
774                         full_path= ''.join(context_name)
775                         
776                         # erm... bit of a problem here, this can overwrite files when exporting frames. not too bad.
777                         # EXPORT THE FILE.
778                         write(full_path, export_objects,\
779                         EXPORT_TRI, EXPORT_EDGES, EXPORT_NORMALS,\
780                         EXPORT_NORMALS_HQ, EXPORT_UV, EXPORT_MTL,\
781                         EXPORT_COPY_IMAGES, EXPORT_APPLY_MODIFIERS,\
782                         EXPORT_ROTX90, EXPORT_BLEN_OBS,\
783                         EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER)
784                 
785                 Blender.Set('curframe', orig_frame)
786         
787         # Restore old active scene.
788         orig_scene.makeCurrent()
789         Window.WaitCursor(0)
790
791
792 def do_export(filename, context):
793 #       Window.EditMode(0)
794 #       Window.WaitCursor(1)
795         
796         EXPORT_APPLY_MODIFIERS = True
797         EXPORT_ROTX90 = True
798         EXPORT_TRI = False
799         EXPORT_EDGES = False
800         EXPORT_NORMALS = False
801         EXPORT_NORMALS_HQ = False
802         EXPORT_UV = True
803         EXPORT_MTL = True
804         EXPORT_SEL_ONLY = True
805         EXPORT_ALL_SCENES = False # XXX not working atm
806         EXPORT_ANIMATION = False
807         EXPORT_COPY_IMAGES = False
808         EXPORT_BLEN_OBS = True
809         EXPORT_GROUP_BY_OB = False
810         EXPORT_GROUP_BY_MAT = False
811         EXPORT_KEEP_VERT_ORDER = False
812
813         base_name, ext = splitExt(filename)
814         context_name = [base_name, '', '', ext] # Base name, scene name, frame number, extension
815         
816         orig_scene = context.scene
817
818 #       if EXPORT_ALL_SCENES:
819 #               export_scenes = bpy.data.scenes
820 #       else:
821 #               export_scenes = [orig_scene]
822
823         # XXX only exporting one scene atm since changing 
824         # current scene is not possible.
825         # Brecht says that ideally in 2.5 we won't need such a function,
826         # allowing multiple scenes open at once.
827         export_scenes = [orig_scene]
828
829         # Export all scenes.
830         for scn in export_scenes:
831 #               scn.makeCurrent() # If already current, this is not slow.
832 #               context = scn.getRenderingContext()
833                 orig_frame = scn.current_frame
834                 
835                 if EXPORT_ALL_SCENES: # Add scene name into the context_name
836                         context_name[1] = '_%s' % BPySys.cleanName(scn.name) # WARNING, its possible that this could cause a collision. we could fix if were feeling parranoied.
837                 
838                 # Export an animation?
839                 if EXPORT_ANIMATION:
840                         scene_frames = xrange(scn.start_frame, context.end_frame+1) # Up to and including the end frame.
841                 else:
842                         scene_frames = [orig_frame] # Dont export an animation.
843                 
844                 # Loop through all frames in the scene and export.
845                 for frame in scene_frames:
846                         if EXPORT_ANIMATION: # Add frame to the filename.
847                                 context_name[2] = '_%.6d' % frame
848                         
849                         scn.current_frame = frame
850                         if EXPORT_SEL_ONLY:
851                                 export_objects = context.selected_objects
852                         else:   
853                                 export_objects = scn.objects
854                         
855                         full_path= ''.join(context_name)
856                         
857                         # erm... bit of a problem here, this can overwrite files when exporting frames. not too bad.
858                         # EXPORT THE FILE.
859 #                       write(full_path, export_objects,
860 #                                       EXPORT_TRI, EXPORT_EDGES, EXPORT_NORMALS,
861 #                                       EXPORT_NORMALS_HQ, EXPORT_UV, EXPORT_MTL,
862 #                                       EXPORT_COPY_IMAGES, EXPORT_APPLY_MODIFIERS,
863 #                                       EXPORT_ROTX90, EXPORT_BLEN_OBS,
864 #                                       EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER)
865                 
866                 scn.current_frame = orig_frame
867         
868         # Restore old active scene.
869 #       orig_scene.makeCurrent()
870 #       Window.WaitCursor(0)
871
872
873 class SCRIPT_OT_export_obj(bpy.types.Operator):
874         '''
875         Operator documentatuon text, will be used for the operator tooltip and python docs.
876         '''
877         __label__ = 'My Operator'
878         
879         # List of operator properties, the attributes will be assigned
880         # to the class instance from the operator settings before calling.
881         __props__ = [
882                 #               bpy.types.FloatProperty(attr="setting_1", name="Example 1",
883                 #               default=10.0, min=0, max=10, description="Add info here"),
884                 #               bpy.types.IntProperty(attr="setting_2", default=2),
885                 #               bpy.types.BoolProperty(attr="toggle", default=True)
886                 ]
887         
888         def execu(self, context):
889                 print("Selected: " + context.active_object.name)
890
891                 do_export("/tmp/test.obj", context)
892
893                 return 'FINISHED'
894         
895         def invoke(self, event):
896                 print("Invoke")
897                 return 'FINISHED'
898         
899         def poll(self, context): # Poll isnt working yet
900                 print("Poll")
901                 return True
902
903 if (hasattr(bpy.ops, "SCRIPT_OT_export_obj")):
904         bpy.ops.remove(bpy.ops.SCRIPT_OT_export_obj)
905
906 bpy.ops.add(SCRIPT_OT_export_obj)
907
908 bpy.ops.SCRIPT_OT_export_obj()
909
910 bpy.ops.remove(bpy.ops.SCRIPT_OT_export_obj)