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

« back to all changes in this revision

Viewing changes to release/scripts/3ds_import.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:
117
117
# Importing modules
118
118
 
119
119
import Blender
120
 
from Blender import Mesh, Scene, Object, Material, Image, Texture, Lamp, Mathutils
 
120
import bpy
 
121
from Blender import Mesh, Object, Material, Image, Texture, Lamp, Mathutils
121
122
from Blender.Mathutils import Vector
122
123
import BPyImage
123
124
 
142
143
#which shold be more useful.
143
144
 
144
145
def createBlenderTexture(material, name, image):
145
 
        texture= Texture.New(name)
 
146
        texture= bpy.data.textures.new(name)
146
147
        texture.setType('Image')
147
148
        texture.image= image
148
149
        material.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL)
313
314
        contextObName= None
314
315
        contextLamp= [None, None] # object, Data
315
316
        contextMaterial= None
316
 
        # contextMatrix= Blender.Mathutils.Matrix(); contextMatrix.identity()
 
317
        contextMatrix_rot= None # Blender.Mathutils.Matrix(); contextMatrix.identity()
 
318
        #contextMatrix_tx= None # Blender.Mathutils.Matrix(); contextMatrix.identity()
317
319
        contextMesh_vertls= None
318
320
        contextMesh_facels= None
319
321
        contextMeshMaterials= {} # matname:[face_idxs]
329
331
        STRUCT_SIZE_UNSIGNED_SHORT= calcsize('H')
330
332
        STRUCT_SIZE_4UNSIGNED_SHORT= calcsize('4H')
331
333
        STRUCT_SIZE_4x3MAT= calcsize('ffffffffffff')
332
 
        
 
334
        _STRUCT_SIZE_4x3MAT= calcsize('fffffffffffff')
 
335
        # STRUCT_SIZE_4x3MAT= calcsize('ffffffffffff')
 
336
        # print STRUCT_SIZE_4x3MAT, ' STRUCT_SIZE_4x3MAT'
333
337
        
334
338
        def putContextMesh(myContextMesh_vertls, myContextMesh_facels, myContextMeshMaterials):
335
 
                ####contextMesh.transform(contextMatrix.copy().invert())
336
339
                
337
340
                materialFaces= set() # faces that have a material. Can optimize?
338
341
                
362
365
                        vertsToUse = [i for i in xrange(len(myContextMesh_vertls)) if faceVertUsers[i]]
363
366
                        myVertMapping = dict( [ (ii, i) for i, ii in enumerate(vertsToUse) ] )
364
367
                        
365
 
                        ##bmesh = Mesh.New(contextMesh.name)
366
 
                        bmesh = Mesh.New()
 
368
                        tempName= '%s_%s' % (contextObName, matName) # matName may be None.
 
369
                        bmesh = bpy.data.meshes.new(tempName)
367
370
                        
368
371
                        if matName == None:
369
372
                                img= None
387
390
                                        # BUGGY API - face_mapping is not always the right length
388
391
                                        map_index= face_mapping[ii]
389
392
                                        
390
 
                                        if map_index != None:   
 
393
                                        if map_index != None:
391
394
                                                targetFace= bmesh.faces[map_index]
392
395
                                                if contextMeshUV:
393
396
                                                        # v.index-1 because of the DUMMYVERT
395
398
                                                if img:
396
399
                                                        targetFace.image= img
397
400
                        
398
 
                        tempName= '%s_%s' % (contextObName, matName) # matName may be None.
399
 
                        bmesh.name= tempName
400
 
                        # ob = Object.New('Mesh', tempName)
401
 
                        # ob.link(bmesh)
 
401
                        # bmesh.transform(contextMatrix)
402
402
                        ob = SCN_OBJECTS.new(bmesh, tempName)
403
 
                        
404
 
                        ####ob.setMatrix(contextMatrix)
 
403
                        '''
 
404
                        if contextMatrix_tx:
 
405
                                ob.setMatrix(contextMatrix_tx)
 
406
                        '''
 
407
                        
 
408
                        if contextMatrix_rot:
 
409
                                ob.setMatrix(contextMatrix_rot)
 
410
                        
405
411
                        importedObjects.append(ob)
406
412
                        
407
413
                
455
461
                #is it a material chunk?
456
462
                elif (new_chunk.ID==MATERIAL):
457
463
                        #print 'elif (new_chunk.ID==MATERIAL):'
458
 
                        contextMaterial= Material.New()
 
464
                        contextMaterial= bpy.data.materials.new('Material')
