Blender Legacy Importer

Hi,

I’m running the requirements for this blender script (Beldner 2.49 and Python Version 6) but I get this error when I run the script from the blender script window.

Traceback (most recent call last):
File “import-castlevania-20”, line 486, in
ValueError: expected a string and a non-empty sequence.

The script I’m using is below. Thanks in advance for any help provided.

2010-12-25 Glogow Poland Mariusz Szkaradek

import bpy,struct,os
import Blender
from Blender import *
from struct import *
from Blender.Mathutils import *
from Blender import Armature as A
from Blender.Window import DrawProgressBar
import math

sk=1

def HalfToFloat(h):
s = int((h >> 15) & 0x00000001) # sign
e = int((h >> 10) & 0x0000001f) # exponent
f = int(h & 0x000003ff) # fraction

if e == 0:
   if f == 0:
      return int(s << 31)
   else:
      while not (f & 0x00000400):
         f <<= 1
         e -= 1
      e += 1
      f &= ~0x00000400
      #print s,e,f
elif e == 31:
   if f == 0:
      return int((s << 31) | 0x7f800000)
   else:
      return int((s << 31) | 0x7f800000 | (f << 13))

e = e + (127 -15)
f = f << 13
return int((s << 31) | (e << 23) | f)

def converthalf2float(h):
id = HalfToFloat(h)
str = struct.pack(‘I’,id)
return struct.unpack(‘f’, str)[0]

def word(long):
s=’’
for j in range(0,long):
lit = struct.unpack(‘c’,plik.read(1))[0]
if ord(lit)!=0:
s+=lit
if len(s)>100:
break
return s

bigendian = True
littleendian = False

#BINARY DATA - BIG ENDIAN
if bigendian == True:
def B(n):
array = []
for id in range(n):
array.append(struct.unpack(’>B’, plik.read(1))[0])
return array
def H(n):
array = []
for id in range(n):
array.append(struct.unpack(’>H’, plik.read(2))[0])
return array
def f(n):
array = []
for id in range(n):
array.append(struct.unpack(’>f’, plik.read(4))[0])
return array
def i(n):
array = []
for id in range(n):
array.append(struct.unpack(’>i’, plik.read(4))[0])
return array

if littleendian == True:
def b(n):
return struct.unpack(n*‘b’, plik.read(n))
def B(n):
return struct.unpack(n*‘B’, plik.read(n))
def h(n):
return struct.unpack(n*‘h’, plik.read(n2))
def H(n):
return struct.unpack(n
’H’, plik.read(n2))
def i(n):
return struct.unpack(n
’i’, plik.read(n4))
def f(n):
return struct.unpack(n
’f’, plik.read(n*4))

def read_faces():
faceslist=[]
data = i(3)
print ‘nie wiem-----------’,data[0]
print ‘nie wiem-----------’,data[1]
print ‘long name----------’,data[2],
print word(data[2])
data = i(2)
print ‘size of faces------’,data[0],‘bytes’
print ‘size of one iter—’,data[1],‘bytes’
print ‘reading faces…’,
for m in range(data[0]):
v = H(3)
faceslist.append(v)
print ‘done’
return faceslist

def read_vertexes():
vertexes = []
uvcoord =[]
groups = []
data = i(4)
print data,word(data[3]),
data = H(6)
print data
if data[3] == 44:
for m in range(data[5]):
vertexes.append(f(3))
data1 = H(8)
u = converthalf2float(data1[6])
v = converthalf2float(data1[7])
uvcoord.append([u,1-v])
B(8)
data1 = B(8)
groups.append([])
for n in range(4):
groups[m].append([data1[n],data1[n+4]/255.0])

if data[3] == 36:
    for m in range(data[5]):
        vertexes.append(f(3))
        data1 = H(8)
        u = converthalf2float(data1[6])
        v = converthalf2float(data1[7])
        uvcoord.append([u,1-v])
        data1 = B(8)                 
        groups.append([])
        for n in range(4):
            groups[m].append([data1[n],data1[n+4]/255.0])

if data[3]<32:
    for m in range(data[5]):
        vertexes.append(f(3))
        plik.read(data[3]-12)

if data[3]==32:
    for m in range(data[5]):
        vertexes.append(f(3))
        data1 = f(3)
        u = data1[1]  
        v = data1[2]  
        uvcoord.append([u,1-v])
        data1 = B(8)
        #print data1            
        groups.append([])
        for n in range(4):
            groups[m].append([data1[n],data1[n+4]/255.0])
return vertexes,uvcoord,groups

def drawmesh(name):
global obj,mesh
mesh = bpy.data.meshes.new(name)
mesh.verts.extend(vertexy)
mesh.faces.extend(faceslist,ignoreDups=True)
if len(uvcoord)!=0:
uv()
scene = bpy.data.scenes.active
obj = scene.objects.new(mesh,name)
mesh.recalcNormals()
mesh.update()
Redraw()

