I have a model entity (from Reality Composer Pro) I want to change the material of the model entity inside swift. The material is also imported in reality composer pro. I am copying the USDZ file of the material in the same directory as the script.
This is the code I am using to reference the Material.
do {
// Load the file data
if let materialURL = Bundle.main.url(forResource: "BlackABSPlastic", withExtension: "usdz") {
let materialData = try Data(contentsOf: materialURL)
// Check the first few bytes of the data to see if it matches expected types
let headerBytes = materialData.prefix(4)
let headerString = String(decoding: headerBytes, as: UTF8.self)
// Print out the header information for debugging
print("File header: \(headerString)")
// Attempt to load the ShaderGraphMaterial
let ScratchedMetallicPaint = try await ShaderGraphMaterial(
named: "BlackABSPlastic",
from: materialData
)
print(ScratchedMetallicPaint)
} else {
print("BlackABSPlastic.usdz file not found.")
}
} catch {
// Catch the error and print it
print("BlackABSPlastic load failed: \(error)")
// Attempt to infer file type based on the error or file content
if let error = error as? DecodingError {
switch error {
case .typeMismatch(let type, _):
print("Type mismatch: Expected \(type)")
case .dataCorrupted(let context):
print("Data corrupted: \(context.debugDescription)")
default:
print("Decoding error: \(error)")
}
} else {
print("Unexpected error: \(error)")
}
}
I am receiving these errors:
File header: PK
TBB Global TLS count is not == 1, instead it is: 2
Unable to create stage from in-memory buffer.
BlackABSPlastic load failed: internalImportError
Unexpected error: internalImportError
am I doing anything wrong? I am able to access the materials of the model entity easily but this seems to something different. How can this be resolved?
Thanks.
Hello @snehdev
Your code looks mostly correct to me, however maybe something is not right about the named:
parameter?
For example, if you create a new vision os app in Xcode from the default App template, a GridMaterial is created inside a bundle called realityKitContentBundle
. That material can be loaded at runtime with the following Swift code:
let gridMaterial = try await ShaderGraphMaterial(named:"/Root/GridMaterial", from: "Materials/GridMaterial", in: realityKitContentBundle)
If you open GridMaterial.usda
in Reality Composer Pro, you'll see that "/Root/GridMaterial"
matches the structure shown in the hierarchy browser. "Materials/GridMaterials"
matches the location of the file in the bundle, and omits the extension.
Good luck! Let me know if you have any questions.