~ubuntu-branches/ubuntu/gutsy/blender/gutsy-security

« back to all changes in this revision

Viewing changes to release/scripts/import_obj.py

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ernst
  • Date: 2007-05-17 11:47:59 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517114759-yp4ybrnhp2u7pk66
Tags: 2.44-1
* New upstream release.
* Drop debian/patches/01_64bits_stupidity, not needed anymore: as of this
  version blender is 64 bits safe again. Adjust README.Debian accordingly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
# --------------------------------------------------------------------------
42
42
 
43
43
from Blender import *
 
44
import bpy
44
45
import BPyMesh
45
46
import BPyImage
46
47
import BPyMessages
117
118
        #==================================================================================#
118
119
        def load_material_image(blender_material, context_material_name, imagepath, type):
119
120
                
120
 
                texture= Texture.New(type)
 
121
                texture= bpy.data.textures.new(type)
121
122
                texture.setType('Image')
122
123
                
123
124
                # Absolute path - c:\.. etc would work here
167
168
        
168
169
        #Create new materials
169
170
        for name in unique_materials.iterkeys():
170
 
                unique_materials[name]= Material.New(name)
 
171
                unique_materials[name]= bpy.data.materials.new(name)
171
172
                
172
173
                unique_material_images[name]= None, False # assign None to all material images to start with, add to later.
173
174
                
208
209
                                                context_material.setAlpha(float(line_split[1]))
209
210
                                        elif line_lower.startswith('map_ka'):
210
211
                                                img_filepath= line_value(line.split())
211
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'Ka')
 
212
                                                if img_filepath:
 
213
                                                        load_material_image(context_material, context_material_name, img_filepath, 'Ka')
212
214
                                        elif line_lower.startswith('map_ks'):
213
215
                                                img_filepath= line_value(line.split())
214
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'Ks')
 
216
                                                if img_filepath:
 
217
                                                        load_material_image(context_material, context_material_name, img_filepath, 'Ks')
215
218
                                        elif line_lower.startswith('map_kd'):
216
219
                                                img_filepath= line_value(line.split())
217
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'Kd')
 
220
                                                if img_filepath:
 
221
                                                        load_material_image(context_material, context_material_name, img_filepath, 'Kd')
218
222
                                        elif line_lower.startswith('map_bump'):
219
223
                                                img_filepath= line_value(line.split())
220
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'Bump')
 
224
                                                if img_filepath:
 
225
                                                        load_material_image(context_material, context_material_name, img_filepath, 'Bump')
221
226
                                        elif line_lower.startswith('map_d') or line_lower.startswith('map_tr'): # Alpha map - Dissolve
222
227
                                                img_filepath= line_value(line.split())
223
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'D')
 
228
                                                if img_filepath:
 
229
                                                        load_material_image(context_material, context_material_name, img_filepath, 'D')
224
230
                                        
225
231
                                        elif line_lower.startswith('refl'): # Reflectionmap
226
232
                                                img_filepath= line_value(line.split())
227
 
                                                load_material_image(context_material, context_material_name, img_filepath, 'refl')
 
233
                                                if img_filepath:
 
234
                                                        load_material_image(context_material, context_material_name, img_filepath, 'refl')
228
235
                        mtl.close()
229
236
 
230
237
 
231
238
 
232
239
        
233
 
def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OBJECTS, SPLIT_MATERIALS):
 
240
def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP, SPLIT_MATERIALS):
234
241
        '''
235
242
        Takes vert_loc and faces, and seperates into multiple sets of 
236
243
        (verts_loc, faces, unique_materials, dataname)
239
246
        
240
247
        filename = stripExt(stripPath(filepath))
241
248
        
242
 
        if not SPLIT_OBJECTS and not SPLIT_MATERIALS:
 
249
        if not SPLIT_OB_OR_GROUP and not SPLIT_MATERIALS:
243
250
                # use the filename for the object name since we arnt chopping up the mesh.
244
251
                return [(verts_loc, faces, unique_materials, filename)]
245
252
        
254
261
                        return key
255
262
        
256
263
        # Return a key that makes the faces unique.
257
 
        if SPLIT_OBJECTS and not SPLIT_MATERIALS:
 
264
        if SPLIT_OB_OR_GROUP and not SPLIT_MATERIALS:
258
265
                def face_key(face):
259
266
                        return face[4] # object
260
267
        
261
 
        if not SPLIT_OBJECTS and SPLIT_MATERIALS:
 
268
        elif not SPLIT_OB_OR_GROUP and SPLIT_MATERIALS:
262
269
                def face_key(face):
263
270
                        return face[2] # material
264
271
        
313
320
        return [(value[0], value[1], value[2], key_to_name(key)) for key, value in face_split_dict.iteritems()]
314
321
 
315
322
 
316
 
def create_mesh(new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, verts_tex, faces, unique_materials, unique_material_images, unique_smooth_groups, dataname):
 
323
def create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc, verts_tex, faces, unique_materials, unique_material_images, unique_smooth_groups, dataname):
317
324
        '''