459
465
                
460
466
                elif (new_chunk.ID==MAT_NAME):
461
467
                        #print 'elif (new_chunk.ID==MAT_NAME):'
514
520
                        
515
521
                elif (new_chunk.ID==MAT_TEXTURE_MAP):
516
522
                        #print 'elif (new_chunk.ID==MAT_TEXTURE_MAP):'
517
 
                        new_texture= Blender.Texture.New('Diffuse')
 
523
                        new_texture= bpy.data.textures.new('Diffuse')
518
524
                        new_texture.setType('Image')
519
525
                        img = None
520
526
                        while (new_chunk.bytes_read<new_chunk.length):
538
544
                        
539
545
                elif (new_chunk.ID==MAT_SPECULAR_MAP):
540
546
                        #print 'elif (new_chunk.ID==MAT_SPECULAR_MAP):'
541
 
                        new_texture= Blender.Texture.New('Specular')
 
547
                        new_texture= bpy.data.textures.new('Specular')
542
548
                        new_texture.setType('Image')
543
549
                        img = None
544
550
                        while (new_chunk.bytes_read<new_chunk.length):
560
566
        
561
567
                elif (new_chunk.ID==MAT_OPACITY_MAP):
562
568
                        #print 'new_texture=Blender.Texture.New('Opacity')'
563
 
                        new_texture= Blender.Texture.New('Opacity')
 
569
                        new_texture= bpy.data.textures.new('Opacity')
564
570
                        new_texture.setType('Image')
565
571
                        img = None
566
572
                        while (new_chunk.bytes_read<new_chunk.length):
581
587
 
582
588
                elif (new_chunk.ID==MAT_BUMP_MAP):
583
589
                        #print 'elif (new_chunk.ID==MAT_BUMP_MAP):'
584
 
                        new_texture= Blender.Texture.New('Bump')
 
590
                        new_texture= bpy.data.textures.new('Bump')
585
591
                        new_texture.setType('Image')
586
592
                        img = None
587
593
                        while (new_chunk.bytes_read<new_chunk.length):
613
619
 
614
620
                elif (new_chunk.ID==OBJECT_LAMP): # Basic lamp support.
615
621
                        
616
 
                        #print 'LAMP!!!!!!!!!'
617
622
                        temp_data=file.read(STRUCT_SIZE_3FLOAT)
618
623
                        
619
624
                        x,y,z=unpack('<3f', temp_data)
620
625
                        new_chunk.bytes_read+=STRUCT_SIZE_3FLOAT
621
626
                        
622
 
                        contextLamp[0]= Object.New('Lamp')
623
 
                        contextLamp[1]= Lamp.New()
 
627
                        contextLamp[1]= bpy.data.lamps.new()
 
628
                        contextLamp[0]= SCN_OBJECTS.link(contextLamp[1])
624
629
                        contextLamp[0].link(contextLamp[1])
625
630
                        ##scn.link(contextLamp[0])
626
631
                        importedObjects.append(contextLamp[0])
627
632
                        
628
 
                        
629
 
                        
630
633
                        #print 'number of faces: ', num_faces
631
634
                        #print x,y,z
632
635
                        contextLamp[0].setLocation(x,y,z)
633
636
                        
634
637
                        # Reset matrix
635
 
                        ####contextMatrix= Mathutils.Matrix(); contextMatrix.identity() 
 
638
                        contextMatrix_rot= None
 
639
                        #contextMatrix_tx= None
636
640
                        #print contextLamp.name, 
637
641
                        
638
642
                        
647
651
                        contextMeshUV= None
648
652
                        #contextMesh.vertexUV= 1 # Make sticky coords.
649
653
                        # Reset matrix
650
 
                        ####contextMatrix= Blender.Mathutils.Matrix(); contextMatrix.identity()
 
654
                        contextMatrix_rot= None
 
655
                        #contextMatrix_tx= None
651
656
                
652
657
                elif (new_chunk.ID==OBJECT_VERTICES):
