Skip to content

Commit

Permalink
fix: unexpected constrainted node rotation on export
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Dec 9, 2024
1 parent 31d1323 commit 90809f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/io_scene_vrm/editor/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ class ExportConstraint:
aim_constraints: dict[str, DampedTrackConstraint]
rotation_constraints: dict[str, CopyRotationConstraint]

@property
def all_constraints(self) -> list[tuple[str, Constraint]]:
all_constraints: list[tuple[str, Constraint]] = []
all_constraints.extend(self.roll_constraints.items())
all_constraints.extend(self.aim_constraints.items())
all_constraints.extend(self.rotation_constraints.items())
return all_constraints


def roll_constraint_or_none(
constraint: Constraint,
Expand Down
41 changes: 41 additions & 0 deletions src/io_scene_vrm/exporter/vrm1_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Armature,
ArmatureModifier,
Bone,
Constraint,
Context,
CopyRotationConstraint,
Curve,
Expand Down Expand Up @@ -2146,6 +2147,45 @@ def setup_dummy_human_bones(
if dummy_edit_bone:
armature_data.edit_bones.remove(dummy_edit_bone)

@contextmanager
def disable_constraints(self, context: Context) -> Iterator[None]:
constraint: Optional[Constraint] = None

object_constraints, bone_constraints, _ = search.export_constraints(
self.export_objects, self.armature
)
object_name_and_constraint_name: list[tuple[str, str]] = []
for object_name, constraint in object_constraints.all_constraints:
constraint.mute = True
object_name_and_constraint_name.append((object_name, constraint.name))

bone_name_and_constraint_name: list[tuple[str, str]] = []
for bone_name, constraint in bone_constraints.all_constraints:
constraint.mute = True
bone_name_and_constraint_name.append((bone_name, constraint.name))

try:
yield
finally:
# ObjectやConstraintが消えている可能性を考慮し、それらを取得し直す
for object_name, constraint_name in object_name_and_constraint_name:
obj = context.blend_data.objects.get(object_name)
if not obj:
continue
constraint = obj.constraints.get(constraint_name)
if not constraint:
continue
constraint.mute = False

for bone_name, constraint_name in bone_name_and_constraint_name:
bone = self.armature.pose.bones.get(bone_name)
if not bone:
continue
constraint = bone.constraints.get(constraint_name)
if not constraint:
continue
constraint.mute = False

@contextmanager
def assign_export_custom_properties(
self, armature_data: Armature
Expand Down Expand Up @@ -2214,6 +2254,7 @@ def export_vrm(self) -> Optional[bytes]:
self.overwrite_object_visibility_and_selection(),
):
with (
self.disable_constraints(self.context),
self.hide_mtoon1_outline_geometry_nodes(self.context),
self.disable_mtoon1_material_nodes(self.context),
self.mount_skinned_mesh_parent(),
Expand Down

0 comments on commit 90809f0

Please sign in to comment.