b3d s3d importer for blender

Jack Claw was in development between 2006-2008 and was released in the Humble Frozenbyte Bundle for the community to play and build upon.
demolos
Posts: 3
Joined: Sat Apr 23, 2011 8:54 pm

b3d s3d importer for blender

Postby demolos » Sat Apr 23, 2011 9:00 pm

Hi,

I asked a guy to build an b3d s3d importer for Blender 2.49. :D
http://forum.xentax.com/viewtopic.php?f=16&t=6465
Now we need an b3d s3d exporter. :wink:

User avatar
Urfoex
Posts: 50
Joined: Fri Apr 15, 2011 11:14 am

Re: b3d s3d importer for blender

Postby Urfoex » Sat Apr 23, 2011 9:23 pm

demolos wrote:Hi,

I asked a guy to build an b3d s3d importer for Blender 2.49. :D
http://forum.xentax.com/viewtopic.php?f=16&t=6465
Now we need an b3d s3d exporter. :wink:


Nice!

As Blender 2.57a (stable) is out and Blender 2.5 is very different from 2.49 and much more usable especially for beginners it *maybe* would be better to have a importer and exporter for Blender 2.5x instead of the 2.49 one. But having one for 2.49 in such a quick time is nice.

Here is the python-code from the blend-file:

Code: Select all

#2011-04-19 Glogow Poland Mariusz Szkaradek
import bpy,struct,os
import Blender
from Blender import *
from struct import *
from math import *
from Blender.Mathutils import *

def vertexuv():
   mesh.vertexUV = 1
   for m in range(len(mesh.verts)):
      mesh.verts[m].uvco = Vector(uvcoord[m])
   mesh.update()
   mesh.faceUV = 1
   for fc in mesh.faces: fc.uv = [v.uvco for v in fc.verts];fc.smooth = 1
   mesh.update()   

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


def make_vertex_group():
   for id_0 in range(len(weighting)):
      data = weighting[id_0]
      #print data
      for id_1 in range(2):
         grint = data[0][id_1]
         #try:
         #gr = fix_groups[gr]
         gr = str(grint)
         #gr = bonenames[armature_name][gr]
         w  = data[1][id_1]/100.0
         if grint!=-1:
            if gr not in mesh.getVertGroupNames():
               mesh.addVertGroup(gr)   
               mesh.update()
            mesh.assignVertsToGroup(gr,[id_0],w,1)
         #except:
         #   pass
   mesh.update()


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(n*2))
def H(n):
   return struct.unpack(n*'H', plik.read(n*2))
def i(n):
   return struct.unpack(n*'i', plik.read(n*4))
def f(n):
   return struct.unpack(n*'f', plik.read(n*4))

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)>300:
            break
   return s


def check_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 == 'armature':
            scene.objects.unlink(object)
   for object in bpy.data.objects:
      if object.name == 'armature':
         armobj = Blender.Object.Get('armature')
         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','armature')
   if newarm==None:
      newarm = Armature.New('armature')
      armobj.link(newarm)
   scn.link(armobj)
   newarm.drawType = Armature.STICK
   armobj.drawMode = Blender.Object.DrawModes.XRAY
   for object in scene.objects:
      if 'model' in object.name and object.getType()=='Mesh':
            armobj.makeParentDeform([object],1,0)

def make_bone():
   newarm.makeEditable()
   for bone_id in range(len(bonesdata)):
      bonedata = bonesdata[bone_id]
      bonename = bonedata[0]
      eb = Armature.Editbone()
      newarm.bones[bonename] = eb
   newarm.update()

def make_bone_parent():
   newarm.makeEditable()
   for bone_id in range(len(bonesdata)):
      bonedata = bonesdata[bone_id]
      parent_id = bonedata[3]
      bonename = bonedata[0]   
      if parent_id !=-1:
         bone = newarm.bones[bonename] 
         boneparent = newarm.bones[bonesdata[parent_id][0]]
         bone.parent = boneparent
   newarm.update()

def make_bone_position():
   newarm.makeEditable()
   for bone_id in range(len(bonesdata)):
      bonedata = bonesdata[bone_id]
      namebone = bonedata[0]
      pos = bonedata[1]
      rot = bonedata[2]
      qx,qy,qz,qw = rot[0],rot[1],rot[2],rot[3]
      rot = Quaternion(qw,qx,qy,qz)
      rot = rot.toMatrix().invert()
      bone = newarm.bones[namebone]
      #if bone.parent:
      #   bone.head =   bone.parent.head+Vector(pos) * bone.parent.matrix
         #tempM = rot * bone.parent.matrix
         #bone.matrix = tempM
      #else:
      bone.head = Vector(pos)
      bone.matrix = rot
      bvec = bone.tail- bone.head
      bvec.normalize()
      bone.tail = bone.head + 0.01 * bvec
   newarm.update()

