#!BPY """ Name: '3D Studio (.3ds)...' Blender: 241 Group: 'Import' Tooltip: 'Import from 3DS file format (.3ds)' """ __author__= ['Bob Holcomb', 'Richard L?rk?ng', 'Damien McGinnes', 'Campbell Barton'] __url__= ('blender', 'elysiun', 'http://www.gametutorials.com') __version__= '0.95' __bpydoc__= '''\ 3ds Importer This script imports a 3ds file and the materials into Blender for editing. Loader is based on 3ds loader from www.gametutorials.com (Thanks DigiBen). 0.95 by Campbell Barton
- Removed workarounds for Blender 2.41 - Mesh objects split by material- many 3ds objects used more then 16 per mesh. - Removed a lot of unneeded variable creation. 0.94 by Campbell Barton
- Face import tested to be about overall 16x speedup over 0.93. - Material importing speedup. - Tested with more models. - Support some corrupt models. 0.93 by Campbell Barton
- Tested with 400 3ds files from turbosquid and samples. - Tactfully ignore faces that used the same verts twice. - Rollback to 0.83 sloppy un-reorganized code, this broke UV coord loading. - Converted from NMesh to Mesh. - Faster and cleaner new names. - Use external comprehensive image loader. - Re intergrated 0.92 and 0.9 changes - Fixes for 2.41 compat. - Non textured faces do not use a texture flag. 0.92
- Added support for diffuse, alpha, spec, bump maps in a single material 0.9
- Reorganized code into object/material block functions
- Use of Matrix() to copy matrix data
- added support for material transparency
0.83 2005-08-07: Campell Barton - Aggressive image finding and case insensitivy for posisx systems. 0.82a 2005-07-22 - image texture loading (both for face uv and renderer) 0.82 - image texture loading (for face uv) 0.81a (fork- not 0.9) Campbell Barton 2005-06-08 - Simplified import code - Never overwrite data - Faster list handling - Leaves import selected 0.81 Damien McGinnes 2005-01-09 - handle missing images better 0.8 Damien McGinnes 2005-01-08 - copies sticky UV coords to face ones - handles images better - Recommend that you run 'RemoveDoubles' on each imported mesh after using this script ''' # Importing modules import Blender from Blender import Mesh, Scene, Object, Material, Image, Texture, Lamp, Mathutils from Blender.Mathutils import Vector import BPyImage reload( BPyImage ) import struct from struct import calcsize, unpack import os # If python version is less than 2.4, try to get set stuff from module import sys if ( (sys.version_info[0] <= 2) and (sys.version_info[1] < 4) ): from sets import Set as set #this script imports uvcoords as sticky vertex coords #this parameter enables copying these to face uv coords #which shold be more useful. #===========================================================================# # Returns unique name of object/mesh (stops overwriting existing meshes) # #===========================================================================# def getUniqueName(name): count= 0 newname= name[:19] while newname in getUniqueName.uniqueObNames: newname= '%s.%.3i' % (name[:15], count) count+=1 # Dont use again. getUniqueName.uniqueObNames.append(newname) return newname getUniqueName.uniqueObNames= Blender.NMesh.GetNames() + [ob.name for ob in Object.Get()] def createBlenderTexture(material, name, image): texture= Texture.New(name) texture.setType('Image') texture.image= image material.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL) ###################################################### # Data Structures ###################################################### #Some of the chunks that we will see #----- Primary Chunk, at the beginning of each file PRIMARY= long('0x4D4D',16) #------ Main Chunks OBJECTINFO = long('0x3D3D',16); #This gives the version of the mesh and is found right before the material and object information VERSION = long('0x0002',16); #This gives the version of the .3ds file EDITKEYFRAME= long('0xB000',16); #This is the header for all of the key frame info #------ sub defines of OBJECTINFO MATERIAL=45055 #0xAFFF // This stored the texture info OBJECT=16384 #0x4000 // This stores the faces, vertices, etc... #>------ sub defines of MATERIAL #------ sub defines of MATERIAL_BLOCK MAT_NAME = long('0xA000',16) # This holds the material name MAT_AMBIENT = long('0xA010',16) # Ambient color of the object/material MAT_DIFFUSE = long('0xA020',16) # This holds the color of the object/material MAT_SPECULAR = long('0xA030',16) # SPecular color of the object/material MAT_SHINESS = long('0xA040',16) # ?? MAT_TRANSPARENCY= long('0xA050',16) # Transparency value of material MAT_SELF_ILLUM = long('0xA080',16) # Self Illumination value of material MAT_WIRE = long('0xA085',16) # Only render's wireframe MAT_TEXTURE_MAP = long('0xA200',16) # This is a header for a new texture map MAT_SPECULAR_MAP= long('0xA204',16) # This is a header for a new specular map MAT_OPACITY_MAP = long('0xA210',16) # This is a header for a new opacity map MAT_REFLECTION_MAP= long('0xA220',16) # This is a header for a new reflection map MAT_BUMP_MAP = long('0xA230',16) # This is a header for a new bump map MAT_MAP_FILENAME = long('0xA300',16); # This holds the file name of the texture #>------ sub defines of OBJECT OBJECT_MESH = long('0x4100',16); # This lets us know that we are reading a new object OBJECT_LAMP = long('0x4600',16); # This lets un know we are reading a light object OBJECT_LAMP_SPOT = long('0x4610',16); # The light is a spotloght. OBJECT_LAMP_OFF = long('0x4620',16); # The light off. OBJECT_LAMP_ATTENUATE = long('0x4625',16); OBJECT_LAMP_RAYSHADE = long('0x4627',16); OBJECT_LAMP_SHADOWED = long('0x4630',16); OBJECT_LAMP_LOCAL_SHADOW = long('0x4640',16); OBJECT_LAMP_LOCAL_SHADOW2 = long('0x4641',16); OBJECT_LAMP_SEE_CONE = long('0x4650',16); OBJECT_LAMP_SPOT_RECTANGULAR= long('0x4651',16); OBJECT_LAMP_SPOT_OVERSHOOT= long('0x4652',16); OBJECT_LAMP_SPOT_PROJECTOR= long('0x4653',16); OBJECT_LAMP_EXCLUDE= long('0x4654',16); OBJECT_LAMP_RANGE= long('0x4655',16); OBJECT_LAMP_ROLL= long('0x4656',16); OBJECT_LAMP_SPOT_ASPECT= long('0x4657',16); OBJECT_LAMP_RAY_BIAS= long('0x4658',16); OBJECT_LAMP_INNER_RANGE= long('0x4659',16); OBJECT_LAMP_OUTER_RANGE= long('0x465A',16); OBJECT_LAMP_MULTIPLIER = long('0x465B',16); OBJECT_LAMP_AMBIENT_LIGHT = long('0x4680',16); OBJECT_CAMERA= long('0x4700',16); # This lets un know we are reading a camera object #>------ sub defines of CAMERA OBJECT_CAM_RANGES= long('0x4720',16); # The camera range values #>------ sub defines of OBJECT_MESH OBJECT_VERTICES = long('0x4110',16); # The objects vertices OBJECT_FACES = long('0x4120',16); # The objects faces OBJECT_MATERIAL = long('0x4130',16); # This is found if the object has a material, either texture map or color OBJECT_UV = long('0x4140',16); # The UV texture coordinates OBJECT_TRANS_MATRIX = long('0x4160',16); # The Object Matrix global scn scn= None #the chunk class class chunk: ID=0 length=0 bytes_read=0 #we don't read in the bytes_read, we compute that binary_format='10: print '/tError: Cannot add diffuse map. Too many textures' def process_next_chunk(file, previous_chunk, importedObjects): #print previous_chunk.bytes_read, 'BYTES READ' contextObName= None contextLamp= [None, None] # object, Data contextMaterial= None contextMatrix= Blender.Mathutils.Matrix(); contextMatrix.identity() contextMesh= None contextMeshMaterials= {} # matname:[face_idxs] TEXTURE_DICT={} MATDICT={} TEXMODE= Mesh.FaceModes['TEX'] # Localspace variable names, faster. STRUCT_SIZE_1CHAR= calcsize('c') STRUCT_SIZE_2FLOAT= calcsize('2f') STRUCT_SIZE_3FLOAT= calcsize('3f') STRUCT_SIZE_UNSIGNED_SHORT= calcsize('H') STRUCT_SIZE_4UNSIGNED_SHORT= calcsize('4H') STRUCT_SIZE_4x3MAT= calcsize('ffffffffffff') def putContextMesh(myContextMesh, myContextMeshMaterials): #print 'prtting myContextMesh', myContextMesh.name INV_MAT= Blender.Mathutils.Matrix(contextMatrix) INV_MAT.invert() contextMesh.transform(INV_MAT) materialFaces= set() # Now make copies with assigned materils. def makeMeshMaterialCopy(matName, faces): # Make a new mesh with only face the faces that use this material. faceVertUsers = [False] * len(myContextMesh.verts) ok=0 for fIdx in faces: for v in myContextMesh.faces[fIdx].v: faceVertUsers[v.index] = True if matName != None: # if matName is none then this is a set(), meaning we are using the untextured faces and do not need to store textured faces. materialFaces.add(fIdx) ok=1 if not ok: return myVertMapping = {} vertMappingIndex = 0 vertsToUse = [i for i in xrange(len(myContextMesh.verts)) if faceVertUsers[i]] myVertMapping = dict( [ (ii, i) for i, ii in enumerate(vertsToUse) ] ) bmesh = Mesh.New(contextMesh.name) if matName != None: bmat = MATDICT[matName][1] try: img= TEXTURE_DICT[bmat.name] except: img= None bmesh.materials= [bmat] else: img= None bmesh.verts.extend( [myContextMesh.verts[i].co for i in vertsToUse] ) bmesh.faces.extend( [ [ bmesh.verts[ myVertMapping[v.index]] for v in myContextMesh.faces[fIdx].v] for fIdx in faces ] ) if contextMeshUV or img: bmesh.faceUV= 1 for ii, i in enumerate(faces): targetFace= bmesh.faces[ii] if contextMeshUV: targetFace.uv= [contextMeshUV[v.index] for v in myContextMesh.faces[i].v] if img: targetFace.image= img tempName= contextObName + '_' + str(matName) # str because we may be None bmesh.name= tempName ob = Object.New('Mesh', tempName) ob.link(bmesh) ob.setMatrix(contextMatrix) importedObjects.append(ob) ##scn.link(ob) ##ob.Layers= scn.Layers ##ob.sel= 1 for matName, faces in myContextMeshMaterials.iteritems(): makeMeshMaterialCopy(matName, faces) if len(materialFaces)!=len(contextMesh.faces): # Invert material faces. makeMeshMaterialCopy(None, set(range(len( contextMesh.faces ))) - materialFaces) #raise 'Some UnMaterialed faces', len(contextMesh.faces) #a spare chunk new_chunk= chunk() temp_chunk= chunk() #loop through all the data for this chunk (previous chunk) and see what it is while (previous_chunk.bytes_read3): print '\tNon-Fatal Error: Version greater than 3, may not load correctly: ', version #is it an object info chunk? elif (new_chunk.ID==OBJECTINFO): #print 'elif (new_chunk.ID==OBJECTINFO):' # print 'found an OBJECTINFO chunk' process_next_chunk(file, new_chunk, importedObjects) #keep track of how much we read in the main chunk new_chunk.bytes_read+=temp_chunk.bytes_read #is it an object chunk? elif (new_chunk.ID==OBJECT): tempName= read_string(file) contextObName= getUniqueName( tempName ) new_chunk.bytes_read += len(tempName)+1 #is it a material chunk? elif (new_chunk.ID==MATERIAL): #print 'elif (new_chunk.ID==MATERIAL):' contextMaterial= Material.New() elif (new_chunk.ID==MAT_NAME): #print 'elif (new_chunk.ID==MAT_NAME):' material_name= read_string(file) #plus one for the null character that ended the string new_chunk.bytes_read+= len(material_name)+1 contextMaterial.name= material_name MATDICT[material_name]= (contextMaterial.name, contextMaterial) elif (new_chunk.ID==MAT_AMBIENT): #print 'elif (new_chunk.ID==MAT_AMBIENT):' read_chunk(file, temp_chunk) temp_data=file.read(calcsize('3B')) temp_chunk.bytes_read+= 3 contextMaterial.mirCol= [float(col)/255 for col in unpack('3B', temp_data)] # data [0,1,2] == rgb new_chunk.bytes_read+= temp_chunk.bytes_read elif (new_chunk.ID==MAT_DIFFUSE): #print 'elif (new_chunk.ID==MAT_DIFFUSE):' read_chunk(file, temp_chunk) temp_data=file.read(calcsize('3B')) temp_chunk.bytes_read+= 3 contextMaterial.rgbCol= [float(col)/255 for col in unpack('3B', temp_data)] # data [0,1,2] == rgb new_chunk.bytes_read+= temp_chunk.bytes_read elif (new_chunk.ID==MAT_SPECULAR): #print 'elif (new_chunk.ID==MAT_SPECULAR):' read_chunk(file, temp_chunk) temp_data= file.read(calcsize('3B')) temp_chunk.bytes_read+= 3 contextMaterial.specCol= [float(col)/255 for col in unpack('3B', temp_data)] # data [0,1,2] == rgb new_chunk.bytes_read+= temp_chunk.bytes_read elif (new_chunk.ID==MAT_TEXTURE_MAP): #print 'elif (new_chunk.ID==MAT_TEXTURE_MAP):' new_texture= Blender.Texture.New('Diffuse') new_texture.setType('Image') while (new_chunk.bytes_read /tmp/temp3ds_list') # os.system('find /storage/ -iname "*.3ds" > /tmp/temp3ds_list') print '...Done' file= open('/tmp/temp3ds_list', 'r') lines= file.readlines() file.close() def between(v,a,b): if v <= max(a,b) and v >= min(a,b): return True return False for i, _3ds in enumerate(lines): if between(i, 600, 700): _3ds= _3ds[:-1] print 'Importing', _3ds, '\nNUMBER', i, 'of', len(lines) _3ds_file= _3ds.split('/')[-1].split('\\')[-1] newScn= Scene.New(_3ds_file) newScn.makeCurrent() load_3ds(_3ds) print 'TOTAL TIME: %.6f' % (Blender.sys.time() - TIME) '''