653
658
                        '''
717
722
                        contextMeshUV= [ getuv() for i in xrange(num_uv) ]
718
723
                
719
724
                elif (new_chunk.ID== OBJECT_TRANS_MATRIX):
 
725
                        # How do we know the matrix size? 54 == 4x4 48 == 4x3
720
726
                        temp_data=file.read(STRUCT_SIZE_4x3MAT)
721
 
                        data= list( unpack('<ffffffffffff', temp_data) )
722
 
                        new_chunk.bytes_read += STRUCT_SIZE_4x3MAT 
 
727
                        data= list( unpack('<ffffffffffff', temp_data)  )
 
728
                        new_chunk.bytes_read += STRUCT_SIZE_4x3MAT
723
729
                        
724
 
                        """contextMatrix= Blender.Mathutils.Matrix(\
 
730
                        contextMatrix_rot= Blender.Mathutils.Matrix(\
725
731
                         data[:3] + [0],\
726
732
                         data[3:6] + [0],\
727
733
                         data[6:9] + [0],\
728
734
                         data[9:] + [1])
729
 
                        """
730
 
                
 
735
                        
 
736
                        
 
737
                        '''
 
738
                        contextMatrix_rot= Blender.Mathutils.Matrix(\
 
739
                         data[:3] + [0],\
 
740
                         data[3:6] + [0],\
 
741
                         data[6:9] + [0],\
 
742
                         [0,0,0,1])
 
743
                        '''
 
744
                        
 
745
                        '''
 
746
                        contextMatrix_rot= Blender.Mathutils.Matrix(\
 
747
                         data[:3] ,\
 
748
                         data[3:6],\
 
749
                         data[6:9])
 
750
                        '''
 
751
                        
 
752
                        '''
 
753
                        contextMatrix_rot = Blender.Mathutils.Matrix()
 
754
                        m = 0
 
755
                        for j in xrange(4):
 
756
                                for i in xrange(3):
 
757
                                        contextMatrix_rot[j][i] = data[m]
 
758
                                        m+=1
 
759
                        
 
760
                        contextMatrix_rot[0][3]=0;
 
761
                        contextMatrix_rot[1][3]=0;
 
762
                        contextMatrix_rot[2][3]=0;
 
763
                        contextMatrix_rot[3][3]=1;
 
764
                        '''
 
765
                        
 
766
                        #contextMatrix_rot.resize4x4()
 
767
                        #print "MTX"
 
768
                        #print contextMatrix_rot
 
769
                        contextMatrix_rot.invert()
 
770
                        #print contextMatrix_rot
 
771
                        #contextMatrix_tx = Blender.Mathutils.TranslationMatrix(0.5 * Blender.Mathutils.Vector(data[9:]))
 
772
                        #contextMatrix_tx.invert()
 
773
                        
 
774
                        #tx.invert()
 
775
                        
 
776
                        #contextMatrix = contextMatrix * tx
 
777
                        #contextMatrix = contextMatrix  *tx
 
778
                        
731
779
                elif  (new_chunk.ID==MAT_MAP_FILENAME):
732
780
                        texture_name=read_string(file)
733
781
                        try:
809
857
        
810
858
        ##IMAGE_SEARCH
811
859
        
812
 
        scn= Scene.GetCurrent()
 
860
        scn= bpy.data.scenes.active
813
861
        SCN_OBJECTS = scn.objects
814
862
        SCN_OBJECTS.selected = [] # de select all
815
863
        
822
870
        
823
871
        # REMOVE DUMMYVERT, - remove this in the next release when blenders internal are fixed.
824
872
        
825
 
        '''
 
873
        
826
874
        for ob in importedObjects:
827
875
                if ob.type=='Mesh':
828
876
                        me= ob.getData(mesh=1)
829
877
                        me.verts.delete([me.verts[0],])
830
 
        '''
 
878
        
831
879
        # Done DUMMYVERT
832
880
        """
833
881
        if IMPORT_AS_INSTANCE:
892
940
        
893
941
 
894
942
DEBUG= False
895
 
 
896
943
if __name__=='__main__' and not DEBUG:
897
944
        Blender.Window.FileSelector(load_3ds, 'Import 3DS', '*.3ds')
898
945
 
899
946
# For testing compatibility
 
947
#load_3ds('/metavr/convert/vehicle/truck_002/TruckTanker1.3DS', False)
 
948
#load_3ds('/metavr/archive/convert/old/arranged_3ds_to_hpx-2/only-need-engine-trains/Engine2.3DS', False)
900
949
'''
901
950
else:
902
951
        # DEBUG ONLY
920
969
                        _3ds= _3ds[:-1]
921
970
                        print 'Importing', _3ds, '\nNUMBER', i, 'of', len(lines)
922
971
                        _3ds_file= _3ds.split('/')[-1].split('\\')[-1]
923
 
                        newScn= Scene.New(_3ds_file)
 
972
                        newScn= Blender.Scene.New(_3ds_file)
924
973
                        newScn.makeCurrent()
925
974
                        load_3ds(_3ds, False)
926
975