Accessing Children of USDZ Model in SwiftUI

Hi everyone,

I'm developing a visionOS app using SwiftUI and RealityKit. I'm facing a challenge with accessing the children of a USDZ model.

Scenario:

I can successfully load and display a USDZ model in my RealityView. When I import the model into Reality Composer Pro, I can access and manipulate its individual parts (children) using the scene hierarchy. However, if I directly load the USDZ model from the Navigation tab in my project, I cannot seem to access the children programmatically.

Question:

Is there a way to access and manipulate the children of a USDZ model loaded directly from the Navigation tab in SwiftUI for visionOS?

Additional Information:

I've explored using Entity(named: "childName", in: realityKitContentBundle), but this only works for the main entity. I'm open to alternative approaches if directly accessing children isn't feasible.

Thanks in advance for any insights or suggestions!

Best,

Siddharth

[Edited by Moderator]

Answered by Vision Pro Engineer in 788917022

Entity.findEntity(named:) recursively searches all descendant entities for one with the given name. Here's a link to its documentation. Here's a snippet:

if let entity = try? await Entity(named: "ModelName") {
    let childEntity = entity.findEntity(named: "child-name")
    
    // ...
}
Accepted Answer

Entity.findEntity(named:) recursively searches all descendant entities for one with the given name. Here's a link to its documentation. Here's a snippet:

if let entity = try? await Entity(named: "ModelName") {
    let childEntity = entity.findEntity(named: "child-name")
    
    // ...
}

Thank you so much for your prompt and helpful response! The snippet you provided using Entity.findEntity(named:) is exactly what I needed. I appreciate the guidance and the link to the documentation.

Best regards, Siddharth

Accessing Children of USDZ Model in SwiftUI
 
 
Q