three.js icon indicating copy to clipboard operation
three.js copied to clipboard

GLTFLoader outputs an empty Group when multiple scenes refer to the same root node

Open wlinna opened this issue 1 year ago • 3 comments

Description

GLTFLoader outputs an empty Group when multiple scenes refer to the same root node. I created a simple cube in Blender, exported to glTF. This works fine. But when I duplicate the scenes array in glTF file and try to view it in ThreeJS, glTF loader returns an empty group.

So this works:

	"scene":0,
	"scenes":[
		{
			"name":"Scene",
			"nodes":[0]
		}
	],

... while this doesn't:

	"scene":0,
	"scenes":[
		{
			"name":"Scene",
			"nodes":[0]
		},
		{
			"name":"Scene2",
			"nodes":[0]
		}
	],

glTF specification allows referring to the same root nodes from multiple scenes, so I consider this a bug.

While I have no reason to duplicate scenes, we do sometimes load such models from external sources, and have no practical way to preprocess the files to remove

Reproduction steps

  1. Download the attached GLB file
  2. Add it to threejs editor, gltfviewer or any three.js scene
  3. Admire the empty scene

duplicatescene.zip

Code


 const loader = new GLTFLoader();
 loader.parse(e.target.result, '', gltf => {
            console.log(gltf)
            if (gltf.scene.isGroup && gltf.scene.children.length === 0) {
            		console.error('gltf.scene is a group with no children!')
            }
  });

Live example

https://jsfiddle.net/jteq8mov/2/

Screenshots

No response

Version

r156 and r162 at least

Device

No response

Browser

No response

OS

No response

wlinna avatar Mar 25 '24 11:03 wlinna

Agreed, while glTF models with multiple scenes are fairly rare, I would consider this a bug as well. Note that we'll need to either parse the node subtree a second time, or use SkeletonUtils.clone to make a copy. Calling node.clone() alone will break connections to the bones of skinned meshes.

donmccurdy avatar Mar 26 '24 03:03 donmccurdy

Hi @donmccurdy,

I would like to work on this issue. Could you please assign it to me or let me know if there are any specific requirements or guidelines I should follow?

Thank you!

parthsali avatar Aug 20 '24 17:08 parthsali

I'm not him, but here's their CONTRIBUTING document https://github.com/mrdoob/three.js/blob/dev/.github/CONTRIBUTING.md

wlinna avatar Aug 20 '24 18:08 wlinna

@parthsali I don't use the assign/unassign feature of GitHub frequently, but a PR would be welcome! Of the two solutions I mentioned above, detecting when a node already has a parent and then using SkeletonUtils.clone is probably where I'd recommend starting.

donmccurdy avatar Sep 15 '24 16:09 donmccurdy

Got it! I’ll start by trying the solution you recommended with SkeletonUtils.clone and see how it goes. I’ll open a PR once I have something ready.

parthsali avatar Sep 15 '24 19:09 parthsali

@parthsali Are you still working on this? I am interested in fixing this bug cc @donmccurdy

yuantianle avatar Apr 13 '25 21:04 yuantianle