Is it possible to change usdz objects inside a scene programmatically?

Let's say I've created a scene with 3 models inside side by side. Now upon user interaction, I'd like to change these models to another model (that is also in the same reality composer pro project). Is that possible? How can one do that?

One way I can think of is to just load all the individual models in RealityView and then just toggle the opacity to show/hide the models. But this doesn't seem like the right way for performance/memory reasons.

How do you swap in and out usdz models?

Replies

You can create parent nodes for your different models in your Reality Composer Pro project and toggle them on/off. Create a root entity in your Reality Composer Pro project and give it a name. For the sake of this example let's use MyRootEntity. You can then assign this to a variable in Swift:

var rootEntity = try? await Entity(named: "MyRootEntity")

You can then create child a node for each one of your models under this root entity and query them by name. Let's assume your nodes are named Model1, Model2 and Model3. Export your completed project to a usdz file that is loaded in your project. You can query for individual entities in that hierarchy and toggle their enablement:

func setEntityVisibility(name: String, visible: Bool, root: Entity) {

    guard let entity = root.findEntity(named: name) else {
        return
    }
    entity.isEnabled = visible
}

// Enable Model1
setEntityVisibility(name: "Model1", visible: true, root: rootEntity)