318
325
        Takes all the data gathered and generates a mesh, adding the new object to new_objects
319
326
        deals with fgons, sharp edges and assigning materials
377
384
                        if has_ngons and len_face_vert_loc_indicies > 4:
378
385
                                
379
386
                                ngon_face_indices= BPyMesh.ngon(verts_loc, face_vert_loc_indicies)
380
 
                                
381
387
                                faces.extend(\
382
388
                                [(\
383
389
                                [face_vert_loc_indicies[ngon[0]], face_vert_loc_indicies[ngon[1]], face_vert_loc_indicies[ngon[2]] ],\
426
432
        for name, index in material_mapping.iteritems():
427
433
                materials[index]= unique_materials[name]
428
434
        
429
 
        me= Mesh.New(dataname)
 
435
        me= bpy.data.meshes.new(dataname)
430
436
        
431
437
        me.materials= materials[0:16] # make sure the list isnt too big.
432
438
        #me.verts.extend([(0,0,0)]) # dummy vert
445
451
        
446
452
        for i, face in enumerate(faces):
447
453
                if len(face[0]) < 2:
448
 
                        raise "Fooo"
449
 
                if len(face[0])==2:
 
454
                        pass #raise "bad face"
 
455
                elif len(face[0])==2:
450
456
                        if CREATE_EDGES:
451
457
                                edges.append(face[0])
452
458
                else:
521
527
        
522
528
        me.calcNormals()
523
529
        
524
 
        scn= Scene.GetCurrent()
525
530
        ob= scn.objects.new(me)
526
531
        new_objects.append(ob)
527
532
 
701
706
        
702
707
        
703
708
        # deselect all
704
 
        Scene.GetCurrent().objects.selected = []
 
709
        scn = bpy.data.scenes.active
 
710
        scn.objects.selected = []
705
711
        new_objects= [] # put new objects here
706
712
        
707
713
        print '\tbuilding geometry...\n\tverts:%i faces:%i materials: %i smoothgroups:%i ...' % ( len(verts_loc), len(faces), len(unique_materials), len(unique_smooth_groups) ),
708
714
        # Split the mesh by objects/materials, may 
709
 
        for verts_loc_split, faces_split, unique_materials_split, dataname in split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OBJECTS, SPLIT_MATERIALS):
 
715
        if SPLIT_OBJECTS or SPLIT_GROUPS:       SPLIT_OB_OR_GROUP = True
 
716
        else:                                                           SPLIT_OB_OR_GROUP = False
 
717
        
 
718
        for verts_loc_split, faces_split, unique_materials_split, dataname in split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP, SPLIT_MATERIALS):
710
719
                # Create meshes from the data
711
 
                create_mesh(new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc_split, verts_tex, faces_split, unique_materials_split, unique_material_images, unique_smooth_groups, dataname)
 
720
                create_mesh(scn, new_objects, has_ngons, CREATE_FGONS, CREATE_EDGES, verts_loc_split, verts_tex, faces_split, unique_materials_split, unique_material_images, unique_smooth_groups, dataname)
712
721
        
713
722
        axis_min= [ 1000000000]*3
714
723
        axis_max= [-1000000000]*3
762
771
        ('Smooth Groups', CREATE_SMOOTH_GROUPS, 'Surround smooth groups by sharp edges'),\
763
772
        ('Create FGons', CREATE_FGONS, 'Import faces with more then 4 verts as fgons.'),\
764
773
        ('Lines', CREATE_EDGES, 'Import lines and faces with 2 verts as edges'),\
765
 
        'Seperate objects from obj...',\
 
774
        'Separate objects from obj...',\
766
775
        ('Object', SPLIT_OBJECTS, 'Import OBJ Objects into Blender Objects'),\
767
776
        ('Group', SPLIT_GROUPS, 'Import OBJ Groups into Blender Objects'),\
768
777
        ('Material', SPLIT_MATERIALS, 'Import each material into a seperate mesh (Avoids > 16 per mesh error)'),\
796
805
                        return
797
806
                
798
807
                for f in files:
799
 
                        scn= Scene.New( stripExt(f) )
 
808
                        scn= bpy.data.scenes.new( stripExt(f) )
800
809
                        scn.makeCurrent()
801
810
                        
802
811
                        load_obj(sys.join(filepath, f),\
861
870
                        _obj= _obj[:-1]
862
871
                        print 'Importing', _obj, '\nNUMBER', i, 'of', len(lines)
863
872
                        _obj_file= _obj.split('/')[-1].split('\\')[-1]
864
 
                        newScn= Scene.New(_obj_file)
 
873
                        newScn= bpy.data.scenes.new(_obj_file)
865
874
                        newScn.makeCurrent()
866
875
                        load_obj(_obj, False)
867
876