Skip to content
Snippets Groups Projects
Commit 3a2eaa64 authored by John Phan's avatar John Phan
Browse files

Fixed bone operate and matrix.

parent d00841b4
No related branches found
No related tags found
No related merge requests found
...@@ -146,11 +146,13 @@ def fixRoll(b): ...@@ -146,11 +146,13 @@ def fixRoll(b):
#align Z-axis #align Z-axis
b.roll -= math.degrees(math.atan2(v[0]*v[2]*(1 - v[1]),v[0]*v[0] + v[1]*v[2]*v[2])) b.roll -= math.degrees(math.atan2(v[0]*v[2]*(1 - v[1]),v[0]*v[0] + v[1]*v[2]*v[2]))
def pskimport(infile): def pskimport(infile,importmesh,importbone,bDebugLogPSK):
global DEBUGLOG global DEBUGLOG
DEBUGLOG = bDebugLogPSK
print ("--------------------------------------------------") print ("--------------------------------------------------")
print ("---------SCRIPT EXECUTING PYTHON IMPORTER---------") print ("---------SCRIPT EXECUTING PYTHON IMPORTER---------")
print ("--------------------------------------------------") print ("--------------------------------------------------")
print (" DEBUG Log:",bDebugLogPSK)
print ("Importing file: ", infile) print ("Importing file: ", infile)
md5_bones=[] md5_bones=[]
...@@ -163,7 +165,7 @@ def pskimport(infile): ...@@ -163,7 +165,7 @@ def pskimport(infile):
def printlog(strdata): def printlog(strdata):
if (DEBUGLOG): if (DEBUGLOG):
logf.write(strdata) logf.write(strdata)
objName = infile.split('\\')[-1].split('.')[0] objName = infile.split('\\')[-1].split('.')[0]
me_ob = bpy.data.meshes.new(objName) me_ob = bpy.data.meshes.new(objName)
...@@ -351,73 +353,104 @@ def pskimport(infile): ...@@ -351,73 +353,104 @@ def pskimport(infile):
objectname = "armaturedata" objectname = "armaturedata"
bfound = False bfound = False
arm = None arm = None
for obj in bpy.data.objects: if importbone:
if (obj.name == meshname): for obj in bpy.data.objects:
bfound = True if (obj.name == meshname):
arm = obj bfound = True
break arm = obj
break
if bfound == False:
armdata = bpy.data.armatures.new(objectname) if bfound == False:
ob_new = bpy.data.objects.new(meshname, armdata) '''
#ob_new = bpy.data.objects.new(meshname, 'ARMATURE') armdata = bpy.data.armatures.new(objectname)
#ob_new.data = armdata ob_new = bpy.data.objects.new(meshname, armdata)
bpy.context.scene.objects.link(ob_new) #ob_new = bpy.data.objects.new(meshname, 'ARMATURE')
#bpy.ops.object.mode_set(mode='OBJECT') #ob_new.data = armdata
for i in bpy.context.scene.objects: i.select = False #deselect all objects bpy.context.scene.objects.link(ob_new)
ob_new.select = True #bpy.ops.object.mode_set(mode='OBJECT')
#set current armature to edit the bone for i in bpy.context.scene.objects: i.select = False #deselect all objects
bpy.context.scene.objects.active = ob_new ob_new.select = True
#set mode to able to edit the bone #set current armature to edit the bone
bpy.ops.object.mode_set(mode='EDIT') bpy.context.scene.objects.active = ob_new
#newbone = ob_new.data.edit_bones.new('test') #set mode to able to edit the bone
#newbone.tail.y = 1 bpy.ops.object.mode_set(mode='EDIT')
print("creating bone(s)")
for bone in md5_bones: #newbone = ob_new.data.edit_bones.new('test')
#print(dir(bone)) #newbone.tail.y = 1
newbone = ob_new.data.edit_bones.new(bone.name) print("creating bone(s)")
#parent the bone for bone in md5_bones:
parentbone = None #print(dir(bone))
print("bone name:",bone.name) bpy.ops.object.mode_set(mode='EDIT')
#note bone location is set in the real space or global not local newbone = ob_new.data.edit_bones.new(bone.name)
if bone.name != bone.parent: '''
pos_x = bone.bindpos[0] armdata = bpy.data.armatures.new(objectname)
pos_y = bone.bindpos[1] ob_new = bpy.data.objects.new(meshname, armdata)
pos_z = bone.bindpos[2] #ob_new = bpy.data.objects.new(meshname, 'ARMATURE')
#ob_new.data = armdata
bpy.context.scene.objects.link(ob_new)
#bpy.ops.object.mode_set(mode='OBJECT')
for i in bpy.context.scene.objects: i.select = False #deselect all objects
ob_new.select = True
#set current armature to edit the bone
bpy.context.scene.objects.active = ob_new
#set mode to able to edit the bone
bpy.ops.object.mode_set(mode='EDIT')
#newbone = ob_new.data.edit_bones.new('test')
#newbone.tail.y = 1
print("creating bone(s)")
for bone in md5_bones:
#print(dir(bone))
bpy.ops.object.mode_set(mode='EDIT')
newbone = ob_new.data.edit_bones.new(bone.name)
#parent the bone
parentbone = None
print("bone name:",bone.name)
#note bone location is set in the real space or global not local
if bone.name != bone.parent:
pos_x = bone.bindpos[0]
pos_y = bone.bindpos[1]
pos_z = bone.bindpos[2]
#print( "LINKING:" , bone.parent ,"j")
parentbone = ob_new.data.edit_bones[bone.parent]
newbone.parent = parentbone
rotmatrix = bone.bindmat.to_matrix().to_4x4().to_3x3() # XXX, redundant matrix conversion?
#parent_head = parentbone.head * parentbone.matrix.to_quaternion().inverse()
#parent_tail = parentbone.tail * parentbone.matrix.to_quaternion().inverse()
#location=Vector(pos_x,pos_y,pos_z)
#set_position = (parent_tail - parent_head) + location
#print("tmp head:",set_position)
#pos_x = set_position.x
#pos_y = set_position.y
#pos_z = set_position.z
newbone.head.x = parentbone.head.x + pos_x
newbone.head.y = parentbone.head.y + pos_y
newbone.head.z = parentbone.head.z + pos_z
#print("head:",newbone.head)
newbone.tail.x = parentbone.head.x + (pos_x + bonesize * rotmatrix[1][0])
newbone.tail.y = parentbone.head.y + (pos_y + bonesize * rotmatrix[1][1])
newbone.tail.z = parentbone.head.z + (pos_z + bonesize * rotmatrix[1][2])
else:
print("rotmatrix:",dir(bone.bindmat.to_matrix().resize_4x4()))
#rotmatrix = bone.bindmat.to_matrix().resize_4x4().to_3x3() # XXX, redundant matrix conversion?
rotmatrix = bone.bindmat.to_matrix().to_3x3() # XXX, redundant matrix conversion?
newbone.head.x = bone.bindpos[0]
newbone.head.y = bone.bindpos[1]
newbone.head.z = bone.bindpos[2]
newbone.tail.x = bone.bindpos[0] + bonesize * rotmatrix[1][0]
newbone.tail.y = bone.bindpos[1] + bonesize * rotmatrix[1][1]
newbone.tail.z = bone.bindpos[2] + bonesize * rotmatrix[1][2]
#print("no parent")
#print( "LINKING:" , bone.parent ,"j")
parentbone = ob_new.data.edit_bones[bone.parent]
newbone.parent = parentbone
rotmatrix = bone.bindmat.to_matrix().to_4x4().to_3x3() # XXX, redundant matrix conversion?
#parent_head = parentbone.head * parentbone.matrix.to_quaternion().inverse()
#parent_tail = parentbone.tail * parentbone.matrix.to_quaternion().inverse()
#location=Vector(pos_x,pos_y,pos_z)
#set_position = (parent_tail - parent_head) + location
#print("tmp head:",set_position)
#pos_x = set_position.x
#pos_y = set_position.y
#pos_z = set_position.z
newbone.head.x = parentbone.head.x + pos_x
newbone.head.y = parentbone.head.y + pos_y
newbone.head.z = parentbone.head.z + pos_z
print("head:",newbone.head)
newbone.tail.x = parentbone.head.x + (pos_x + bonesize * rotmatrix[1][0])
newbone.tail.y = parentbone.head.y + (pos_y + bonesize * rotmatrix[1][1])
newbone.tail.z = parentbone.head.z + (pos_z + bonesize * rotmatrix[1][2])
else:
rotmatrix = bone.bindmat.to_matrix().resize_4x4().to_3x3() # XXX, redundant matrix conversion?
newbone.head.x = bone.bindpos[0]
newbone.head.y = bone.bindpos[1]
newbone.head.z = bone.bindpos[2]
newbone.tail.x = bone.bindpos[0] + bonesize * rotmatrix[1][0]
newbone.tail.y = bone.bindpos[1] + bonesize * rotmatrix[1][1]
newbone.tail.z = bone.bindpos[2] + bonesize * rotmatrix[1][2]
#print("no parent")
bpy.context.scene.update() bpy.context.scene.update()
#================================================================================================== #==================================================================================================
...@@ -556,13 +589,13 @@ def pskimport(infile): ...@@ -556,13 +589,13 @@ def pskimport(infile):
print ("PSK2Blender completed") print ("PSK2Blender completed")
#End of def pskimport######################### #End of def pskimport#########################
def getInputFilename(filename): def getInputFilename(filename,importmesh,importbone,bDebugLogPSK):
checktype = filename.split('\\')[-1].split('.')[1] checktype = filename.split('\\')[-1].split('.')[1]
print ("------------",filename) print ("------------",filename)
if checktype.upper() != 'PSK': if checktype.upper() != 'PSK':
print (" Selected file = ",filename) print (" Selected file = ",filename)
raise (IOError, "The selected input file is not a *.psk file") raise (IOError, "The selected input file is not a *.psk file")
pskimport(filename) pskimport(filename,importmesh,importbone,bDebugLogPSK)
from bpy.props import * from bpy.props import *
...@@ -574,9 +607,12 @@ class IMPORT_OT_psk(bpy.types.Operator): ...@@ -574,9 +607,12 @@ class IMPORT_OT_psk(bpy.types.Operator):
# List of operator properties, the attributes will be assigned # List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling. # to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "") filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "")
importmesh = BoolProperty(name="Mesh", description="Import mesh only. (not yet build.)", default=True)
importbone = BoolProperty(name="Bones", description="Import bones only. Current not working yet.", default=True)
bDebugLogPSK = BoolProperty(name="Debug Log.txt", description="Log the output of raw format. It will save in current file dir. Note this just for testing.", default=False)
def execute(self, context): def execute(self, context):
getInputFilename(self.filepath) getInputFilename(self.filepath,self.importmesh,self.importbone,self.bDebugLogPSK)
return {'FINISHED'} return {'FINISHED'}
def invoke(self, context, event): def invoke(self, context, event):
...@@ -587,7 +623,6 @@ class IMPORT_OT_psk(bpy.types.Operator): ...@@ -587,7 +623,6 @@ class IMPORT_OT_psk(bpy.types.Operator):
def menu_func(self, context): def menu_func(self, context):
self.layout.operator(IMPORT_OT_psk.bl_idname, text="Skeleton Mesh (.psk)") self.layout.operator(IMPORT_OT_psk.bl_idname, text="Skeleton Mesh (.psk)")
def register(): def register():
bpy.types.INFO_MT_file_import.append(menu_func) bpy.types.INFO_MT_file_import.append(menu_func)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment