Skip to content

Commit

Permalink
Small changes to PyWMD
Browse files Browse the repository at this point in the history
Tried to import armature - fail
  • Loading branch information
REDxEYE committed Feb 2, 2020
1 parent b6db7f9 commit 1a3ae9a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PyWMD
Submodule PyWMD updated from 29401f to fbdde7
84 changes: 83 additions & 1 deletion model_import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,85 @@
from PyWMD.pragma_model import PragmaModel
from pathlib import Path

if __name__ == '__main__':
import sys

sys.path.append(r"F:\PYTHON_STUFF\blender_wmd")

from PyWMD.byte_io_wmd import ByteIO
from PyWMD.pragma_model import PragmaModel, PragmaBone
import bpy
from mathutils import Vector, Quaternion, Matrix


def create_child_bones(root_bone: PragmaBone, parent, armature):
for child in root_bone.childs:
bone = armature.edit_bones.new(child.name)
bone.parent = parent
create_child_bones(child, bone, armature)


def pose_bone(root_bone: PragmaBone, armature):
bl_bone = armature.pose.bones.get(root_bone.name)
pos = Vector(root_bone.position.values)
rot = Quaternion(root_bone.rotation.values)
mat = Matrix.Translation(pos) @ rot.to_matrix().to_4x4()
bl_bone.matrix_basis.identity()
if bl_bone.parent:
bl_bone.matrix = bl_bone.parent.matrix @ mat
else:
bl_bone.matrix = mat
for child in root_bone.childs:
pose_bone(child, armature)


def create_armature(model: PragmaModel, collection):
bpy.ops.object.armature_add(enter_editmode=True)

armature_obj = bpy.context.object
try:
bpy.context.scene.collection.objects.unlink(armature_obj)
except:
pass

collection.objects.link(armature_obj)
armature_obj.name = model.name + '_ARM'
armature = armature_obj.data
armature.name = model.name + "_ARM_DATA"

armature_obj.select_set(True)
bpy.context.view_layer.objects.active = armature_obj
bpy.ops.object.mode_set(mode='EDIT')
armature.edit_bones.remove(armature.edit_bones[0])

for root_bone in model.armature.roots:
bone = armature.edit_bones.new(root_bone.name)
create_child_bones(root_bone, bone, armature)

for bone in model.armature.bones: # type: PragmaBone
bl_bone = armature.edit_bones.get(bone.name)
bl_bone.tail = Vector([0, 0, 1]) + bl_bone.head

bpy.ops.object.mode_set(mode='POSE')
for root_bone in model.armature.roots:
pose_bone(root_bone, armature_obj)

bpy.ops.pose.armature_apply()
bpy.ops.object.mode_set(mode='OBJECT')
return armature_obj


def import_model(model_path: str):
model_path = Path(model_path)
reader = ByteIO(path=model_path)
if PragmaModel.check_header(reader):
model = PragmaModel()
model.from_file(reader)
model.set_name(model_path.stem)
main_collection = bpy.data.collections.new(model_path.stem)
bpy.context.scene.collection.children.link(main_collection)
create_armature(model, main_collection)
pass


if __name__ == '__main__':
import_model(r"F:\PYTHON_STUFF\PyWMD\test_data\medic.wmd")

0 comments on commit 1a3ae9a

Please sign in to comment.