Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vpd import #162

Open
wants to merge 4 commits into
base: blender-v4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions mmd_tools/core/vpd/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ def __init__(self, filepath, scale=1.0, bone_mapper=None, use_pose_mode=False):
self.__scale = scale
self.__bone_mapper = bone_mapper
if use_pose_mode:
self.__bone_util_cls = importer.BoneConverterPoseMode
self.__bone_util_cls = importer.BoneConverter
#self.__bone_util_cls = importer.BoneConverterPoseMode
self.__assignToArmature = self.__assignToArmaturePoseMode
else:
self.__bone_util_cls = importer.BoneConverter
self.__assignToArmature = self.__assignToArmatureSimple
logging.info("Loaded %s", self.__vpd_file)

def __assignToArmaturePoseMode(self, armObj):
pose_orig = {b: b.matrix_basis.copy() for b in armObj.pose.bones}
try:
self.__assignToArmatureSimple(armObj, reset_transform=False)
finally:
for bone, matrix_basis in pose_orig.items():
bone.matrix_basis = matrix_basis
self.__assignToArmatureSimple(armObj, reset_transform=False)
#pose_orig = {b: b.matrix_basis.copy() for b in armObj.pose.bones}
#try:
# self.__assignToArmatureSimple(armObj, reset_transform=False)
#finally:
# for bone, matrix_basis in pose_orig.items():
# bone.matrix_basis = matrix_basis

def __assignToArmatureSimple(self, armObj: bpy.types.Object, reset_transform=True):
logging.info(' - assigning to armature "%s"', armObj.name)
Expand All @@ -54,22 +56,41 @@ def __assignToArmatureSimple(self, armObj: bpy.types.Object, reset_transform=Tru
assert bone not in pose_data
pose_data[bone] = Matrix.Translation(loc) @ rot.to_matrix().to_4x4()

# Check if animation data exists
if armObj.animation_data is None:
armObj.animation_data_create()

# Check if an action exists
if armObj.animation_data.action is None:
action = bpy.data.actions.new(name="PoseLib")
armObj.animation_data.action = action
else:
action = armObj.animation_data.action

# Get the current frame
current_frame = bpy.context.scene.frame_current

# Update and keyframe only the bones affected by the current VPD file
for bone in armObj.pose.bones:
vpd_pose = pose_data.get(bone, None)
bone.bone.select = bool(vpd_pose)
if vpd_pose:
bone.matrix_basis = vpd_pose
bone.keyframe_insert(data_path="location", frame=current_frame)
bone.keyframe_insert(data_path="rotation_quaternion", frame=current_frame)
elif reset_transform:
bone.matrix_basis.identity()
bone.keyframe_insert(data_path="location", frame=current_frame)
bone.keyframe_insert(data_path="rotation_quaternion", frame=current_frame)

# FIXME: armObj.pose_library is None when the armature is not in pose mode
if armObj.pose_library is None:
armObj.pose_library = bpy.data.actions.new(name="PoseLib")
# Add or update a pose marker
if self.__pose_name not in action.pose_markers:
marker = action.pose_markers.new(self.__pose_name)
else:
marker = action.pose_markers[self.__pose_name]
marker.frame = current_frame

frames = [m.frame for m in armObj.pose_library.pose_markers]
frame_max = max(frames) if len(frames) else 0
# FIXME: poselib.pose_add is deprecated, use animation_data instead
bpy.ops.poselib.pose_add(frame=frame_max + 1, name=self.__pose_name)
# Ensure the timeline is updated
bpy.context.view_layer.update()

def __assignToMesh(self, meshObj):
if meshObj.data.shape_keys is None:
Expand All @@ -92,13 +113,7 @@ def assign(self, obj):
if obj is None:
return
if obj.type == "ARMATURE":
with FnContext.temp_override_objects(
FnContext.ensure_context(),
active_object=obj,
selected_objects=[obj],
):
bpy.ops.object.mode_set(mode="POSE")
self.__assignToArmature(obj)
self.__assignToArmature(obj)
elif obj.type == "MESH":
self.__assignToMesh(obj)
else:
Expand Down