- 3ds importer working (without the "scale to size" option)

- changing object.matrix now updates object.loc/rot/scale
- added bpy.data.add_lamp()
This commit is contained in:
Arystanbek Dyussenov
2009-08-13 11:14:06 +00:00
parent 6a056e4717
commit 44ddff5c51
6 changed files with 552 additions and 389 deletions

View File

@@ -125,9 +125,14 @@ Loader is based on 3ds loader from www.gametutorials.com (Thanks DigiBen).
# Importing modules
from struct import calcsize, unpack
import os
import time
import struct
from import_obj import unpack_face_list, load_image
import bpy
import Mathutils
# import Blender
# from Blender import Mesh, Object, Material, Image, Texture, Lamp, Mathutils
@@ -149,7 +154,7 @@ import bpy
# except:
# from sets import Set as set
BOUNDS_3DS= []
BOUNDS_3DS = []
#this script imports uvcoords as sticky vertex coords
@@ -157,9 +162,9 @@ BOUNDS_3DS= []
#which shold be more useful.
def createBlenderTexture(material, name, image):
texture= bpy.data.textures.new(name)
texture = bpy.data.textures.new(name)
texture.setType('Image')
texture.image= image
texture.image = image
material.setTexture(0, texture, Texture.TexCo.UV, Texture.MapTo.COL)
@@ -170,7 +175,7 @@ def createBlenderTexture(material, name, image):
#Some of the chunks that we will see
#----- Primary Chunk, at the beginning of each file
PRIMARY= int('0x4D4D',16)
PRIMARY = int('0x4D4D',16)
#------ Main Chunks
OBJECTINFO = int('0x3D3D',16); #This gives the version of the mesh and is found right before the material and object information
@@ -178,8 +183,8 @@ VERSION = int('0x0002',16); #This gives the version of the .3ds f
EDITKEYFRAME= int('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...
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
@@ -213,16 +218,16 @@ OBJECT_LAMP_SHADOWED = int('0x4630',16);
OBJECT_LAMP_LOCAL_SHADOW = int('0x4640',16);
OBJECT_LAMP_LOCAL_SHADOW2 = int('0x4641',16);
OBJECT_LAMP_SEE_CONE = int('0x4650',16);
OBJECT_LAMP_SPOT_RECTANGULAR= int('0x4651',16);
OBJECT_LAMP_SPOT_OVERSHOOT= int('0x4652',16);
OBJECT_LAMP_SPOT_PROJECTOR= int('0x4653',16);
OBJECT_LAMP_EXCLUDE= int('0x4654',16);
OBJECT_LAMP_RANGE= int('0x4655',16);
OBJECT_LAMP_ROLL= int('0x4656',16);
OBJECT_LAMP_SPOT_ASPECT= int('0x4657',16);
OBJECT_LAMP_RAY_BIAS= int('0x4658',16);
OBJECT_LAMP_INNER_RANGE= int('0x4659',16);
OBJECT_LAMP_OUTER_RANGE= int('0x465A',16);
OBJECT_LAMP_SPOT_RECTANGULAR = int('0x4651',16);
OBJECT_LAMP_SPOT_OVERSHOOT = int('0x4652',16);
OBJECT_LAMP_SPOT_PROJECTOR = int('0x4653',16);
OBJECT_LAMP_EXCLUDE = int('0x4654',16);
OBJECT_LAMP_RANGE = int('0x4655',16);
OBJECT_LAMP_ROLL = int('0x4656',16);
OBJECT_LAMP_SPOT_ASPECT = int('0x4657',16);
OBJECT_LAMP_RAY_BIAS = int('0x4658',16);
OBJECT_LAMP_INNER_RANGE = int('0x4659',16);
OBJECT_LAMP_OUTER_RANGE = int('0x465A',16);
OBJECT_LAMP_MULTIPLIER = int('0x465B',16);
OBJECT_LAMP_AMBIENT_LIGHT = int('0x4680',16);
@@ -241,21 +246,21 @@ OBJECT_UV = int('0x4140',16); # The UV texture coordinates
OBJECT_TRANS_MATRIX = int('0x4160',16); # The Object Matrix
global scn
scn= None
scn = None
#the chunk class
class chunk:
ID=0
length=0
bytes_read=0
ID = 0
length = 0
bytes_read = 0
#we don't read in the bytes_read, we compute that
binary_format='<HI'
def __init__(self):
self.ID=0
self.length=0
self.bytes_read=0
self.ID = 0
self.length = 0
self.bytes_read = 0
def dump(self):
print('ID: ', self.ID)
@@ -264,95 +269,107 @@ class chunk:
print('bytes_read: ', self.bytes_read)
def read_chunk(file, chunk):
temp_data=file.read(calcsize(chunk.binary_format))
data=unpack(chunk.binary_format, temp_data)
chunk.ID=data[0]
chunk.length=data[1]
temp_data = file.read(struct.calcsize(chunk.binary_format))
data = struct.unpack(chunk.binary_format, temp_data)
chunk.ID = data[0]
chunk.length = data[1]
#update the bytes read function
chunk.bytes_read=6
chunk.bytes_read = 6
#if debugging
#chunk.dump()
def read_string(file):
#read in the characters till we get a null character
s=''
while not s.endswith('\x00'):
s+=unpack( '<c', file.read(1) )[0]
s = b''
# s = ''
while not s.endswith(b'\x00'):
# while not s.endswith('\x00'):
s += struct.unpack('<c', file.read(1))[0]
# s += struct.unpack( '<c', file.read(1) )[0]
#print 'string: ',s
s = str(s[:-1], 'ASCII')
# print("read string", s)
#remove the null character from the string
return s[:-1]
return s
# return s[:-1]
######################################################
# IMPORT
######################################################
def process_next_object_chunk(file, previous_chunk):
new_chunk=chunk()
temp_chunk=chunk()
new_chunk = chunk()
temp_chunk = chunk()
while (previous_chunk.bytes_read<previous_chunk.length):
while (previous_chunk.bytes_read < previous_chunk.length):
#read the next chunk
read_chunk(file, new_chunk)
def skip_to_end(file, skip_chunk):
buffer_size=skip_chunk.length-skip_chunk.bytes_read
buffer_size = skip_chunk.length - skip_chunk.bytes_read
binary_format='%ic' % buffer_size
temp_data=file.read(calcsize(binary_format))
skip_chunk.bytes_read+=buffer_size
temp_data = file.read(struct.calcsize(binary_format))
skip_chunk.bytes_read += buffer_size
def add_texture_to_material(image, texture, material, mapto):
if mapto=='DIFFUSE':
map=Texture.MapTo.COL
elif mapto=='SPECULAR':
map=Texture.MapTo.SPEC
elif mapto=='OPACITY':
map=Texture.MapTo.ALPHA
elif mapto=='BUMP':
map=Texture.MapTo.NOR
else:
# if mapto=='DIFFUSE':
# map = Texture.MapTo.COL
# elif mapto=='SPECULAR':
# map = Texture.MapTo.SPEC
# elif mapto=='OPACITY':
# map = Texture.MapTo.ALPHA
# elif mapto=='BUMP':
# map = Texture.MapTo.NOR
# else:
if mapto not in ("COLOR", "SPECULARITY", "ALPHA", "NORMAL"):
print('/tError: Cannot map to "%s"\n\tassuming diffuse color. modify material "%s" later.' % (mapto, material.name))
map=Texture.MapTo.COL
mapto = "COLOR"
# map = Texture.MapTo.COL
if image: texture.setImage(image) # double check its an image.
free_tex_slots= [i for i, tex in enumerate( material.getTextures() ) if tex==None]
if not free_tex_slots:
print('/tError: Cannot add "%s" map. 10 Texture slots alredy used.' % mapto)
else:
material.setTexture(free_tex_slots[0],texture,Texture.TexCo.UV,map)
if image: texture.image = image
# if image: texture.setImage(image) # double check its an image.
material.add_texture(texture, "UV", mapto)
# free_tex_slots = [i for i, tex in enumerate( material.getTextures() ) if tex == None]
# if not free_tex_slots:
# print('/tError: Cannot add "%s" map. 10 Texture slots alredy used.' % mapto)
# else:
# material.setTexture(free_tex_slots[0],texture,Texture.TexCo.UV,map)
def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
#print previous_chunk.bytes_read, 'BYTES READ'
contextObName= None
contextLamp= [None, None] # object, Data
contextMaterial= None
contextMatrix_rot= None # Blender.Mathutils.Matrix(); contextMatrix.identity()
#contextMatrix_tx= None # Blender.Mathutils.Matrix(); contextMatrix.identity()
contextMesh_vertls= None
contextMesh_facels= None
contextMeshMaterials= {} # matname:[face_idxs]
contextMeshUV= None
contextObName = None
contextLamp = [None, None] # object, Data
contextMaterial = None
contextMatrix_rot = None # Blender.Mathutils.Matrix(); contextMatrix.identity()
#contextMatrix_tx = None # Blender.Mathutils.Matrix(); contextMatrix.identity()
contextMesh_vertls = None
contextMesh_facels = None
contextMeshMaterials = {} # matname:[face_idxs]
contextMeshUV = None
TEXTURE_DICT={}
MATDICT={}
TEXMODE= Mesh.FaceModes['TEX']
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')
_STRUCT_SIZE_4x3MAT= calcsize('fffffffffffff')
# STRUCT_SIZE_4x3MAT= calcsize('ffffffffffff')
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_4x3MAT = struct.calcsize('fffffffffffff')
# STRUCT_SIZE_4x3MAT = calcsize('ffffffffffff')
# print STRUCT_SIZE_4x3MAT, ' STRUCT_SIZE_4x3MAT'
def putContextMesh(myContextMesh_vertls, myContextMesh_facels, myContextMeshMaterials):
materialFaces= set() # faces that have a material. Can optimize?
materialFaces = set() # faces that have a material. Can optimize?
# Now make copies with assigned materils.
@@ -363,13 +380,13 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
'''
faceVertUsers = [False] * len(myContextMesh_vertls)
ok=0
ok = 0
for fIdx in faces:
for vindex in myContextMesh_facels[fIdx]:
faceVertUsers[vindex] = 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
ok = 1
if not ok:
return
@@ -381,370 +398,460 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
myVertMapping = dict( [ (ii, i) for i, ii in enumerate(vertsToUse) ] )
tempName= '%s_%s' % (contextObName, matName) # matName may be None.
bmesh = bpy.data.meshes.new(tempName)
bmesh = bpy.data.add_mesh(tempName)
# bmesh = bpy.data.meshes.new(tempName)
if matName == None:
img= None
img = None
else:
bmat = MATDICT[matName][1]
bmesh.materials= [bmat]
try: img= TEXTURE_DICT[bmat.name]
except: img= None
bmesh.add_material(bmat)
# bmesh.materials = [bmat]
try: img = TEXTURE_DICT[bmat.name]
except: img = None
bmesh_verts = bmesh.verts
bmesh_verts.extend( [Vector()] )
bmesh_verts.extend( [myContextMesh_vertls[i] for i in vertsToUse] )
# +1 because of DUMMYVERT
face_mapping= bmesh.faces.extend( [ [ bmesh_verts[ myVertMapping[vindex]+1] for vindex in myContextMesh_facels[fIdx]] for fIdx in faces ], indexList=True )
if bmesh.faces and (contextMeshUV or img):
bmesh.faceUV= 1
for ii, i in enumerate(faces):
# Mapped index- faces may have not been added- if so, then map to the correct index
# BUGGY API - face_mapping is not always the right length
map_index= face_mapping[ii]
if map_index != None:
targetFace= bmesh.faces[map_index]
if contextMeshUV:
# v.index-1 because of the DUMMYVERT
targetFace.uv= [contextMeshUV[vindex] for vindex in myContextMesh_facels[i]]
if img:
targetFace.image= img
# bmesh_verts = bmesh.verts
if len(vertsToUse):
bmesh.add_geometry(len(vertsToUse), 0, len(faces))
# XXX why add extra vertex?
# bmesh_verts.extend( [Vector()] )
bmesh.verts.foreach_set("co", [x for tup in [myContextMesh_vertls[i] for i in vertsToUse] for x in tup])
# bmesh_verts.extend( [myContextMesh_vertls[i] for i in vertsToUse] )
# +1 because of DUMMYVERT
bmesh.faces.foreach_set("verts", unpack_face_list([[myVertMapping[vindex] for vindex in myContextMesh_facels[fIdx]] for fIdx in faces]))
# face_mapping = bmesh.faces.extend( [ [ bmesh_verts[ myVertMapping[vindex]+1] for vindex in myContextMesh_facels[fIdx]] for fIdx in faces ], indexList=True )
if bmesh.faces and (contextMeshUV or img):
bmesh.add_uv_texture()
# bmesh.faceUV = 1
for ii, i in enumerate(faces):
# Mapped index- faces may have not been added- if so, then map to the correct index
# BUGGY API - face_mapping is not always the right length
# map_index = face_mapping[ii]
if 1:
# if map_index != None:
targetFace = bmesh.faces[ii]
# targetFace = bmesh.faces[map_index]
uf = bmesh.active_uv_texture.data[ii]
if contextMeshUV:
# v.index-1 because of the DUMMYVERT
uvs = [contextMeshUV[vindex] for vindex in myContextMesh_facels[i]]
if len(myContextMesh_facels[i]) == 3:
uf.uv1, uf.uv2, uf.uv3, uf.uv4 = uvs + [(0.0, 0.0)]
else:
uf.uv1, uf.uv2, uf.uv3, uf.uv4 = uvs
# targetFace.uv = [contextMeshUV[vindex] for vindex in myContextMesh_facels[i]]
if img:
uf.image = img
# targetFace.image = img
# bmesh.transform(contextMatrix)
ob = SCN_OBJECTS.new(bmesh, tempName)
ob = bpy.data.add_object("MESH", tempName)
ob.data = bmesh
SCN.add_object(ob)
# ob = SCN_OBJECTS.new(bmesh, tempName)
'''
if contextMatrix_tx:
ob.setMatrix(contextMatrix_tx)
'''
if contextMatrix_rot:
ob.setMatrix(contextMatrix_rot)
ob.matrix = [x for row in contextMatrix_rot for x in row]
# ob.setMatrix(contextMatrix_rot)
importedObjects.append(ob)
bmesh.calcNormals()
bmesh.update()
# bmesh.calcNormals()
for matName, faces in myContextMeshMaterials.items():
makeMeshMaterialCopy(matName, faces)
if len(materialFaces)!=len(myContextMesh_facels):
if len(materialFaces) != len(myContextMesh_facels):
# Invert material faces.
makeMeshMaterialCopy(None, set(range(len( myContextMesh_facels ))) - materialFaces)
#raise 'Some UnMaterialed faces', len(contextMesh.faces)
#a spare chunk
new_chunk= chunk()
temp_chunk= chunk()
new_chunk = chunk()
temp_chunk = chunk()
CreateBlenderObject = False
def read_float_color(temp_chunk):
temp_data = file.read(struct.calcsize('3f'))
temp_chunk.bytes_read += 12
return [float(col) for col in struct.unpack('<3f', temp_data)]
def read_byte_color(temp_chunk):
temp_data = file.read(struct.calcsize('3B'))
temp_chunk.bytes_read += 3
return [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
def read_texture(new_chunk, temp_chunk, name, mapto):
new_texture = bpy.data.add_texture('Diffuse')
new_texture.type = 'IMAGE'
img = None
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 = read_string(file)
img = TEXTURE_DICT[contextMaterial.name] = load_image(texture_name, dirname)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
skip_to_end(file, temp_chunk)
new_chunk.bytes_read += temp_chunk.bytes_read
# add the map to the material in the right channel
if img:
add_texture_to_material(img, new_texture, contextMaterial, mapto)
dirname = os.path.dirname(FILENAME)
#loop through all the data for this chunk (previous chunk) and see what it is
while (previous_chunk.bytes_read<previous_chunk.length):
while (previous_chunk.bytes_read < previous_chunk.length):
#print '\t', previous_chunk.bytes_read, 'keep going'
#read the next 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):'
if (new_chunk.ID == VERSION):
#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(calcsize('I'))
version = unpack('<I', temp_data)[0]
new_chunk.bytes_read+= 4 #read the 4 bytes for the version number
temp_data = file.read(struct.calcsize('I'))
version = struct.unpack('<I', temp_data)[0]
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):
if (version > 3):
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):'
elif (new_chunk.ID == OBJECTINFO):
#print 'elif (new_chunk.ID == OBJECTINFO):'
# print 'found an OBJECTINFO chunk'
process_next_chunk(file, new_chunk, importedObjects, IMAGE_SEARCH)
#keep track of how much we read in the main chunk
new_chunk.bytes_read+=temp_chunk.bytes_read
new_chunk.bytes_read += temp_chunk.bytes_read
#is it an object chunk?
elif (new_chunk.ID==OBJECT):
elif (new_chunk.ID == OBJECT):
if CreateBlenderObject:
putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
contextMesh_vertls= []; contextMesh_facels= []
contextMesh_vertls = []; contextMesh_facels = []
## preparando para receber o proximo objeto
contextMeshMaterials= {} # matname:[face_idxs]
contextMeshUV= None
#contextMesh.vertexUV= 1 # Make sticky coords.
contextMeshMaterials = {} # matname:[face_idxs]
contextMeshUV = None
#contextMesh.vertexUV = 1 # Make sticky coords.
# Reset matrix
contextMatrix_rot= None
#contextMatrix_tx= None
contextMatrix_rot = None
#contextMatrix_tx = None
CreateBlenderObject= True
tempName= read_string(file)
contextObName= tempName
CreateBlenderObject = True
tempName = read_string(file)
contextObName = 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= bpy.data.materials.new('Material')
elif (new_chunk.ID == MATERIAL):
# print("read material")
#print 'elif (new_chunk.ID == MATERIAL):'
contextMaterial = bpy.data.add_material('Material')
# contextMaterial = bpy.data.materials.new('Material')
elif (new_chunk.ID==MAT_NAME):
#print 'elif (new_chunk.ID==MAT_NAME):'
material_name= read_string(file)
elif (new_chunk.ID == MAT_NAME):
#print 'elif (new_chunk.ID == MAT_NAME):'
material_name = read_string(file)
# print("material name", material_name)
#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.name= material_name.rstrip() # remove trailing whitespace
contextMaterial.name = material_name.rstrip() # remove trailing whitespace
MATDICT[material_name]= (contextMaterial.name, contextMaterial)
elif (new_chunk.ID==MAT_AMBIENT):
#print 'elif (new_chunk.ID==MAT_AMBIENT):'
elif (new_chunk.ID == MAT_AMBIENT):
#print 'elif (new_chunk.ID == MAT_AMBIENT):'
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_FLOAT_COLOR):
temp_data=file.read(calcsize('3f'))
temp_chunk.bytes_read+=12
contextMaterial.mirCol=[float(col) for col in unpack('<3f', temp_data)]
elif (temp_chunk.ID==MAT_24BIT_COLOR):
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
if (temp_chunk.ID == MAT_FLOAT_COLOR):
contextMaterial.mirror_color = read_float_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3f'))
# temp_chunk.bytes_read += 12
# contextMaterial.mirCol = [float(col) for col in struct.unpack('<3f', temp_data)]
elif (temp_chunk.ID == MAT_24BIT_COLOR):
contextMaterial.mirror_color = read_byte_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3B'))
# temp_chunk.bytes_read += 3
# contextMaterial.mirCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
else:
skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
new_chunk.bytes_read += temp_chunk.bytes_read
elif (new_chunk.ID==MAT_DIFFUSE):
#print 'elif (new_chunk.ID==MAT_DIFFUSE):'
elif (new_chunk.ID == MAT_DIFFUSE):
#print 'elif (new_chunk.ID == MAT_DIFFUSE):'
read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_FLOAT_COLOR):
temp_data=file.read(calcsize('3f'))
temp_chunk.bytes_read+=12
contextMaterial.rgbCol=[float(col) for col in unpack('<3f', temp_data)]
elif (temp_chunk.ID==MAT_24BIT_COLOR):
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
if (temp_chunk.ID == MAT_FLOAT_COLOR):
contextMaterial.diffuse_color = read_float_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3f'))
# temp_chunk.bytes_read += 12
# contextMaterial.rgbCol = [float(col) for col in struct.unpack('<3f', temp_data)]
elif (temp_chunk.ID == MAT_24BIT_COLOR):
contextMaterial.diffuse_color = read_byte_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3B'))
# temp_chunk.bytes_read += 3
# contextMaterial.rgbCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
else:
skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
elif (new_chunk.ID==MAT_SPECULAR):
#print 'elif (new_chunk.ID==MAT_SPECULAR):'
# print("read material diffuse color", contextMaterial.diffuse_color)
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)
if (temp_chunk.ID==MAT_FLOAT_COLOR):
temp_data=file.read(calcsize('3f'))
temp_chunk.bytes_read+=12
contextMaterial.mirCol=[float(col) for col in unpack('<3f', temp_data)]
elif (temp_chunk.ID==MAT_24BIT_COLOR):
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
if (temp_chunk.ID == MAT_FLOAT_COLOR):
contextMaterial.specular_color = read_float_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3f'))
# temp_chunk.bytes_read += 12
# contextMaterial.mirCol = [float(col) for col in struct.unpack('<3f', temp_data)]
elif (temp_chunk.ID == MAT_24BIT_COLOR):
contextMaterial.specular_color = read_byte_color(temp_chunk)
# temp_data = file.read(struct.calcsize('3B'))
# temp_chunk.bytes_read += 3
# contextMaterial.mirCol = [float(col)/255 for col in struct.unpack('<3B', temp_data)] # data [0,1,2] == rgb
else:
skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
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= bpy.data.textures.new('Diffuse')
new_texture.setType('Image')
img = None
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)
elif (new_chunk.ID == MAT_TEXTURE_MAP):
read_texture(new_chunk, temp_chunk, "Diffuse", "COLOR")
# #print 'elif (new_chunk.ID==MAT_TEXTURE_MAP):'
# new_texture= bpy.data.textures.new('Diffuse')
# new_texture.setType('Image')
# img = None
# 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=read_string(file)
#img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
# if (temp_chunk.ID==MAT_MAP_FILENAME):
# texture_name=read_string(file)
# #img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
# img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
# new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
skip_to_end(file, temp_chunk)
# else:
# skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
# new_chunk.bytes_read+= temp_chunk.bytes_read
#add the map to the material in the right channel
if img:
add_texture_to_material(img, new_texture, contextMaterial, 'DIFFUSE')
# #add the map to the material in the right channel
# if img:
# add_texture_to_material(img, new_texture, contextMaterial, 'DIFFUSE')
elif (new_chunk.ID==MAT_SPECULAR_MAP):
#print 'elif (new_chunk.ID==MAT_SPECULAR_MAP):'
new_texture= bpy.data.textures.new('Specular')
new_texture.setType('Image')
img = None
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
elif (new_chunk.ID == MAT_SPECULAR_MAP):
read_texture(new_chunk, temp_chunk, "Specular", "SPECULARITY")
# #print 'elif (new_chunk.ID == MAT_SPECULAR_MAP):'
# new_texture = bpy.data.textures.new('Specular')
# new_texture.setType('Image')
# img = None
# while (new_chunk.bytes_read < new_chunk.length):
# read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name= read_string(file)
#img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
new_chunk.bytes_read+= (len(texture_name)+1) #plus one for the null character that gets removed
else:
skip_to_end(file, temp_chunk)
# if (temp_chunk.ID == MAT_MAP_FILENAME):
# texture_name = read_string(file)
# #img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
# img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER=False, RECURSIVE=IMAGE_SEARCH)
# new_chunk.bytes_read+= (len(texture_name)+1) #plus one for the null character that gets removed
# else:
# skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
# new_chunk.bytes_read += temp_chunk.bytes_read
#add the map to the material in the right channel
if img:
add_texture_to_material(img, new_texture, contextMaterial, 'SPECULAR')
# #add the map to the material in the right channel
# if img:
# add_texture_to_material(img, new_texture, contextMaterial, 'SPECULAR')
elif (new_chunk.ID==MAT_OPACITY_MAP):
#print 'new_texture=Blender.Texture.New('Opacity')'
new_texture= bpy.data.textures.new('Opacity')
new_texture.setType('Image')
img = None
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
elif (new_chunk.ID == MAT_OPACITY_MAP):
read_texture(new_chunk, temp_chunk, "Opacity", "ALPHA")
# #print 'new_texture = Blender.Texture.New('Opacity')'
# new_texture = bpy.data.textures.new('Opacity')
# new_texture.setType('Image')
# img = None
# while (new_chunk.bytes_read < new_chunk.length):
# read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name= read_string(file)
#img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
skip_to_end(file, temp_chunk)
# if (temp_chunk.ID == MAT_MAP_FILENAME):
# texture_name = read_string(file)
# #img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
# img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER=False, RECURSIVE=IMAGE_SEARCH)
# new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
# else:
# skip_to_end(file, temp_chunk)
new_chunk.bytes_read+= temp_chunk.bytes_read
#add the map to the material in the right channel
if img:
add_texture_to_material(img, new_texture, contextMaterial, 'OPACITY')
# new_chunk.bytes_read += temp_chunk.bytes_read
# #add the map to the material in the right channel
# if img:
# add_texture_to_material(img, new_texture, contextMaterial, 'OPACITY')
elif (new_chunk.ID==MAT_BUMP_MAP):
#print 'elif (new_chunk.ID==MAT_BUMP_MAP):'
new_texture= bpy.data.textures.new('Bump')
new_texture.setType('Image')
img = None
while (new_chunk.bytes_read<new_chunk.length):
read_chunk(file, temp_chunk)
elif (new_chunk.ID == MAT_BUMP_MAP):
read_texture(new_chunk, temp_chunk, "Bump", "NORMAL")
# #print 'elif (new_chunk.ID == MAT_BUMP_MAP):'
# new_texture = bpy.data.textures.new('Bump')
# new_texture.setType('Image')
# img = None
# while (new_chunk.bytes_read < new_chunk.length):
# read_chunk(file, temp_chunk)
if (temp_chunk.ID==MAT_MAP_FILENAME):
texture_name= read_string(file)
#img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
else:
skip_to_end(file, temp_chunk)
# if (temp_chunk.ID == MAT_MAP_FILENAME):
# texture_name = read_string(file)
# #img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
# img = BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER=False, RECURSIVE=IMAGE_SEARCH)
# new_chunk.bytes_read += (len(texture_name)+1) #plus one for the null character that gets removed
# else:
# skip_to_end(file, temp_chunk)
new_chunk.bytes_read+=temp_chunk.bytes_read
# new_chunk.bytes_read += temp_chunk.bytes_read
#add the map to the material in the right channel
if img:
add_texture_to_material(img, new_texture, contextMaterial, 'BUMP')
# #add the map to the material in the right channel
# if img:
# add_texture_to_material(img, new_texture, contextMaterial, 'BUMP')
elif (new_chunk.ID==MAT_TRANSPARENCY):
#print 'elif (new_chunk.ID==MAT_TRANSPARENCY):'
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)
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
temp_chunk.bytes_read+=2
contextMaterial.alpha= 1-(float(unpack('<H', temp_data)[0])/100)
new_chunk.bytes_read+=temp_chunk.bytes_read
temp_chunk.bytes_read += 2
contextMaterial.alpha = 1-(float(struct.unpack('<H', temp_data)[0])/100)
new_chunk.bytes_read += temp_chunk.bytes_read
elif (new_chunk.ID==OBJECT_LAMP): # Basic lamp support.
elif (new_chunk.ID == OBJECT_LAMP): # Basic lamp support.
temp_data=file.read(STRUCT_SIZE_3FLOAT)
temp_data = file.read(STRUCT_SIZE_3FLOAT)
x,y,z=unpack('<3f', temp_data)
new_chunk.bytes_read+=STRUCT_SIZE_3FLOAT
x,y,z = struct.unpack('<3f', temp_data)
new_chunk.bytes_read += STRUCT_SIZE_3FLOAT
ob = bpy.data.add_object("LAMP", "Lamp")
ob.data = bpy.data.add_lamp("Lamp")
SCN.add_object(ob)
contextLamp[1]= bpy.data.lamps.new()
contextLamp[0]= SCN_OBJECTS.new(contextLamp[1])
contextLamp[1]= ob.data
# contextLamp[1]= bpy.data.lamps.new()
contextLamp[0]= ob
# contextLamp[0]= SCN_OBJECTS.new(contextLamp[1])
importedObjects.append(contextLamp[0])
#print 'number of faces: ', num_faces
#print x,y,z
contextLamp[0].setLocation(x,y,z)
contextLamp[0].location = (x, y, z)
# contextLamp[0].setLocation(x,y,z)
# Reset matrix
contextMatrix_rot= None
#contextMatrix_tx= None
contextMatrix_rot = None
#contextMatrix_tx = None
#print contextLamp.name,
elif (new_chunk.ID==OBJECT_MESH):
elif (new_chunk.ID == OBJECT_MESH):
# print 'Found an OBJECT_MESH chunk'
pass
elif (new_chunk.ID==OBJECT_VERTICES):
elif (new_chunk.ID == OBJECT_VERTICES):
'''
Worldspace vertex locations
'''
# print 'elif (new_chunk.ID==OBJECT_VERTICES):'
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_verts=unpack('<H', temp_data)[0]
new_chunk.bytes_read+=2
# print 'elif (new_chunk.ID == OBJECT_VERTICES):'
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_verts = struct.unpack('<H', temp_data)[0]
new_chunk.bytes_read += 2
# print 'number of verts: ', num_verts
def getvert():
temp_data= unpack('<3f', file.read(STRUCT_SIZE_3FLOAT))
temp_data = struct.unpack('<3f', file.read(STRUCT_SIZE_3FLOAT))
new_chunk.bytes_read += STRUCT_SIZE_3FLOAT #12: 3 floats x 4 bytes each
return temp_data
#contextMesh.verts.extend( [Vector(),] ) # DUMMYVERT! - remove when blenders internals are fixed.
contextMesh_vertls= [getvert() for i in range(num_verts)]
contextMesh_vertls = [getvert() for i in range(num_verts)]
#print 'object verts: bytes read: ', new_chunk.bytes_read
elif (new_chunk.ID==OBJECT_FACES):
# print 'elif (new_chunk.ID==OBJECT_FACES):'
temp_data= file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_faces= unpack('<H', temp_data)[0]
new_chunk.bytes_read+= 2
elif (new_chunk.ID == OBJECT_FACES):
# print 'elif (new_chunk.ID == OBJECT_FACES):'
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_faces = struct.unpack('<H', temp_data)[0]
new_chunk.bytes_read += 2
#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= unpack('<4H', temp_data)
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)
return v1, v2, v3
contextMesh_facels= [ getface() for i in range(num_faces) ]
contextMesh_facels = [ getface() for i in range(num_faces) ]
elif (new_chunk.ID==OBJECT_MATERIAL):
# print 'elif (new_chunk.ID==OBJECT_MATERIAL):'
material_name= read_string(file)
elif (new_chunk.ID == OBJECT_MATERIAL):
# print 'elif (new_chunk.ID == OBJECT_MATERIAL):'
material_name = read_string(file)
new_chunk.bytes_read += len(material_name)+1 # remove 1 null character.
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_faces_using_mat = unpack('<H', temp_data)[0]
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_faces_using_mat = struct.unpack('<H', temp_data)[0]
new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
def getmat():
temp_data= file.read(STRUCT_SIZE_UNSIGNED_SHORT)
new_chunk.bytes_read+= STRUCT_SIZE_UNSIGNED_SHORT
return unpack('<H', temp_data)[0]
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
new_chunk.bytes_read += STRUCT_SIZE_UNSIGNED_SHORT
return struct.unpack('<H', temp_data)[0]
contextMeshMaterials[material_name]= [ getmat() for i in range(num_faces_using_mat) ]
#look up the material in all the materials
elif (new_chunk.ID==OBJECT_UV):
temp_data=file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_uv=unpack('<H', temp_data)[0]
new_chunk.bytes_read+= 2
elif (new_chunk.ID == OBJECT_UV):
temp_data = file.read(STRUCT_SIZE_UNSIGNED_SHORT)
num_uv = struct.unpack('<H', temp_data)[0]
new_chunk.bytes_read += 2
def getuv():
temp_data=file.read(STRUCT_SIZE_2FLOAT)
temp_data = file.read(STRUCT_SIZE_2FLOAT)
new_chunk.bytes_read += STRUCT_SIZE_2FLOAT #2 float x 4 bytes each
return Vector( unpack('<2f', temp_data) )
return Mathutils.Vector( struct.unpack('<2f', temp_data) )
# return Vector( struct.unpack('<2f', temp_data) )
contextMeshUV= [ getuv() for i in range(num_uv) ]
contextMeshUV = [ getuv() for i in range(num_uv) ]
elif (new_chunk.ID== OBJECT_TRANS_MATRIX):
elif (new_chunk.ID == OBJECT_TRANS_MATRIX):
# How do we know the matrix size? 54 == 4x4 48 == 4x3
temp_data=file.read(STRUCT_SIZE_4x3MAT)
data= list( unpack('<ffffffffffff', temp_data) )
temp_data = file.read(STRUCT_SIZE_4x3MAT)
data = list( struct.unpack('<ffffffffffff', temp_data) )
new_chunk.bytes_read += STRUCT_SIZE_4x3MAT
contextMatrix_rot= Blender.Mathutils.Matrix(\
contextMatrix_rot = Mathutils.Matrix(\
# contextMatrix_rot = Blender.Mathutils.Matrix(\
data[:3] + [0],\
data[3:6] + [0],\
data[6:9] + [0],\
@@ -752,7 +859,7 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
'''
contextMatrix_rot= Blender.Mathutils.Matrix(\
contextMatrix_rot = Blender.Mathutils.Matrix(\
data[:3] + [0],\
data[3:6] + [0],\
data[6:9] + [0],\
@@ -760,7 +867,7 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
'''
'''
contextMatrix_rot= Blender.Mathutils.Matrix(\
contextMatrix_rot = Blender.Mathutils.Matrix(\
data[:3] ,\
data[3:6],\
data[6:9])
@@ -772,7 +879,7 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
for j in xrange(4):
for i in xrange(3):
contextMatrix_rot[j][i] = data[m]
m+=1
m += 1
contextMatrix_rot[0][3]=0;
contextMatrix_rot[1][3]=0;
@@ -793,49 +900,54 @@ def process_next_chunk(file, previous_chunk, importedObjects, IMAGE_SEARCH):
#contextMatrix = contextMatrix * tx
#contextMatrix = contextMatrix *tx
elif (new_chunk.ID==MAT_MAP_FILENAME):
texture_name=read_string(file)
elif (new_chunk.ID == MAT_MAP_FILENAME):
texture_name = read_string(file)
try:
TEXTURE_DICT[contextMaterial.name]
except:
#img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img= TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER= False, RECURSIVE= IMAGE_SEARCH)
#img = TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME)
img = TEXTURE_DICT[contextMaterial.name] = load_image(texture_name, dirname)
# img = TEXTURE_DICT[contextMaterial.name]= BPyImage.comprehensiveImageLoad(texture_name, FILENAME, PLACE_HOLDER=False, RECURSIVE=IMAGE_SEARCH)
new_chunk.bytes_read+= len(texture_name)+1 #plus one for the null character that gets removed
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'
buffer_size=new_chunk.length-new_chunk.bytes_read
buffer_size = new_chunk.length - new_chunk.bytes_read
binary_format='%ic' % buffer_size
temp_data=file.read(calcsize(binary_format))
new_chunk.bytes_read+=buffer_size
temp_data = file.read(struct.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
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_facels != None:
putContextMesh(contextMesh_vertls, contextMesh_facels, contextMeshMaterials)
def load_3ds(filename, PREF_UI= True):
global FILENAME, SCN_OBJECTS
def load_3ds(filename, context, IMPORT_CONSTRAIN_BOUNDS=10.0, IMAGE_SEARCH=True, APPLY_MATRIX=False):
global FILENAME, SCN
# global FILENAME, SCN_OBJECTS
# XXX
# if BPyMessages.Error_NoFile(filename):
# return
if BPyMessages.Error_NoFile(filename):
return
print('\n\nImporting 3DS: "%s"' % (bpy.sys.expandpath(filename)))
# print('\n\nImporting 3DS: "%s"' % (Blender.sys.expandpath(filename)))
time1 = time.clock()
# time1 = Blender.sys.time()
print('\n\nImporting 3DS: "%s"' % (Blender.sys.expandpath(filename)))
FILENAME = filename
current_chunk = chunk()
time1= Blender.sys.time()
FILENAME=filename
current_chunk=chunk()
file=open(filename,'rb')
file = open(filename,'rb')
#here we go!
# print 'reading the first chunk'
@@ -846,29 +958,29 @@ def load_3ds(filename, PREF_UI= True):
return
# IMPORT_AS_INSTANCE= Blender.Draw.Create(0)
IMPORT_CONSTRAIN_BOUNDS= Blender.Draw.Create(10.0)
IMAGE_SEARCH= Blender.Draw.Create(1)
APPLY_MATRIX= Blender.Draw.Create(0)
# IMPORT_AS_INSTANCE = Blender.Draw.Create(0)
# IMPORT_CONSTRAIN_BOUNDS = Blender.Draw.Create(10.0)
# IMAGE_SEARCH = Blender.Draw.Create(1)
# APPLY_MATRIX = Blender.Draw.Create(0)
# Get USER Options
pup_block= [\
('Size Constraint:', IMPORT_CONSTRAIN_BOUNDS, 0.0, 1000.0, 'Scale the model by 10 until it reacehs the size constraint. Zero Disables.'),\
('Image Search', IMAGE_SEARCH, 'Search subdirs for any assosiated images (Warning, may be slow)'),\
('Transform Fix', APPLY_MATRIX, 'Workaround for object transformations importing incorrectly'),\
#('Group Instance', IMPORT_AS_INSTANCE, 'Import objects into a new scene and group, creating an instance in the current scene.'),\
]
# pup_block = [\
# ('Size Constraint:', IMPORT_CONSTRAIN_BOUNDS, 0.0, 1000.0, 'Scale the model by 10 until it reacehs the size constraint. Zero Disables.'),\
# ('Image Search', IMAGE_SEARCH, 'Search subdirs for any assosiated images (Warning, may be slow)'),\
# ('Transform Fix', APPLY_MATRIX, 'Workaround for object transformations importing incorrectly'),\
# #('Group Instance', IMPORT_AS_INSTANCE, 'Import objects into a new scene and group, creating an instance in the current scene.'),\
# ]
if PREF_UI:
if not Blender.Draw.PupBlock('Import 3DS...', pup_block):
return
# if PREF_UI:
# if not Blender.Draw.PupBlock('Import 3DS...', pup_block):
# return
Blender.Window.WaitCursor(1)
# Blender.Window.WaitCursor(1)
IMPORT_CONSTRAIN_BOUNDS= IMPORT_CONSTRAIN_BOUNDS.val
# IMPORT_AS_INSTANCE= IMPORT_AS_INSTANCE.val
IMAGE_SEARCH = IMAGE_SEARCH.val
APPLY_MATRIX = APPLY_MATRIX.val
# IMPORT_CONSTRAIN_BOUNDS = IMPORT_CONSTRAIN_BOUNDS.val
# # IMPORT_AS_INSTANCE = IMPORT_AS_INSTANCE.val
# IMAGE_SEARCH = IMAGE_SEARCH.val
# APPLY_MATRIX = APPLY_MATRIX.val
if IMPORT_CONSTRAIN_BOUNDS:
BOUNDS_3DS[:]= [1<<30, 1<<30, 1<<30, -1<<30, -1<<30, -1<<30]
@@ -877,93 +989,100 @@ def load_3ds(filename, PREF_UI= True):
##IMAGE_SEARCH
scn= bpy.data.scenes.active
SCN_OBJECTS = scn.objects
SCN_OBJECTS.selected = [] # de select all
scn = context.scene
# scn = bpy.data.scenes.active
SCN = scn
# SCN_OBJECTS = scn.objects
# SCN_OBJECTS.selected = [] # de select all
importedObjects= [] # Fill this list with objects
importedObjects = [] # Fill this list with objects
process_next_chunk(file, current_chunk, importedObjects, IMAGE_SEARCH)
# Link the objects into this scene.
# Layers= scn.Layers
# Layers = scn.Layers
# REMOVE DUMMYVERT, - remove this in the next release when blenders internal are fixed.
for ob in importedObjects:
if ob.type=='Mesh':
me= ob.getData(mesh=1)
me.verts.delete([me.verts[0],])
if not APPLY_MATRIX:
me.transform(ob.matrixWorld.copy().invert())
# for ob in importedObjects:
# if ob.type == 'MESH':
# # if ob.type=='Mesh':
# me = ob.getData(mesh=1)
# me.verts.delete([me.verts[0],])
# if not APPLY_MATRIX:
# me.transform(ob.matrixWorld.copy().invert())
# Done DUMMYVERT
"""
if IMPORT_AS_INSTANCE:
name= filename.split('\\')[-1].split('/')[-1]
name = filename.split('\\')[-1].split('/')[-1]
# Create a group for this import.
group_scn= Scene.New(name)
group_scn = Scene.New(name)
for ob in importedObjects:
group_scn.link(ob) # dont worry about the layers
grp= Blender.Group.New(name)
grp.objects= importedObjects
grp = Blender.Group.New(name)
grp.objects = importedObjects
grp_ob= Object.New('Empty', name)
grp_ob.enableDupGroup= True
grp_ob.DupGroup= grp
grp_ob = Object.New('Empty', name)
grp_ob.enableDupGroup = True
grp_ob.DupGroup = grp
scn.link(grp_ob)
grp_ob.Layers= Layers
grp_ob.sel= 1
grp_ob.Layers = Layers
grp_ob.sel = 1
else:
# Select all imported objects.
for ob in importedObjects:
scn.link(ob)
ob.Layers= Layers
ob.sel= 1
ob.Layers = Layers
ob.sel = 1
"""
if IMPORT_CONSTRAIN_BOUNDS!=0.0:
if 0:
# if IMPORT_CONSTRAIN_BOUNDS!=0.0:
# Set bounds from objecyt bounding box
for ob in importedObjects:
if ob.type=='Mesh':
if ob.type == 'MESH':
# if ob.type=='Mesh':
ob.makeDisplayList() # Why dosnt this update the bounds?
for v in ob.getBoundBox():
for i in (0,1,2):
if v[i] < BOUNDS_3DS[i]:
BOUNDS_3DS[i]= v[i] # min
if v[i] > BOUNDS_3DS[i+3]:
BOUNDS_3DS[i+3]= v[i] # min
if v[i] > BOUNDS_3DS[i + 3]:
BOUNDS_3DS[i + 3]= v[i] # min
# Get the max axis x/y/z
max_axis= max(BOUNDS_3DS[3]-BOUNDS_3DS[0], BOUNDS_3DS[4]-BOUNDS_3DS[1], BOUNDS_3DS[5]-BOUNDS_3DS[2])
max_axis = max(BOUNDS_3DS[3]-BOUNDS_3DS[0], BOUNDS_3DS[4]-BOUNDS_3DS[1], BOUNDS_3DS[5]-BOUNDS_3DS[2])
# print max_axis
if max_axis < 1<<30: # Should never be false but just make sure.
if max_axis < 1 << 30: # Should never be false but just make sure.
# Get a new scale factor if set as an option
SCALE=1.0
while (max_axis*SCALE) > IMPORT_CONSTRAIN_BOUNDS:
SCALE = 1.0
while (max_axis * SCALE) > IMPORT_CONSTRAIN_BOUNDS:
SCALE/=10
# SCALE Matrix
SCALE_MAT= Blender.Mathutils.Matrix([SCALE,0,0,0],[0,SCALE,0,0],[0,0,SCALE,0],[0,0,0,1])
SCALE_MAT = Mathutils.Matrix([SCALE,0,0,0],[0,SCALE,0,0],[0,0,SCALE,0],[0,0,0,1])
# SCALE_MAT = Blender.Mathutils.Matrix([SCALE,0,0,0],[0,SCALE,0,0],[0,0,SCALE,0],[0,0,0,1])
for ob in importedObjects:
ob.setMatrix(ob.matrixWorld*SCALE_MAT)
ob.setMatrix(ob.matrixWorld * SCALE_MAT)
# Done constraining to bounds.
# Select all new objects.
print('finished importing: "%s" in %.4f sec.' % (filename, (Blender.sys.time()-time1)))
print('finished importing: "%s" in %.4f sec.' % (filename, (time.clock()-time1)))
# print('finished importing: "%s" in %.4f sec.' % (filename, (Blender.sys.time()-time1)))
file.close()
Blender.Window.WaitCursor(0)
# Blender.Window.WaitCursor(0)
DEBUG= False
DEBUG = False
# if __name__=='__main__' and not DEBUG:
# if calcsize==None:
# if calcsize == None:
# Blender.Draw.PupMenu('Error%t|a full python installation not found')
# else:
# Blender.Window.FileSelector(load_3ds, 'Import 3DS', '*.3ds')
@@ -976,14 +1095,14 @@ DEBUG= False
else:
import os
# DEBUG ONLY
TIME= Blender.sys.time()
TIME = Blender.sys.time()
import os
print 'Searching for files'
os.system('find /metavr/ -iname "*.3ds" > /tmp/temp3ds_list')
# os.system('find /storage/ -iname "*.3ds" > /tmp/temp3ds_list')
print '...Done'
file= open('/tmp/temp3ds_list', 'r')
lines= file.readlines()
file = open('/tmp/temp3ds_list', 'r')
lines = file.readlines()
file.close()
# sort by filesize for faster testing
lines_size = [(os.path.getsize(f[:-1]), f[:-1]) for f in lines]
@@ -1001,7 +1120,7 @@ else:
#_3ds= _3ds[:-1]
print 'Importing', _3ds, '\nNUMBER', i, 'of', len(lines)
_3ds_file= _3ds.split('/')[-1].split('\\')[-1]
newScn= Blender.Scene.New(_3ds_file)
newScn = Blender.Scene.New(_3ds_file)
newScn.makeCurrent()
load_3ds(_3ds, False)
@@ -1020,11 +1139,14 @@ class IMPORT_OT_3ds(bpy.types.Operator):
# to the class instance from the operator settings before calling.
__props__ = [
bpy.props.StringProperty(attr="filename", name="File Name", description="File name used for importing the 3DS file", maxlen= 1024, default= ""),
bpy.props.StringProperty(attr="filename", name="File Name", description="File name used for importing the 3DS file", maxlen=1024, default= ""),
# bpy.props.FloatProperty(attr="size_constraint", name="Size Constraint", description="Scale the model by 10 until it reacehs the size constraint. Zero Disables.", min=0.0, max=1000.0, soft_min=0.0, soft_max=1000.0, default=10.0),
# bpy.props.BoolProperty(attr="search_images", name="Image Search", description="Search subdirectories for any assosiated images (Warning, may be slow)", default=True),
# bpy.props.BoolProperty(attr="apply_matrix", name="Transform Fix", description="Workaround for object transformations importing incorrectly", default=False),
]
def execute(self, context):
raise Exception("Not doing anything yet.")
load_3ds(self.filename, context, 0.0, False, False)
return ('FINISHED',)
def invoke(self, context, event):
@@ -1037,3 +1159,7 @@ class IMPORT_OT_3ds(bpy.types.Operator):
return context.active_object != None
bpy.ops.add(IMPORT_OT_3ds)
# NOTES:
# why add 1 extra vertex? and remove it when done?
# disabled scaling to size, this requires exposing bb (easy) and understanding how it works (needs some time)

View File

@@ -294,18 +294,21 @@ def line_value(line_split):
return ' '.join( line_split[1:] )
# limited replacement for BPyImage.comprehensiveImageLoad
def load_image(imagepath, direc):
def load_image(imagepath, dirname):
if os.path.exists(imagepath):
return bpy.data.add_image(imagepath)
im_base = os.path.basename(imagepath)
variants = [os.path.join(dirname, imagepath), os.path.join(dirname, os.path.basename(imagepath))]
tmp = os.path.join(direc, im_base)
if os.path.exists(tmp):
return bpy.data.add_image(tmp)
for path in variants:
if os.path.exists(path):
return bpy.data.add_image(path)
else:
print(path, "doesn't exist")
# TODO comprehensiveImageLoad also searched in bpy.config.textureDir
return None
def obj_image_load(imagepath, DIR, IMAGE_SEARCH):
@@ -387,7 +390,7 @@ def create_materials(filepath, material_libs, unique_materials, unique_material_
# blender_material.setTexture(1, texture, Texture.TexCo.UV, Texture.MapTo.CMIR) # TODO- Add AMB to BPY API
elif type == 'Ks':
blender_material.add_texture(texture, "UV", "SPECULAR")
blender_material.add_texture(texture, "UV", "SPECULARITY")
# blender_material.setTexture(2, texture, Texture.TexCo.UV, Texture.MapTo.SPEC)
elif type == 'Bump':

View File

@@ -44,6 +44,9 @@
#include "BKE_object.h"
#include "BKE_material.h"
#include "BKE_image.h"
#include "BKE_texture.h"
#include "DNA_lamp_types.h"
static Mesh *rna_Main_add_mesh(Main *main, char *name)
{
@@ -62,6 +65,23 @@ static void rna_Main_remove_mesh(Main *main, ReportList *reports, Mesh *me)
/* XXX python now has invalid pointer? */
}
static Lamp *rna_Main_add_lamp(Main *main, char *name)
{
Lamp *la= add_lamp(name);
la->id.us--;
return la;
}
/*
static void rna_Main_remove_lamp(Main *main, ReportList *reports, Lamp *la)
{
if(la->id.us == 0)
free_libblock(&main->lamp, la);
else
BKE_report(reports, RPT_ERROR, "Lamp must have zero users to be removed.");
}
*/
static Object* rna_Main_add_object(Main *main, int type, char *name)
{
Object *ob= add_only_object(type, name);
@@ -158,6 +178,13 @@ void RNA_api_main(StructRNA *srna)
parm= RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh to remove.");
RNA_def_property_flag(parm, PROP_REQUIRED);
func= RNA_def_function(srna, "add_lamp", "rna_Main_add_lamp");
RNA_def_function_ui_description(func, "Add a new lamp.");
parm= RNA_def_string(func, "name", "Lamp", 0, "", "New name for the datablock.");
RNA_def_property_flag(parm, PROP_REQUIRED);
parm= RNA_def_pointer(func, "mesh", "Lamp", "", "New lamp.");
RNA_def_function_return(func, parm);
func= RNA_def_function(srna, "add_material", "rna_Main_add_material");
RNA_def_function_ui_description(func, "Add a new material.");
parm= RNA_def_string(func, "name", "Material", 0, "", "New name for the datablock."); /* optional */

View File

@@ -101,7 +101,7 @@ void RNA_api_material(StructRNA *srna)
static EnumPropertyItem prop_texture_mapto_items[] = {
{MAP_COL, "COLOR", 0, "Color", "Causes the texture to affect basic color of the material"},
{MAP_NORM, "NORMAL", 0, "Normal", "Causes the texture to affect the rendered normal"},
{MAP_COLSPEC, "SPEC_COLOR", 0, "Specularity Color", "Causes the texture to affect the specularity color"},
{MAP_COLSPEC, "SPECULAR_COLOR", 0, "Specularity Color", "Causes the texture to affect the specularity color"},
{MAP_COLMIR, "MIRROR", 0, "Mirror", "Causes the texture to affect the mirror color"},
{MAP_REF, "REFLECTION", 0, "Reflection", "Causes the texture to affect the value of the materials reflectivity"},
{MAP_SPEC, "SPECULARITY", 0, "Specularity", "Causes the texture to affect the value of specularity"},

View File

@@ -248,7 +248,7 @@ static void rna_Mesh_add_geometry(Mesh *mesh, int verts, int edges, int faces)
rna_Mesh_add_faces(mesh, faces);
}
static void rna_Mesh_add_uv_layer(Mesh *me)
static void rna_Mesh_add_uv_texture(Mesh *me)
{
me->mtface= CustomData_add_layer(&me->fdata, CD_MTFACE, CD_DEFAULT, NULL, me->totface);
}
@@ -306,8 +306,8 @@ void RNA_api_mesh(StructRNA *srna)
parm= RNA_def_pointer(func, "mesh", "Mesh", "", "Mesh, remove it if it is only used for export.");
RNA_def_function_return(func, parm);
func= RNA_def_function(srna, "add_uv_layer", "rna_Mesh_add_uv_layer");
RNA_def_function_ui_description(func, "Add new UV layer to Mesh.");
func= RNA_def_function(srna, "add_uv_texture", "rna_Mesh_add_uv_texture");
RNA_def_function_ui_description(func, "Add a UV texture layer to Mesh.");
func= RNA_def_function(srna, "calc_normals", "rna_Mesh_calc_normals");
RNA_def_function_ui_description(func, "Calculate vertex normals.");

View File

@@ -74,6 +74,12 @@ void rna_Object_update(bContext *C, PointerRNA *ptr)
DAG_object_flush_update(CTX_data_scene(C), ptr->id.data, OB_RECALC_OB);
}
void rna_Object_matrix_update(bContext *C, PointerRNA *ptr)
{
ED_object_apply_obmat(ptr->id.data);
rna_Object_update(C, ptr);
}
void rna_Object_update_data(bContext *C, PointerRNA *ptr)
{
DAG_object_flush_update(CTX_data_scene(C), ptr->id.data, OB_RECALC_DATA);
@@ -1101,6 +1107,7 @@ static void rna_def_object(BlenderRNA *brna)
RNA_def_property_float_sdna(prop, NULL, "obmat");
RNA_def_property_array(prop, 16);
RNA_def_property_ui_text(prop, "Matrix", "Transformation matrix.");
RNA_def_property_update(prop, NC_OBJECT|ND_TRANSFORM, "rna_Object_matrix_update");
/* collections */
prop= RNA_def_property(srna, "constraints", PROP_COLLECTION, PROP_NONE);