Adding instances and groups in realtime

I have x many objects.
I want them all to be in the scene.
So I add them to the scene.
No problem.

But sometimes I want to refer to those objects in terms of groups.

So I make a group, add the objects to it and add that group to the scene.
Compiler doesn’t complain.
Off course, the objects were there to begin with so I can’t really tell if group adding is working.

But now I try to manipulate the objects via this new group and it doesn’t work.
When I do for instance,


The objects of the group do not move.

But If I avoid adding the instances in the beginning.
Then I can add the group and then I can manipulate their location through the group.

Is there no way to just add all the instances to the scene.
Then define the groups?

I have a suspicion each object can only belong to one group at most. Is that the case?

Our world is full of objects.
And those objects are all in all kinds of groups.

I don’t think it is correct, to define groups by adding them to the scene.
Those are 2 different actions. Defining the group, and adding to the scene.

We should just add to the scene, what should be added to the scene.
Then define whatever groups we want to.

Game on.

If you are talking about THREE.Group, then yes. In scene graphs (like in other tree structures) nodes can only have one parent.

Then maybe the word group is a misnomer.

In the outer world objects can be in multiple groups at the same time.
So it can be confusing when people starting using threejs and the word group
doesn’t really have the same meaning anymore.

Maybe the word group should really be something like parent.
For that communicates better what it really is.

what you could do it this:

  • have your group added to the scene empty
  • loop over the objects you want to move in one go,
  • for each of them keep oldParents[i] = objects[i].parent
  • do group.attach(objects[i]);
  • once the group is filled, do group.position.x += 500 and group.updateMatrixWorld()
  • loop over the objects again but this time do oldParents[i].attach(objects[i])

it is not too convenient, which is why nobody does it that way; it seems easier to just calculate the change for each objects[i].position

instances are purely virtual, you can’t control them like you do with normal objects.

most likely the only exception to this is drei/Instances, this abstracts instances so that each instance can be controlled as a single object, which can be grouped, nested, transformed.

in the following demo go to line 25, where it says <group position={[0, 0, 0]}> and change the y coordinate, you’ll see it. with that also the number of items can be controlled, you can add/remove instances runtime (try the slider).

this works because you’re giving <Instances> a rough estimate of how many objects you expect. it will then automatically determine how many exist and set the drawing-range so that it’s not drawing more than needed. probably doesn’t help because this uses react, but maybe it inspires you to find your own solution, certainly it’s possible if that’s the only take away.