Speed up bone merge in export prep #75

Merged
kkbp-dev merged 1 commit from sialim/KKBP_Importer:master into development 2026-06-26 19:08:24 +00:00
Contributor

The "Prep for target application" merge gets extremely slow on high-bone-count models. On a 89-object model with ~600–1000 bones each it runs for hours, sometimes effectively days.

Cause: merge_weights calls mix_weights once per (mesh, bone) pair, and each call creates a VERTEX_WEIGHT_MIX modifier and runs bpy.ops.object.modifier_apply. Every modifier_apply forces a full depsgraph re-evaluation and an undo push, so the work scales with meshes × bones × per-apply overhead and is entirely main-thread bound (extra CPU cores don't help).

Fix: Do the same operation directly on vertex-group data. A VERTEX_WEIGHT_MIX in ADD mode is just target_weight += source_weight for vertices in the source group, followed by deleting the source group — so this rewrites the merge to accumulate weights with vertex_group.add(..., 'ADD') in a single pass per mesh. No modifiers, no operators, no per-bone depsgraph evaluation. Cost is now roughly proportional to total vertex count.
I verified the direct-add output is numerically identical to the modifier-based result.

Tested on this card (https://db.bepis.moe/koikatsu/view/392842), VRM + "very simple" preset: merge dropped from ~[3-4 hours] to ~[30 seconds]. Output weights unchanged.

The "Prep for target application" merge gets extremely slow on high-bone-count models. On a 89-object model with ~600–1000 bones each it runs for hours, sometimes effectively days. Cause: merge_weights calls mix_weights once per (mesh, bone) pair, and each call creates a VERTEX_WEIGHT_MIX modifier and runs bpy.ops.object.modifier_apply. Every modifier_apply forces a full depsgraph re-evaluation and an undo push, so the work scales with meshes × bones × per-apply overhead and is entirely main-thread bound (extra CPU cores don't help). Fix: Do the same operation directly on vertex-group data. A VERTEX_WEIGHT_MIX in ADD mode is just target_weight += source_weight for vertices in the source group, followed by deleting the source group — so this rewrites the merge to accumulate weights with vertex_group.add(..., 'ADD') in a single pass per mesh. No modifiers, no operators, no per-bone depsgraph evaluation. Cost is now roughly proportional to total vertex count. I verified the direct-add output is numerically identical to the modifier-based result. Tested on this card (https://db.bepis.moe/koikatsu/view/392842), VRM + "very simple" preset: merge dropped from ~[3-4 hours] to ~[30 seconds]. Output weights unchanged.
The "Prep for target application" merge gets extremely slow on high-bone-count models. On a 89-object model with ~600–1000 bones each it runs for hours, sometimes effectively days.

Cause: merge_weights calls mix_weights once per (mesh, bone) pair, and each call creates a VERTEX_WEIGHT_MIX modifier and runs bpy.ops.object.modifier_apply. Every modifier_apply forces a full depsgraph re-evaluation and an undo push, so the work scales with meshes × bones × per-apply overhead and is entirely main-thread bound (extra CPU cores don't help).

Fix: Do the same operation directly on vertex-group data. A VERTEX_WEIGHT_MIX in ADD mode is just target_weight += source_weight for vertices in the source group, followed by deleting the source group — so this rewrites the merge to accumulate weights with vertex_group.add(..., 'ADD') in a single pass per mesh. No modifiers, no operators, no per-bone depsgraph evaluation. Cost is now roughly proportional to total vertex count.
I verified the direct-add output is numerically identical to the modifier-based result.

Tested on this card (https://db.bepis.moe/koikatsu/view/392842), VRM + "very simple" preset: merge dropped from ~[3-4 hours] to ~[30 seconds]. Output weights unchanged.
Owner

This is smart! Waiting even two minutes for a few bones to merge was a pain, and like you said the runtime goes from a coffee break to an unbearable wait when the amount of objects and bones increase. Thanks for diagnosing and finding an improvement!

This is smart! Waiting even two minutes for a few bones to merge was a pain, and like you said the runtime goes from a coffee break to an unbearable wait when the amount of objects and bones increase. Thanks for diagnosing and finding an improvement!
kkbp-dev changed target branch from master to development 2026-06-26 19:07:47 +00:00
kkbp-dev merged commit 4a0908d541 into development 2026-06-26 19:08:24 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
kkbp-dev/KKBP_Importer!75
No description provided.