model-viewer: Cloning animated models doesn't work

I tried it very simply with:

clone = viewer.entities[0].clone()
viewer.app.root.addChild(clone)
clone.translate(1, 0, 0)
clone.anim.playing

Anything I’m doing wrong? The second model is just not animated, even though all properties seem right.

image

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Sorry, @kungfooman. This was fixed in the last release: https://github.com/playcanvas/engine/releases/tag/v1.46.5

Fix for cloned anim and render components (https://github.com/playcanvas/engine/pull/3463)

I don’t know which commit fixed it, but its working now:

image

Code to spawn 8x8 models and playing random animations on them:


function spawn8x8(s = 2) {
	var clones = [];
	for (var i=0; i<8; i++) {
		for (var j=0; j<8; j++) {
			var ee = viewer.entities[0].clone();
			clones.push(ee);
			ee.translate(j * s, 0, i * s);
			viewer.app.root.addChild(ee)
		}
	}
	return clones;
}

clones = spawn8x8();

function playRandomAnimation(entity) {
  var layer = entity.anim.layers[0];
  var states = layer.states;
  var randomState = states[Math.trunc(Math.random() * states.length)];
  layer.play(randomState);
  return randomState;
}

clones.map(playRandomAnimation);

@kungfooman We are looking at the issue and looks like it be a bigger thing to handle and needs discussion with the wider team.

Work around is to call .rebind() on the anim component after the clone:

cloneEntity.anim.rebind();

Hm… All the animation data and information is available in the clone, wondering if this is a viewer or engine issue right now 🤔

Edit: Looks like when the clone is added to the scene graph, the evaluator fails to resolve the path in adding the clip. https://github.com/playcanvas/engine/blob/master/src/anim/evaluator/anim-evaluator.js#L135

I will need to talk to @ellthompson on this as this is over my head a bit. I think it is an order of initialisation issue where the model component doesn’t add the children before the anim component attempts to resolve the bindings

Edit2: Confirmed, during the clone, the anim component is trying to resolve the graph before the children are created. Thinking that maybe this should be done when the entity is initialised, not cloned.

Looks like if the stategraph is built in code (as done via the viewer here https://github.com/playcanvas/playcanvas-viewer/blob/4b2868ef938db592c358685f6b3c130ec73c0716/src/viewer.ts#L1202), cloning the component doesn’t clone or reference that data.

It’s probably worth noting you shouldn’t have this issue with a Model Component and Anim Component combo. It’s only the newer Render Component and Anim Component combination that has this issue at the moment

The npm modules are always the stable version so it still hasn’t got the fix from the PR.

The next release with this fix would be in v1.44.0

Edit: oh wait, I see what you mean. I need to have a closer look

I’m assigning this to @ellthompson to investigate. When the entity set up for animation is cloned, it should be able to play the animation. This would be great to add as a test to some animation engine example perhaps as well.