---
title: Load a model
summary: Resolve a version, load it through the ModelManager, load a second model and frame it, and replace what is loaded.
order: 20
languages: [javascript]
terms: [version, node, model-manager]
see-also: [tutorials/javascript-viewer/embed-the-viewer, tutorials/javascript-viewer/select-and-colour, how-to/models/list-the-tree, explanation/identifiers]
---

# Load a model

A model in BIM.works is a [[term:version]] of a [[term:node]]; the viewer loads versions. From this lesson on the rounds start with what the first lesson built: a `client`, a `modelManager`, an `objectStateManager` and an empty `viewer` on the page. Your code loads the models.

## One model

The version UUID of the first demo model, `BIM_WORKS_FIRST_MODEL`, stands in for the model you want to show. Two things are easy to miss:

- **The ModelManager wants a complete `Version`**, one that carries `im_metadata`, the description of the processed geometry. `client.fs.getNodeVersion(uuid)` (or `getNodeVersions([…])` for several) always returns one, and so does the `lastVersion` of a node returned by `listNodes` or `getNodeByPath`; the version summaries in tree change notifications do not, and a version that is still being processed has no `im_metadata` yet. Passing an incomplete version to `addModels` throws `IncompleteVersionError`; when in doubt, resolve the UUID.
- **`addModels` resolves when the model is fully loaded**, not when loading starts. Code after the `await` can rely on every object being present; a fit-to-view or a colour change before that point would apply to an empty scene.

```lesson {#lesson-1}
goal: Load the first demo model into the viewer.
language: javascript
setup: viewer
check: models
starter: |
  // BIM_WORKS_FIRST_MODEL is the version UUID of the first demo model
  const uuid = BIM_WORKS_FIRST_MODEL;
hints: |
  - The ModelManager loads `Version` objects, not UUIDs: `await client.fs.getNodeVersion(uuid)` resolves one.
  - `await modelManager.addModels([version])` loads it into every viewer connected to the ObjectStateManager.
solution: |
  const version = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
  await modelManager.addModels([version]);
accept: |
  - const [version] = await client.fs.getNodeVersions([BIM_WORKS_FIRST_MODEL]);
    await modelManager.setModels([version]);
```

The progress handler passed as the viewer's second argument is an object whose methods are all optional: `versionLoadProgress(version, percentage)` fires while geometry streams in, `versionLoaded(version)` once at the end and `versionUnloaded(version)` when a model is removed. The viewer in the rounds has one that writes to its status line.

## Finding a model to load

The UUID in the rounds comes from the page; in an application it comes from the tree. `client.fs.getRootNodeUuid()` is the node your token was created on, `client.fs.listNodes(uuid)` its children, and `client.fs.getNodeByPath("/Demo/Schependomlaan/IFC Schependomlaan.ifc")` a node by its path relative to that root. A node that holds a model has `last_version_uuid`, which is what you resolve and load. [[page:how-to/models/list-the-tree]] shows the whole tree walk; [[page:explanation/identifiers]] explains why models are addressed by version and objects by [[term:qid]].

## Two models, and the camera

`modelManager.addModels([a, b])` loads several models at once. In the default `LOCAL` mode every model sits on its own coordinates, so unrelated models can overlap or lie far apart, and after loading the camera frames whatever it framed before. Two calls fit the camera: `objectStateManager.viewFitModels(versionUuids, {animate: true})` frames whole models, `objectStateManager.viewFit(qids, {animate: true})` frames a set of objects (lesson four). Both go through the ObjectStateManager so that every connected viewer moves.

```lesson {#lesson-2}
goal: Load both demo models, and frame the second one.
language: javascript
setup: viewer
check: models, fit
starter: |
  const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
  await modelManager.addModels([first]);
hints: |
  - `BIM_WORKS_SECOND_MODEL` is the version UUID of the second demo model; it is resolved like the first.
  - `addModels` takes a list: both versions can go in one call.
  - `objectStateManager.viewFitModels([second.uuid], {animate: true})` frames one model; it takes version UUIDs.
solution: |
  const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
  const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
  await modelManager.addModels([first, second]);
  await objectStateManager.viewFitModels([second.uuid], {animate: true});
accept: |
  - const [first, second] = await client.fs.getNodeVersions([BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL]);
    await modelManager.addModels([first, second]);
    await objectStateManager.viewFitModels([second.uuid], {animate: true});
  - const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
    await modelManager.addModels([first]);
    const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
    await modelManager.addModels([second]);
    await objectStateManager.viewFitModels([second.uuid]);
```

```tip
Models that carry a geo reference can be shown at their real relative positions with `viewer.setMode("GEO")`. The call returns `false` when a loaded model has no geo reference; `LOCAL` is the default and always available.
```

## Replacing what is loaded

`modelManager.removeModels([version])` unloads a model, and `modelManager.setModels([...])` replaces the whole set: it loads what is missing and unloads what is no longer listed. Always go through the ModelManager, never through the viewer directly: the ModelManager is what knows which versions are loaded, and a version it still considers loaded is not loaded again.

The next round starts with the first demo model loaded; `version` is its `Version`.

```lesson {#lesson-3}
goal: Show the second demo model instead of the first.
language: javascript
setup: model
check: models
starter: |
  // version is the first demo model, loaded
  const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
  await modelManager.addModels([second]);
hints: |
  - Adding the second model leaves the first one loaded.
  - `modelManager.removeModels([version])` unloads the first model.
  - `modelManager.setModels([second])` does both in one call.
solution: |
  const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
  await modelManager.setModels([second]);
accept: |
  - const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
    await modelManager.removeModels([version]);
    await modelManager.addModels([second]);
```

## The whole page

The rounds as one program of your own, with a button that unloads and loads the second model again:

```example
name: viewer-two-models
code: javascript/viewer-two-models.js
run: none
css: javascript/viewer-two-models.css
notes: |
  Both demo models load into the same viewer and the camera frames the first. The first button removes and re-adds the second model through the ModelManager, the second frames the first model again.
highlight: 15-19,22-31
```

## You can now

- Resolve a version UUID to a `Version` and load it.
- Know when loading is complete, and follow its progress.
- Walk the tree to find a model's last version.
- Load several models, frame one of them, and replace what is loaded.

Next: [Select and colour](select-and-colour.md).
