Entities instantiated from templates are missing their anim layer masks

I have one template which has an anim component with a single layer and a mask. If I instantiate a new entity from this template like so:

let template = this.app.assets.find('Player');
let player = template.resource.instantiate();
this.app.root.addChild(player);

The mask will not be present on the newly instantiated entity. All the animations & state graph etc work fine, just not the mask. I can place the same entity in the scene using the editor and the mask works fine.

chrome-capture (3)
In this example, I’m using a mask that disables the bones on the left side of the character’s body. The character on the left was placed manually using the editor and the one on the right was instantiated from the template. As we can see, the mask applies to the left one but not the right. It’s unclear if this is intended behaviour or a bug with anim layer masks when combined with the use of templates.

I can still assign masks using the AnimComponentLayer assignMask() method which works fine.

Here is an example project.

Ah right, sounds like a bug, but not fully sure.

@Elliott @yaustar may know more.

1 Like

Created a ticket for this: https://github.com/playcanvas/engine/issues/4031

1 Like

Hi @yaustar. The ticket for this has been closed/completed but I’m encountering issues with this now.

In my case it seems to be due to having additional layers on the animation state graph and applying a mask to the second layer. That’s the only real difference I can see compared to the example project in this post already.

On the left in the video is the template already in scene (with working layer mask). On the right is the instantiated template entity (which does not have a working layer mask)

Here’s an example project

Thanks for your help

Having a quick look here at the fix: [AnimComponent] Fix for layer masks in cloned components by ellthompson · Pull Request #4075 · playcanvas/engine · GitHub

It seems to be handling multiple layers correctly, and the masks in data seems to be used correctly in the initializeComponentData function in the same file, so it’s not obvious what could be the problem. Perhaps try to debug those two functions to see if some condition fails?

Ah yes, it seems to fail the condition check here in that fix as the rootBone is set but not to the entity being cloned (it is set to a child)

// If the component animaites from the components entity, any layer mask hierarchy should be updated from the old entity to the cloned entity.
if (!entity.anim.rootBone || entity.anim.rootBone === entity) {

Perhaps

entity.anim.rootBone === entity

should be

entity.anim.rootBone.isDescendantOf(entity)