- Removed workarounds for Blender 2.41

- Mesh objects split by material- many 3ds objects used more then 16 per mesh. and when a face looses its image texture its tedious to set again.
- Removed a lot of unneeded variable creation.
This commit is contained in:
Campbell Barton 2006-02-06 14:23:55 +00:00
parent 6f3c9c4275
commit c629128773

@ -6,10 +6,10 @@ 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.93"
__bpydoc__ = """\
__author__= ['Bob Holcomb', 'Richard L?rk?ng', 'Damien McGinnes', 'Campbell Barton']
__url__= ('blender', 'elysiun', 'http://www.gametutorials.com')
__version__= '0.95'
__bpydoc__= '''\
3ds Importer
@ -17,6 +17,11 @@ 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<br>
- 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<br>
- Face import tested to be about overall 16x speedup over 0.93.
- Material importing speedup.
@ -64,7 +69,7 @@ Loader is based on 3ds loader from www.gametutorials.com (Thanks DigiBen).
- handles images better
- Recommend that you run 'RemoveDoubles' on each imported mesh after using this script
"""
'''
# Importing modules
@ -72,9 +77,9 @@ 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
@ -111,12 +116,12 @@ def createBlenderTexture(material, name, image):
#Some of the chunks that we will see
#----- Primary Chunk, at the beginning of each file
PRIMARY= long("0x4D4D",16)
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
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
@ -124,59 +129,59 @@ 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_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
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_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
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
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
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
@ -188,7 +193,7 @@ class chunk:
bytes_read=0
#we don't read in the bytes_read, we compute that
binary_format="<HI"
binary_format='<HI'
def __init__(self):
self.ID=0
@ -196,15 +201,14 @@ class chunk:
self.bytes_read=0
def dump(self):
print "ID: ", self.ID
print "ID in hex: ", hex(self.ID)
print "length: ", self.length
print "bytes_read: ", self.bytes_read
print 'ID: ', self.ID
print 'ID in hex: ', hex(self.ID)
print 'length: ', self.length
print 'bytes_read: ', self.bytes_read
def read_chunk(file, chunk):
temp_data=file.read(struct.calcsize(chunk.binary_format))
data=struct.unpack(chunk.binary_format, temp_data)
temp_data=file.read(calcsize(chunk.binary_format))
data=unpack(chunk.binary_format, temp_data)
chunk.ID=data[0]
chunk.length=data[1]
#update the bytes read function
@ -214,24 +218,14 @@ def read_chunk(file, chunk):
#chunk.dump()
def read_string(file):
s=""
index=0
#print "reading a string"
#read in the characters till we get a null character
temp_data=file.read(1)
data=struct.unpack("c", temp_data)
s=s+(data[0])
#print "string: ",s
while(ord(s[index])!=0):
index+=1
temp_data=file.read(1)
data=struct.unpack("c", temp_data)
s=s+(data[0])
#print "string: ",s
s=''
while not s.endswith('\x00'):
s+=unpack( 'c', file.read(1) )[0]
#print 'string: ',s
#remove the null character from the string
the_string=s[:-1]
return the_string
return s[:-1]
######################################################
# IMPORT
@ -246,22 +240,22 @@ def process_next_object_chunk(file, previous_chunk):
def skip_to_end(file, skip_chunk):
buffer_size=skip_chunk.length-skip_chunk.bytes_read
binary_format=str(buffer_size)+"c"
temp_data=file.read(struct.calcsize(binary_format))
binary_format='%ic' % buffer_size
temp_data=file.read(calcsize(binary_format))
skip_chunk.bytes_read+=buffer_size
def add_texture_to_material(image, texture, material, mapto):
if mapto=="DIFFUSE":
if mapto=='DIFFUSE':
map=Texture.MapTo.COL
elif mapto=="SPECULAR":
elif mapto=='SPECULAR':
map=Texture.MapTo.SPEC
elif mapto=="OPACITY":
elif mapto=='OPACITY':
map=Texture.MapTo.ALPHA
elif mapto=="BUMP":
elif mapto=='BUMP':
map=Texture.MapTo.NOR
else:
print "/tError: Cannot map to ", mapto
raise '/tError: Cannot map to ', mapto
return
@ -275,57 +269,100 @@ def add_texture_to_material(image, texture, material, mapto):
else:
index+=1
if index>10:
print "/tError: Cannot add diffuse map. Too many textures"
print '/tError: Cannot add diffuse map. Too many textures'
def process_next_chunk(file, filename, previous_chunk, scn):
#print previous_chunk.bytes_read, "BYTES READ"
#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']
objectList = [] # Keep a list of imported objects.
# Localspace variable names, faster.
STRUCT_SIZE_1CHAR = struct.calcsize("c")
STRUCT_SIZE_2FLOAT = struct.calcsize("2f")
STRUCT_SIZE_3FLOAT = struct.calcsize("3f")
STRUCT_SIZE_UNSIGNED_SHORT = struct.calcsize("H")
STRUCT_SIZE_4UNSIGNED_SHORT = struct.calcsize("4H")
STRUCT_SIZE_4x3MAT = struct.calcsize("ffffffffffff")
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):
def putContextMesh(myContextMesh, myContextMeshMaterials):
#print 'prtting myContextMesh', myContextMesh.name
INV_MAT= Blender.Mathutils.Matrix(contextMatrix)
INV_MAT.invert()
contextMesh.transform(INV_MAT)
# Faces without an image can have the image flag disabled.
if myContextMesh.faceUV:
TEX_ON_FLAG = Mesh.FaceModes['TEX']
for f in myContextMesh.faces:
if not f.image:
f.mode &= ~TEX_ON_FLAG # disable tex flag.
for c in f.col:
c.b=c.g=c.r=255
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
newOb = Object.New('Mesh', contextMesh.name) # Meshes name is always a free object name too.
newOb.link(contextMesh)
scn.link(newOb)
newOb.Layers = scn.Layers
newOb.sel = 1
objectList.append(newOb) # last 2 recal normals
newOb.setMatrix(contextMatrix)
if not ok:
return
#Blender.Window.EditMode(1)
#Blender.Window.EditMode(0)
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
ob = Object.New('Mesh', contextMesh.name)
ob.link(bmesh)
ob.setMatrix(contextMatrix)
ob.Layers= scn.Layers
scn.link(ob)
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
@ -336,27 +373,26 @@ def process_next_chunk(file, filename, previous_chunk, scn):
while (previous_chunk.bytes_read<previous_chunk.length):
#print '\t', previous_chunk.bytes_read, 'keep going'
#read the next chunk
#print "reading a chunk"
#print 'reading a chunk'
read_chunk(file, new_chunk)
#is it a Version chunk?
if (new_chunk.ID==VERSION):
#print "if (new_chunk.ID==VERSION):"
#print "found a VERSION chunk"
#print 'if (new_chunk.ID==VERSION):'
#print 'found a VERSION chunk'
#read in the version of the file
#it's an unsigned short (H)
temp_data=file.read(struct.calcsize("I"))
data=struct.unpack("I", temp_data)
version=data[0]
temp_data= file.read(calcsize('I'))
version,= unpack('I', temp_data)
new_chunk.bytes_read+= 4 #read the 4 bytes for the version number
#this loader works with version 3 and below, but may not with 4 and above
if (version>3):
print "\tNon-Fatal Error: Version greater than 3, may not load correctly: ", version
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"
#print 'elif (new_chunk.ID==OBJECTINFO):'
# print 'found an OBJECTINFO chunk'
process_next_chunk(file, filename, new_chunk, scn)
#keep track of how much we read in the main chunk
@ -364,66 +400,61 @@ def process_next_chunk(file, filename, previous_chunk, scn):
#is it an object chunk?
elif (new_chunk.ID==OBJECT):
"elif (new_chunk.ID==OBJECT):"
'elif (new_chunk.ID==OBJECT):'
tempName= read_string(file)
contextObName= getUniqueName( tempName )
new_chunk.bytes_read += (len(tempName)+1)
new_chunk.bytes_read += len(tempName)+1
#is it a material chunk?
elif (new_chunk.ID==MATERIAL):
#print "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=""
#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)
new_chunk.bytes_read+= len(material_name)+1
contextMaterial.setName(material_name)
contextMaterial.name= material_name
MATDICT[material_name]= (contextMaterial.name, contextMaterial)
elif (new_chunk.ID==MAT_AMBIENT):
#print "elif (new_chunk.ID==MAT_AMBIENT):"
#print 'elif (new_chunk.ID==MAT_AMBIENT):'
read_chunk(file, temp_chunk)
temp_data=file.read(struct.calcsize("3B"))
data=struct.unpack("3B", temp_data)
temp_data=file.read(calcsize('3B'))
temp_chunk.bytes_read+= 3
contextMaterial.mirCol = [float(col)/255 for col in data] # data [0,1,2] == rgb
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):"
#print 'elif (new_chunk.ID==MAT_DIFFUSE):'
read_chunk(file, temp_chunk)
temp_data=file.read(struct.calcsize("3B"))
data=struct.unpack("3B", temp_data)
temp_data=file.read(calcsize('3B'))
temp_chunk.bytes_read+= 3
contextMaterial.rgbCol = [float(col)/255 for col in data] # data [0,1,2] == rgb
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):"
#print 'elif (new_chunk.ID==MAT_SPECULAR):'
read_chunk(file, temp_chunk)
temp_data=file.read(struct.calcsize("3B"))
data=struct.unpack("3B", temp_data)
temp_data= file.read(calcsize('3B'))
temp_chunk.bytes_read+= 3
contextMaterial.specCol = [float(col)/255 for col in data] # data [0,1,2] == rgb
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):"
#print 'elif (new_chunk.ID==MAT_TEXTURE_MAP):'
new_texture= Blender.Texture.New('Diffuse')
new_texture.setType('Image')
while (new_chunk.bytes_read<new_chunk.length):
#print "MAT_TEXTURE_MAP..while", new_chunk.bytes_read, new_chunk.length
#print 'MAT_TEXTURE_MAP..while', new_chunk.bytes_read, new_chunk.length
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name=""
texture_name=str(read_string(file))
texture_name=read_string(file)
img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
@ -433,18 +464,17 @@ def process_next_chunk(file, filename, previous_chunk, scn):
new_chunk.bytes_read+= temp_chunk.bytes_read
#add the map to the material in the right channel
add_texture_to_material(img, new_texture, contextMaterial, "DIFFUSE")
add_texture_to_material(img, new_texture, contextMaterial, 'DIFFUSE')
elif (new_chunk.ID==MAT_SPECULAR_MAP):
#print "elif (new_chunk.ID==MAT_SPECULAR_MAP):"
#print 'elif (new_chunk.ID==MAT_SPECULAR_MAP):'
new_texture= Blender.Texture.New('Specular')
new_texture.setType('Image')
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name=""
texture_name=str(read_string(file))
texture_name= read_string(file)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
new_chunk.bytes_read+= (len(texture_name)+1) #plus one for the null character that gets removed
else:
@ -453,18 +483,17 @@ def process_next_chunk(file, filename, previous_chunk, scn):
new_chunk.bytes_read+= temp_chunk.bytes_read
#add the map to the material in the right channel
add_texture_to_material(img, new_texture, contextMaterial, "SPECULAR")
add_texture_to_material(img, new_texture, contextMaterial, 'SPECULAR')
elif (new_chunk.ID==MAT_OPACITY_MAP):
#print "new_texture=Blender.Texture.New('Opacity')"
#print 'new_texture=Blender.Texture.New('Opacity')'
new_texture= Blender.Texture.New('Opacity')
new_texture.setType('Image')
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name=""
texture_name=str(read_string(file))
texture_name= read_string(file)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
@ -473,18 +502,17 @@ def process_next_chunk(file, filename, previous_chunk, scn):
new_chunk.bytes_read+= temp_chunk.bytes_read
#add the map to the material in the right channel
add_texture_to_material(img, new_texture, contextMaterial, "OPACITY")
add_texture_to_material(img, new_texture, contextMaterial, 'OPACITY')
elif (new_chunk.ID==MAT_BUMP_MAP):
#print "elif (new_chunk.ID==MAT_BUMP_MAP):"
#print 'elif (new_chunk.ID==MAT_BUMP_MAP):'
new_texture= Blender.Texture.New('Bump')
new_texture.setType('Image')
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name=""
texture_name=str(read_string(file))
texture_name= read_string(file)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
@ -493,15 +521,15 @@ def process_next_chunk(file, filename, previous_chunk, scn):
new_chunk.bytes_read+=temp_chunk.bytes_read
#add the map to the material in the right channel
add_texture_to_material(img, new_texture, contextMaterial, "BUMP")
add_texture_to_material(img, new_texture, contextMaterial, 'BUMP')
elif (new_chunk.ID==MAT_TRANSPARENCY):
#print "elif (new_chunk.ID==MAT_TRANSPARENCY):"
#print 'elif (new_chunk.ID==MAT_TRANSPARENCY):'
read_chunk(file, temp_chunk)
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
data=struct.unpack("H", temp_data)
temp_chunk.bytes_read+=2
contextMaterial.setAlpha(1-(float(data[0])/100))
contextMaterial.alpha= 1-(float(unpack('H', temp_data)[0])/100)
new_chunk.bytes_read+=temp_chunk.bytes_read
@ -510,10 +538,8 @@ def process_next_chunk(file, filename, previous_chunk, scn):
#print 'LAMP!!!!!!!!!'
temp_data=file.read(STRUCT_SIZE_3FLOAT)
data=struct.unpack("3f", temp_data)
x,y,z=unpack('3f', temp_data)
new_chunk.bytes_read+=STRUCT_SIZE_3FLOAT
x,y,z =tuple(data)
contextLamp[0]= Object.New('Lamp')
contextLamp[1]= Lamp.New()
@ -522,62 +548,63 @@ def process_next_chunk(file, filename, previous_chunk, scn):
#print "number of faces: ", num_faces
#print 'number of faces: ', num_faces
#print x,y,z
contextLamp[0].setLocation(x,y,z)
# Reset matrix
contextMatrix= Mathutils.Matrix(); contextMatrix.identity()
#print contextLamp.name, '"""""""'
#print contextLamp.name,
elif (new_chunk.ID==OBJECT_MESH):
# print "Found an OBJECT_MESH chunk"
# print 'Found an OBJECT_MESH chunk'
if contextMesh != None: # Write context mesh if we have one.
putContextMesh(contextMesh)
putContextMesh(contextMesh, contextMeshMaterials)
contextMesh= Mesh.New()
contextMeshMaterials= {} # matname:[face_idxs]
contextMeshUV= None
#contextMesh.vertexUV= 1 # Make sticky coords.
# Reset matrix
contextMatrix= Blender.Mathutils.Matrix(); contextMatrix.identity()
elif (new_chunk.ID==OBJECT_VERTICES):
# print "elif (new_chunk.ID==OBJECT_VERTICES):"
# print 'elif (new_chunk.ID==OBJECT_VERTICES):'
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
data=struct.unpack("H", temp_data)
num_verts,=unpack('H', temp_data)
new_chunk.bytes_read+=2
num_verts=data[0]
# print "number of verts: ", num_verts
# print 'number of verts: ', num_verts
def getvert():
temp_data=file.read(STRUCT_SIZE_3FLOAT)
new_chunk.bytes_read += STRUCT_SIZE_3FLOAT #12: 3 floats x 4 bytes each
data=struct.unpack("3f", temp_data)
return Vector(data[0],data[1],data[2])
return Vector(unpack('3f', temp_data))
contextMesh.verts.extend( [getvert() for i in xrange(num_verts)] )
#print "object verts: bytes read: ", new_chunk.bytes_read
#print 'object verts: bytes read: ', new_chunk.bytes_read
elif (new_chunk.ID==OBJECT_FACES):
# print "elif (new_chunk.ID==OBJECT_FACES):"
# print 'elif (new_chunk.ID==OBJECT_FACES):'
temp_data= file.read(STRUCT_SIZE_UNSIGNED_SHORT)
data=struct.unpack("H", temp_data)
num_faces,= unpack('H', temp_data)
new_chunk.bytes_read+= 2
num_faces=data[0]
#print "number of faces: ", num_faces
#print 'number of faces: ', num_faces
def getface():
# print '\ngetting a face'
temp_data= file.read(STRUCT_SIZE_4UNSIGNED_SHORT)
new_chunk.bytes_read+= STRUCT_SIZE_4UNSIGNED_SHORT #4 short ints x 2 bytes each
v1,v2,v3, dummy =struct.unpack("4H", temp_data)
v1,v2,v3,dummy= unpack('4H', temp_data)
if v1==v2 or v1==v3 or v2==v3:
return None
return contextMesh.verts[v1], contextMesh.verts[v2], contextMesh.verts[v3]
faces= [ getface() for i in xrange(num_faces) ]
contextMesh.faces.extend( [f for f in faces if f] )
facesExtend= [ f for f in faces if f ]
if facesExtend:
contextMesh.faces.extend( facesExtend )
# face mapping so duplicate faces dont mess us up.
if len(contextMesh.faces)==len(faces):
@ -590,7 +617,7 @@ def process_next_chunk(file, filename, previous_chunk, scn):
contextFaceMapping[i]= None
meshFaceOffset+= 1
else:
#print "DOUBLE FACE", '\tfacelen', len(f), i, num_faces, (i-meshFaceOffset)
#print 'DOUBLE FACE', '\tfacelen', len(f), i, num_faces, (i-meshFaceOffset)
#print i-meshFaceOffset, len(contextMesh.faces)q
if len(contextMesh.faces) <= i-meshFaceOffset: # SHOULD NEVER HAPPEN, CORRUPS 3DS?
contextFaceMapping[i]= None
@ -609,117 +636,54 @@ def process_next_chunk(file, filename, previous_chunk, scn):
contextFaceMapping[i]= None
#print 'LENFACEWS', len(contextMesh.faces), num_faces
if contextMeshUV:
contextMesh.faceUV= 1
for f in contextMesh.faces:
f.uv = [contextMeshUV[v.index] for v in f.v ]
#print "object faces: bytes read: ", new_chunk.bytes_read
elif (new_chunk.ID==OBJECT_MATERIAL):
# print "elif (new_chunk.ID==OBJECT_MATERIAL):"
material_name=""
# print 'elif (new_chunk.ID==OBJECT_MATERIAL):'
material_name= read_string(file)
new_chunk.bytes_read += len(material_name)+1 # remove 1 null character.
#look up the material in all the materials
material_found=0
try:
mat_name, mat = MATDICT[material_name]
except:
mat_name = mat = None
tempMatFaceIndexList = contextMeshMaterials[material_name]= []
msh_materials = contextMesh.materials
if mat:
meshHasMat = 0
for i, myMat in enumerate(msh_materials):
if myMat.name == mat_name:
meshHasMat = 1
mat_index = i
break
if not meshHasMat:
if len(msh_materials) > 15:
print "\tCant assign more than 16 materials per mesh, keep going..."
material_found = 0
else:
mat_index = len(msh_materials)
contextMesh.materials = msh_materials + [mat]
material_found=1
else:
material_found=0
# print "Not matching: ", mat.name, " and ", material_name
#print contextMesh.materials
if material_found == 1:
contextMaterial = mat
#read the number of faces using this material
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
data=struct.unpack("H", temp_data)
num_faces_using_mat,= unpack('H', temp_data)
new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
num_faces_using_mat=data[0]
try:
img = TEXTURE_DICT[ MATDICT[contextMaterial.name][0] ]
contextMesh.faceUV = 1
except KeyError:
img = None
#list of faces using mat
for face_counter in xrange(num_faces_using_mat):
temp_data= file.read(STRUCT_SIZE_UNSIGNED_SHORT)
new_chunk.bytes_read+= STRUCT_SIZE_UNSIGNED_SHORT
data=struct.unpack("H", temp_data)
faceIndex,= unpack('H', temp_data)
# We dont have to use context face mapping.
if contextFaceMapping:
facemap= contextFaceMapping[data[0]]
if facemap != None:
face= contextMesh.faces[contextFaceMapping[data[0]]]
meshFaceIndex= contextFaceMapping[faceIndex]
else:
face= None
else:
face = contextMesh.faces[data[0]]
meshFaceIndex= faceIndex
if face:
face.mat = mat_index
if img:
#print 'Assigning image', img.name
face.mode |= TEXMODE
face.image = img
if meshFaceIndex != None:
tempMatFaceIndexList.append(meshFaceIndex)
else:
#read past the information about the material you couldn't find
#print "Couldn't find material. Reading past face material info"
buffer_size=new_chunk.length-new_chunk.bytes_read
binary_format=str(buffer_size)+"c"
temp_data=file.read(struct.calcsize(binary_format))
new_chunk.bytes_read+=buffer_size
#print "object mat: bytes read: ", new_chunk.bytes_read
tempMatFaceIndexList.sort()
del tempMatFaceIndexList
#look up the material in all the materials
elif (new_chunk.ID==OBJECT_UV):
# print "elif (new_chunk.ID == OBJECT_UV):"
# print 'elif (new_chunk.ID==OBJECT_UV):'
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
data=struct.unpack("H", temp_data)
num_uv,=unpack('H', temp_data)
new_chunk.bytes_read+= 2
num_uv=data[0]
def getuv():
temp_data=file.read(STRUCT_SIZE_2FLOAT)
new_chunk.bytes_read += STRUCT_SIZE_2FLOAT #2 float x 4 bytes each
data=struct.unpack("2f", temp_data)
return Vector(data[0], data[1])
return Vector( unpack('2f', temp_data) )
contextMeshUV= [ getuv() for i in xrange(num_uv) ]
elif (new_chunk.ID== OBJECT_TRANS_MATRIX):
# print "elif (new_chunk.ID == OBJECT_TRANS_MATRIX):"
# print 'elif (new_chunk.ID== OBJECT_TRANS_MATRIX):'
temp_data=file.read(STRUCT_SIZE_4x3MAT)
data = list( struct.unpack("ffffffffffff", temp_data) )
data= list( unpack('ffffffffffff', temp_data) )
new_chunk.bytes_read += STRUCT_SIZE_4x3MAT
contextMatrix= Blender.Mathutils.Matrix(\
@ -728,29 +692,34 @@ def process_next_chunk(file, filename, previous_chunk, scn):
data[6:9] + [0],\
data[9:] + [1])
elif (new_chunk.ID==MAT_MAP_FILENAME):
raise 'Hello--'
texture_name=read_string(file)
try:
TEXTURE_DICT[contextMaterial.name]
except:
img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
new_chunk.bytes_read+= len(texture_name)+1 #plus one for the null character that gets removed
else: #(new_chunk.ID!=VERSION or new_chunk.ID!=OBJECTINFO or new_chunk.ID!=OBJECT or new_chunk.ID!=MATERIAL):
# print "skipping to end of this chunk"
# print 'skipping to end of this chunk'
buffer_size=new_chunk.length-new_chunk.bytes_read
binary_format=str(buffer_size)+"c"
temp_data=file.read(struct.calcsize(binary_format))
binary_format='%ic' % buffer_size
temp_data=file.read(calcsize(binary_format))
new_chunk.bytes_read+=buffer_size
#update the previous chunk bytes read
# print "previous_chunk.bytes_read += new_chunk.bytes_read"
# print 'previous_chunk.bytes_read += new_chunk.bytes_read'
# print previous_chunk.bytes_read, new_chunk.bytes_read
previous_chunk.bytes_read += new_chunk.bytes_read
## print "Bytes left in this chunk: ", previous_chunk.length-previous_chunk.bytes_read
## print 'Bytes left in this chunk: ', previous_chunk.length-previous_chunk.bytes_read
# FINISHED LOOP
# There will be a number of objects still not added
if contextMesh != None:
putContextMesh(contextMesh)
for ob in objectList:
ob.sel = 1
putContextMesh(contextMesh, contextMeshMaterials)
def load_3ds(filename):
print '\n\nImporting "%s"' % filename
@ -764,13 +733,13 @@ def load_3ds(filename):
FILENAME=filename
current_chunk=chunk()
file=open(filename,"rb")
file=open(filename,'rb')
#here we go!
# print "reading the first chunk"
# print 'reading the first chunk'
read_chunk(file, current_chunk)
if (current_chunk.ID!=PRIMARY):
print "\tFatal Error: Not a valid 3ds file: ", filename
print '\tFatal Error: Not a valid 3ds file: ', filename
file.close()
return
@ -780,29 +749,34 @@ def load_3ds(filename):
print 'finished importing: "%s" in %.4f sec.' % (filename, (Blender.sys.time()-time1))
file.close()
if __name__=='__main__':
Blender.Window.FileSelector(load_3ds, "Import 3DS", '*.3ds')
Blender.Window.FileSelector(load_3ds, 'Import 3DS', '*.3ds')
# For testing compatibility
"""
'''
TIME= Blender.sys.time()
import os
print "Searching for files"
print 'Searching for files'
os.system('find /metavr/ -iname "*.3ds" > /tmp/temp3ds_list')
print "Done"
# 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 i > 817:
if between(i, 600, 700):
_3ds= _3ds[:-1]
print "Importing", _3ds, '\nNUMBER', i, 'of', len(lines)
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)
"""
print 'TOTAL TIME: %.6f' % (Blender.sys.time() - TIME)
'''