Request

Response

    Version 8 preview
    This page documents a preview of the Viewer SDK v8. APIs and content can still change before the final release.

    Wrapping Loaded Instances

    This example shows how to interact with loaded models and customize the rendering of loaded objects. When a model is loaded from the cloud, its instances already exist in the InstanceCollection3D.

    We show how to use the ObjectTree (via Model.getObjectTreeAsync) to map objects (identified by dbIds) to instances and change their position.

    Instead of directly using the InstanceCollection3D interfaces, we show how to wrap existing objects in scene graph nodes with InstanceNode3D.fromInstance(instanceId, parent) and move them as one transformation group. This gives you the full transform hierarchy of the scene-graph API over geometry that was loaded, not authored in code.

    Understanding the object-to-instance direction is essential. A dbId is an object id — it identifies a logical component in the object tree (think: a door, a room, a structural column). One object can have multiple instances, each with its own geometry, material, and transform stored in the InstanceCollection3D. enumNodeInstances(node, callback) iterates those instances, supplying each instance id to the callback. The pattern is: object tree → dbId → enumNodeInstances → instance ids → InstanceNode3D.fromInstance().

    Source

    Gather instance ids from dbIds

    The snippet below collects only instances that have geometry.

    const targetDbIds = [
      260, 261, 262, 263, 264, 265,
      266, 267, 268, 269, 270, 271,
    ];
    
    const objectTree = await model.getObjectTreeAsync();
    
    const instanceIds = [];
    for (let i = 0; i < targetDbIds.length; i++) {
      objectTree.enumNodeInstances(
        targetDbIds[i],
        (instanceId) => {
          if (instances.getGeometry(instanceId)) {
            instanceIds.push(instanceId);
          }
        },
        true
      );
    }
    
    Show More

    Wrap instances in scene graph nodes

    Instances are wrapped in a scene graph node and grouped under a single Node3D.

    const root = instances.getSceneGraph();
    const group = new avs.Node3D();
    root.add(group);
    
    // Wrap the selected instances into the group
    const nodes = [];
    for (let j = 0; j < instanceIds.length; j++) {
      const node = avs.InstanceNode3D.fromInstance(
        instanceIds[j], group
      );
      nodes.push(node);
    }
    
    Show More

    Update positions of all instances

    The group of wrapped instances can be moved in one call.

    const togglePosition = () => {
      // Move the group — all wrapped instances follow
      if (group.position.equals(originalPosition)) {
        group.position.y -= 40;
        group.position.z -= 4;
      } else {
        group.position.copy(originalPosition);
      }
      group.updateTransformWorld(true);
      viewer.refresh(true);
      setTimeout(togglePosition, 1000);
    };
    
    togglePosition();
    
    Show More