def skeleton():
   check_armature(),make_bone(),make_bone_parent();make_bone_position()


def find_0():
   s=''
   while(True):
      litera =  struct.unpack('c',plik.read(1))[0]
      if  litera=='\x00':
         break
      else:
         s+=litera
   return s


def s3d():
   global vertexes,faceslist,weighting,uvcoord
   print word(4)
   data = H(9)
   print data
   images = []
   materials = []
   #textures
   for m in range(data[2]):
      image_name = find_0().split('.')[0]
      images.append(image_name+'.dds')
      for file_name in g:
         if image_name in file_name and '.dds' in file_name:
            print file_name
            if file_name not in Blender.Image.Get():
               Blender.Image.Load(dir+os.sep+file_name)   
      B(9)
   #materials
   for m in range(data[3]):
      material_name = find_0()
      materials.append(material_name)
      mat = Material.New(material_name)
      image_id = H(1)[0]
      try:
         tex = Texture.New('diff')
         tex.setType('Image')       
         #tex.image = Blender.Image.Get(images[image_id])
         tex.image =  Blender.Image.Load(dir+os.sep+images[image_id])
         mat.setTexture(0,tex,Texture.TexCo.UV,Texture.MapTo.COL)
      except:
         pass


      data1 = H(4)
      print data1
      f(3)
      if data1[2] == 1:
         print B(63)
      elif data1[2] == 3:
         print B(64)
      else:
         print B(55)
   #objects
   for m in range(data[4]):
      print find_0()
      print find_0()
      mat_id = H(1)[0]
      print B(24),f(4)
      B(1)
      data2 = H(4)
      print data2
      vertexes = []
      faceslist = []
      weighting = []
      uvcoord = []
      for n in range(data2[1]):
         back = plik.tell()
         #print m,f(3)
         vertexes.append(f(3))
         f(3)#normals
         uvcoord.append([f(1)[0],-f(1)[0]])
         plik.seek(back+40)
      for n in range(data2[2]):
         faceslist.append(H(3))
      for n in range(data2[1]):
         weighting.append([i(2),B(2)]) 
         #break
      #break 
      drawmesh('model-'+str(m))
      mesh.materials+=[Material.Get(materials[mat_id])]
   print plik.tell()


def b3d():
   global bonesdata
   bonesdata = []
   print word(4)
   B(5)
   nBones = i(1)[0]
   for m in range(nBones):
      bone_name = find_0()
      f(7)
      pos = f(3)
      rot = f(4)
      f(6)
      i(2)
      parent_id = i(1)[0]
      bonesdata.append([bone_name,pos,rot,parent_id])
   
   skeleton()
   Redraw()
   scene = bpy.data.scenes.active
   for object in scene.objects:
      if 'model' in object.name and object.getType()=='Mesh':
         mesh = object.getData(mesh=1)
         for m in range(len(bonesdata)):
            print object.name,m
            if str(m) in mesh.getVertGroupNames():
               mesh.renameVertGroup(str(m),bonesdata[m][0])
         armobj.makeParentDeform([object],1,0)
   print plik.tell()




def import_model(filename):
   global plik,g,dir
   plik = open(filename,'rb')
   print '============================'
   print filename
   print '============================'
   file = sys.basename(filename)
   dir = sys.dirname(filename)
   g = os.listdir(dir)
   if file.split('.')[-1].lower() == 's3d':
      s3d()
   if file.split('.')[-1].lower() == 'b3d':
      b3d()
   Redraw()
   
   
Window.FileSelector (import_model)   
+-----------------------------------------------------------------\
| Debian testing 64Bit on
| * AMD Phenom x4 905e (4x2500Mhz)
| * 6GB Ram
| * AMD/ATI Radeon HD4770 (fglrx)
+-----------------------------------------------------------------/

Twigger
Posts: 1
Joined: Mon Oct 10, 2011 4:50 pm

Re: b3d s3d importer for blender

Postby Twigger » Mon Oct 10, 2011 4:53 pm

Hi, I'm am trying to import a 3DS object with Blender. I've tried a model and even a simple box - each time I get the error "Python Script Error - Check Console". !! I'm stuck at this point, I thought it was because the files were on my desktop - and the path lenght was too long, so moved the file to a folder on the local HDD and still same problem.


Return to “Jack Claw Feedback & Development”

Who is online

Users browsing this forum: No registered users and 1 guest