armatures and bones help

ok so i’ve made an importer/exporter for a custom model file format with blender but I am now trying to improve it so that it can import the armature but i’m having problems…

So i have an array of class joints that i’ve read from the file, each joint has a transform/matrix. and a parentbone. I’ve created a new armature and created the bones and linked the parents. Then i tried setting the transform matrix using

bone.transform(matrix, scale=False, roll=False)

but i get just a bones almost in a pile, i’ve tried with and without scale/roll options but same problem.

I’ve been looking through other importers and online but i’m really not sure what to do next.

Any help is much appreciated… here is what i have

#create armature
bpy.ops.object.armature_add()
obj = bpy.context.scene.objects.active
obj.name = "Armature"
arm = obj.data

#make bones
bpy.ops.object.mode_set(mode='EDIT')
for j in joints:
	bone = arm.edit_bones.new(j._name)
	bone.head = (0,0,0)
	bone.tail = (0,0,1)
bpy.context.scene.update()

#make bone parents
for j in joints:
	nameparent=None
	if j._parentId in indexedJoints:
		nameparent = indexedJoints[j._parentId]._name
	if nameparent != None:
		bone = arm.edit_bones[j._name]
		parentbone = arm.edit_bones[nameparent]
		bone.parent = parentbone
		bone.use_connect = True
bpy.context.scene.update()

#set bone positioning/matrix
for j in joints:
	mat = j._transform.getMatrix()
	(pos, rot, scale) = mat.decompose()
	bone.transform(rot, scale=False, roll=True)
bpy.context.scene.update()

settings head and tail of bones to same coordinate is not a good idea:
bone.head = (0,0,0)
bone.tail = (0,0,1)
Blender may remove them for being in the same spot.

If an armature looks crunched together, the coordinates might be absolute and not relative to each other

Ok well i tried something different and i’m not sure if this is better or not but at least its not all over the place.

#create bonetable
boneTable1 = []

for j in joints:
	if j._parentId in indexedJoints:
		nameparent = indexedJoints[j._parentId]._name
	else:
		nameparent=None
	mat = j._transform.getMatrix()
	(pos, rot, scale) = mat.decompose()
	tmp = (j._name,nameparent,pos,mat)
	boneTable1.append(tmp)
rig = self.createRig('Rig', (0,0,0), boneTable1)


def createRig(self, name, origin, boneTable):
		# Create armature and object
		bpy.ops.object.add(
			type='ARMATURE', 
			enter_editmode=True,
			location=origin)
		ob = bpy.context.object
		ob.show_x_ray = True
		ob.name = name
		amt = ob.data
		amt.name = name+'Amt'
		amt.show_axes = True
	 
		# Create bones
		bpy.ops.object.mode_set(mode='EDIT')
		for (bname, pname, vector, matrix) in boneTable:        
			bone = amt.edit_bones.new(bname)
			
			if pname:
				parent = amt.edit_bones[pname]
				bone.parent = parent
				bone.head = parent.tail
				bone.use_connect = False
				
				mW = parent.matrix
				imW = mW.copy()
				imW.invert()
				m1 = mW * imW
				(trans, rot, scale) = m1.decompose()
			else:
				bone.head = (0,0,0)
				rot = Matrix.Translation((0,0,0))	# identity matrix
			bone.tail = rot * Vector(vector) + bone.head
		bpy.ops.object.mode_set(mode='OBJECT')
		return ob

Any ideas what i’m doing wrong, i think maybe its the whole local and global space stuff or something