5
votes

I have a Blender scene in which objects are organized into collections.

When I export the scene in gltf format and import it in my Three.js project there is no track of these collections.

What I expected was that for each collection correspond a group.

How can I get this result? Is it possible?

2

2 Answers

2
votes

I think the most robust way may be to create one Empty object per collection in Blender, and use it as the parent of the rest of the contents of its collection. Give it a name that you'll be able to look up from the ThreeJS side.

In glTF there are no "collections" per se, but there is a parent/child hierarchy of nodes, and you can use that for grouping purposes.

As a side note, glTF does support multiple "scenes", but I don't recall if the exporter will write out multiple Blender scenes as such. Most glTF readers don't do a good job of reading that anyway. I suspect that using Empty nodes as parents of groups to organize things will be the more well-tested code path.

1
votes

emackey's answer is good, for the use case:

objects_x children of parent y = collection1
objects_z children of parent a = collection2

Three.js has an Object3D property called Layers, which allow this use case:

objects_x = collection1
objects_z = collection2
objects_x & objects_z = collection3

This allows partial display of collections, or for a single object to appear in multiple collections.

The only way I have found to do this is to manually assign Three.js Layer values to each object, like this:

gltf.scene.children[0].layers.enable(1);
camera.layers.enable(1)

You can then toggle the visibility of the objects in the layer 1 with the following:

camera.layers.toggle(1);

Have a look here for more detail: https://threejs.org/examples/#webgl_layers