Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

551
Visualizações
RealityKit – Get a model entity from USDZ file

I haave a file (exists in main bundle with target membership checked) named matrix.usdz and need to load it with

 do {
    let path = Bundle.main.path(forResource: "Matrix", ofType: "usdz")!
  
    let url = URL(fileURLWithPath: path)
        
    let assetsLoader = try Entity.load(contentsOf: url)
  }
  catch {
     print(error)
  }

But it crashes with

Thread 1: signal SIGABRT

on this line

let assetsLoader = try Entity.load(contentsOf: url)

Preview

enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You have to create an anchor if you need to load an entity into your scene. In order to get a ModelEntity, you need to grab it from the scene hierarchy using .children[X] subscript.

import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()            
        do {
            let path = Bundle.main.path(forResource: "Matrix", ofType: "usdz")!
         
            let url = URL(fileURLWithPath: path)
               
            // Scene
            let scene = try Entity.load(contentsOf: url)          
            print(scene)
            
            // Entity
            let entity = scene.children[0].........children[0] as! ModelEntity
            
            entity.model?.materials[0] = UnlitMaterial(color: .red)
            
            let anchor = AnchorEntity(plane: .any)
            anchor.addChild(scene)
            arView.scene.anchors.append(anchor)

         } catch {
             print(error)
         }
    }
}

You can also get a model this way:

let modelEntity = try Entity.loadModel(contentsOf: url)
                    
modelEntity.model?.materials[0] = UnlitMaterial(color: .red)


P.S.

I should say that you have an obvious naming error – "Matrix" vs "matrix". Also Matrix.rcproject and Matrix.usdz are not the same. To load Matrix.rcproject (Reality Composer project) use the following approach:

enter image description here

// .rcproject
let scene = try! Matrix.loadCircle()

let circleEntity = scene.children[0]...........children[0] as! ModelEntity

to load a USDZ model use this one:

// .usdz
let model = try! Entity.loadModel(named: "Matrix", in: nil)

But as far as I know, you do not need an RC project, so export USDZ from Reality Composer.

enter image description here

To load .reality file use the following approach:

// .reality
let carModel = try! Entity.loadAnchor(named: "car")
print(carModel)

arView.scene.addAnchor(carModel)


Here's your USDZ model on iOS simulator:

enter image description here

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda