1

How can I get a name of material that is assigned to my USD mesh?

for mat in importedModel.model!.materials {
    print(mat)
    print(mat as? SimpleMaterial)
}

I'm getting mostly nil when trying to recast it. I want to re-configure all my materials upon import. But I can't do it without knowing what material is what.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Dariusz
  • 960
  • 13
  • 36

1 Answers1

0

The problem seems to be in your loading method. Use Entity.load(named:) instead of Entity.loadModel(named:). To find out what shader is used for each model's part, use this code:

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let usdz = try! Entity.load(named: "car.usdz")
        let anchor = AnchorEntity()
        anchor.addChild(usdz)
        arView.scene.anchors.append(anchor)
        
        print(usdz)          // full scene hierarchy

        let firstAnchor = arView.scene.anchors.first
        let entity = firstAnchor?.findEntity(named: "wheel") as? ModelEntity
        guard let materials = entity?.model?.materials else { return }
        
        for material in materials {
            print(material)
        }
    }
}

Result:

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220