def uv():
for faceID in range(0,len(mesh.faces)):
face = mesh.faces[faceID]
index1 = faceslist[faceID][0]
index2 = faceslist[faceID][1]
index3 = faceslist[faceID][2]
uv1 = Vector(uvcoord[index1])
uv2 = Vector(uvcoord[index2])
uv3 = Vector(uvcoord[index3])
face.uv = [uv1, uv2, uv3]
face.smooth=True

def make_armature(filename):
namefile = filename.split(os.sep)[-1].split(’.’)[0]
t = sys.time()
print ‘checking and making armature…’,
global armobj,newarm
armobj=None
newarm=None
scn = Scene.GetCurrent()
scene = bpy.data.scenes.active
for object in scene.objects:
if object.getType()==‘Armature’:
if object.name == namefile+’-armobj’:
scn.unlink(object)
for object in bpy.data.objects:
if object.name == namefile+’-armobj’:
armobj = Blender.Object.Get(namefile+’-armobj’)
newarm = armobj.getData()
newarm.makeEditable()
for bone in newarm.bones.values():
del newarm.bones[bone.name]
newarm.update()
if armobj==None:
armobj = Blender.Object.New(‘Armature’,namefile+’-armobj’)
if newarm==None:
newarm = Armature.New(namefile+’-arm’)
armobj.link(newarm)
scn.link(armobj)
newarm.drawType = Armature.STICK
print ‘%.2f seconds’ % (sys.time()-t)

def make_vertex_group():
for v_id in range(len(groups)):
for gr_id in groups[v_id]:
gr = gr_id[0]
gr = grupy[gr]
gr = bonenames[gr+0]
w = gr_id[1]
if gr not in mesh.getVertGroupNames():
mesh.addVertGroup(gr)
mesh.update()
mesh.assignVertsToGroup(gr,[v_id],w,1)
mesh.update()

def read_materials():
dir_images = sys.dirname(filename)+os.sep+‘dds’+os.sep
dir_images = dir_images.lower()
data = i(3)
print data
for m in range(data[2]):
print ‘-------------’,m,
word(8)
i(1)
long = i(1)[0]
print word(long)
try:
mat = Material.Get(‘mat-’+str(m))
except:
mat = Material.New(‘mat-’+str(m))
B(44)
data = i(3)
print data
long = i(1)[0]
print word(long)
long = i(1)[0]
print word(long)
data = i(2)
print data
for n in range(data[1]):
i(1)
long = i(1)[0]
name_image = word(long).split(’/’)[-1][:-5]
name_image = name_image.lower()
print name_image

        try:
            tex = Texture.Get('tex-'+str(m))
        except:
            tex = Texture.New('tex-'+str(m))
        tex.setType('Image')
        try:
            img = Blender.Image.Load(dir_images+name_image+'d.dds')                      
            tex.image = img 
            mat.setTexture(0,tex,Texture.TexCo.UV,Texture.MapTo.COL) 
        except:
            pass


        try:
            tex_alpha = Texture.Get('tex_alpha-'+str(m))
        except:
            tex_alpha = Texture.New('tex_alpha-'+str(m))
        tex_alpha.setType('Image')
        try:
            img = Blender.Image.Load(dir_images+name_image+'d.alpha.dds')                      
            tex_alpha.image = img 
            mat.setTexture(1,tex_alpha,Texture.TexCo.UV,Texture.MapTo.COL) 
        except:
            pass


        try:
            tex_norm = Texture.Get('tex_norm-'+str(m))
        except:
            tex_norm = Texture.New('tex_norm-'+str(m))
        tex_norm.setType('Image')
        tex_norm.setImageFlags('NormalMap')  
        try:
            img = Blender.Image.Load(dir_images+name_image+'b.dds')                      
            tex_norm.image = img 
            mat.setTexture(2,tex_norm,Texture.TexCo.UV,Texture.MapTo.NOR) 
        except:
            pass


        try:
            tex_spec = Texture.Get('tex_spec-'+str(m))
        except:
            tex_spec = Texture.New('tex_spec-'+str(m))
        tex_spec.setType('Image')
        try:
            img = Blender.Image.Load(dir_images+name_image+'s.dds')                      
            tex_spec.image = img 
            mat.setTexture(3,tex_spec,Texture.TexCo.UV,Texture.MapTo.SPEC) 
        except:
            pass


        B(23)
    B(40)

def make_bones():
global namebone
newarm.makeEditable()
data = i(6)
print data
namebone = word(i(1)[0])[:25]
bonenames[data[1]] = namebone
print ‘bone------’,namebone
eb = A.Editbone()
newarm.bones[namebone] = eb
newarm.update()
newarm.makeEditable()
nameparent = word(i(1)[0])[:25]
print ‘parent----’,nameparent
if len(nameparent)>0:
parent = newarm.bones[nameparent]
newarm.bones[namebone].parent = parent
f(8)
bonematrix = Matrix(f(4),f(4),f(4),f(4))
f(23)
newarm.update()
newarm.makeEditable()
bone = newarm.bones[namebone]
bone.matrix = bonematrix
newarm.update()

def read_mesh_data(num):
print namebone+’=’+str(num)
meshes[namebone+’=’+str(num)]=[]
grupy = []
meshes[namebone+’=’+str(num)].append(read_faces())
word(4)
meshes[namebone+’=’+str(num)].append(read_vertexes())
data = i(3)
print ‘korekta grup’,data
for k in range(data[1]):
gr = H(1)[0]
grupy.append(gr)
meshes[namebone+’=’+str(num)].append(grupy)
data = i(2)
print ‘material nr =’,data[1]
id_mat = data[1]
print ‘matrix mesh’
objectmatrix = f(16)#not useful
meshes[namebone+’=’+str(num)].append(objectmatrix)
meshes[namebone+’=’+str(num)].append(id_mat)
B(4)

def readdata():
global bonenames,meshes
bonenames = []
meshes = {}
print word(8)
print B(12)
print word(4)
print i(3)
long = i(1)[0]
print word(long)
print i(1)[0]
print word(4)
data = i(3)
print data
for m in range(data[2]):
long = i(1)[0]
print word(long)
print f(19)
read_materials()#----------------MATERIALS
data = i(2)
print
print ‘==== MAKING NODES ====’
print
print ‘begin----------’,data[0]
print ‘num nodes------’,data[1]
for m in range(data[1]):
bonenames.append(’’)
for m in range(data[1]):
print ‘—node—’,m
make_bones()
long = i(1)[0]
for n in range(long):
print word(4)
data = i(3)
for k in range(data[2]):
long = i(1)[0]
print word(long)
long = i(1)[0]
print long
for n in range(long):
print i(1)
num = 0
while(True):
print ‘------------------’#,u
name = word(4)
print name
if name == ‘CAM’:
i(5)
f(14)
break
if name == ‘MD3D’:
read_mesh_data(num)
data = i(1)[0]
if data==0:
break
num+=1
print struct.unpack(’>i’,plik.read(4))[0]
print 'KONIEC AT OFFSET ',plik.tell()

def make_right_mesh_position():
name = mesh_name.split(’=’)[0]
obj_matrix = obj.matrixWorld
bones= newarm.bones.values()
for bone in bones:
if bone.name == name:
bone_mat= bone.matrix[‘ARMATURESPACE’]
bone_mat_world= bone_mat*obj_matrix
return bone_mat_world

def draw_all():
global mesh_name,faceslist,vertexy,uvcoord,groups,grupy
for mesh_name in meshes:
mesh_data = meshes[mesh_name]
faceslist = mesh_data[0]
vertexy = mesh_data[1][0]
uvcoord = mesh_data[1][1]
groups = mesh_data[1][2]
grupy = mesh_data[2]
mat_id = mesh_data[4]
drawmesh(mesh_name)
if len(grupy) == 0:
matrix = make_right_mesh_position()
obj.setMatrix(matrix)
Redraw()
armobj.makeParentBone([obj],mesh_name.split(’=’)[0],0,0)
else:
armobj.makeParentDeform([obj],0,0)
mesh.materials+=[Material.Get(‘mat-’+str(mat_id))]
print mesh_name,grupy
try:
make_vertex_group()
except:
pass
Redraw()

#=========== open file ======================
global plik,filename
extends=[‘sm3’]
dir = sys.dirname(Blender.Get(‘filename’))
g = os.listdir(dir)
bool={}
draw={}
block=[]
for plik in g:
extend = plik[plik.lower().find(’.’)+1:]
if extend.lower() in extends:
bool[plik] = False
draw[plik] = Draw.Create(bool[plik])
block.append((plik,draw[plik]))
Draw.PupBlock(“sm3 files”, block)
for file in bool:
bool[file] = draw[file].val
if bool[file]==True:
filename = dir+os.sep+file
print ‘====================================’
print file
print ‘====================================’
plik = open(filename,‘rb’)
make_armature(filename)
readdata()
draw_all()
else:
Draw.Exit()

  1. Get it working by running Blender 2.49
  2. Import it Blender 2.59 and fix what is broken
  3. Then import Blender 2.69 and what is broken
  4. Then import Blender 2.79b and fix what is broken
  5. Last, import it Blender 2.81 and fix what is broken

It is easily too hard to migrate everything to latest version when everything changes. But if you migrate version to version, you likely got only small fix to make on each. It is important to keep it working every version.

Also use Pylint to catch some issues.