Access child components in USDZ with XCode?

I am fairly new to ios development, and i have a rather simple question (i guess):

I exported an FBX model from blender with attached child elements (some empty axis). I need those transforms for a certain purpose in my app. However, they won't show up when im placing my model entity in the AR Scene and try to access it's child elements. However, they are showing up in the preview view in xcode when i click on the model file.

Can someone please explain why this is and how I can access them? Would be lovely. Thanks for your help in advance!

Answered by artecolote in 708888022

crap. this stupid site removed everything i had typed before i was finished.

anyway. i never used realitykit. but it seems you can access your hierarchy like this

modelEntity.model.mesh.contents.isntances, where instances is a collection (meshInstanceCollection) and you can access the members by string subscript which is the name of the nodes as a path name. for example

modelEntity.model.mesh.contents.isntances["Vase_FP/mod(i can't see this in your screengrab)/FP2"]

or you can iterate through the meshInstanceCollection with its methods mentioned in the documentation. there is also modelEntity.model.mesh.contents.models, but it came up with no visible descriptions on my console so i don't know what it does.

anyway, i hope this gets you closer.

edit: yes you had to go in the nested components, which is .model

are you using a USDZ file or FBX? are you getting any errors?

This is the hierarchy i can see in XCode. I want to access the elements (FP[n] and Pivot when i placed the entity in the scene. however, i cant access them with the child function.

Here are some code snippets on how i place my object in the AR View. I place it by tapping on the screen and handle the tap with an ARView Coorinator. I use the focus entity package to define the anchor point for the model entity. Here, i want to access the information about the child elements, that should be places with the usdz file. As you can see I tried to print out the information, and tried some things. I can't figure out where the children are attached to.

`@objc func handleTap() { guard let view = self.view, let focusEntity = self.focusEntity else {return}

	if (!object.inScene) {
		object.anchorEntity = AnchorEntity()
		view.scene.anchors.append(object.anchorEntity!)
		object.modelEntity = try! ModelEntity.loadModel(named: object.fileName)
		object.modelEntity!.name = object.name

		object.anchorEntity!.position = focusEntity.position
		object.anchorEntity!.addChild(object.modelEntity!)
		
		print(object.modelEntity!.children)
		print(object.modelEntity!.components)

		object.inScene = true
		object.anchorPosition = object.anchorEntity!.transform.translation
		object.anchorRotation = object.anchorEntity!.transform.rotation
	}
	else {return}
}`

The "object" class contains the data i need to use in my whole app, it also tells the coordinator the file name and stores some other info:

` @MainActor class Object: ObservableObject {

let name: String = "Vase"
//let fileName: String = "flower_tulip.usdz"
let fileName: String = "Vase_FP.usdz"
let scale: SIMD3<Float> = SIMD3<Float>(0.1, 0.1, 0.1)

@Published var session: ARSession?

@Published var modelEntity: ModelEntity?
@Published var anchorEntity: AnchorEntity?
@Published var anchorPosition: SIMD3<Float>?
@Published var anchorRotation: simd_quatf?

@Published var deletionRequest: Bool = false
@Published var inScene: Bool = false


func changeObjectPosition(_ sender: Any, newPosition: SIMD3<Float>){
	self.anchorPosition = newPosition
	self.anchorEntity?.transform.translation = newPosition
}

func changeObjectRotation(_ sender: Any, newRotation: simd_quatf){
	self.anchorRotation = newRotation
	self.anchorEntity?.transform.rotation = newRotation
}

func enumerateChildren(_ sender: Any){
	let children = self.modelEntity!.children
	for (n, c) in children.enumerated() {
		print("\(n): '\(c)'")
	}
}

var deletion: Cancellable?

}`

I use scene reconstruction and depth in my ARView, since i work with an iPad Pro. But that does not have to do anything with the question.

I hope things got a bit clearer now

Accepted Answer

crap. this stupid site removed everything i had typed before i was finished.

anyway. i never used realitykit. but it seems you can access your hierarchy like this

modelEntity.model.mesh.contents.isntances, where instances is a collection (meshInstanceCollection) and you can access the members by string subscript which is the name of the nodes as a path name. for example

modelEntity.model.mesh.contents.isntances["Vase_FP/mod(i can't see this in your screengrab)/FP2"]

or you can iterate through the meshInstanceCollection with its methods mentioned in the documentation. there is also modelEntity.model.mesh.contents.models, but it came up with no visible descriptions on my console so i don't know what it does.

anyway, i hope this gets you closer.

edit: yes you had to go in the nested components, which is .model

Well, well.. I finally figured it out! I used ModelEntity in the code above to place the object, which is a subclass of Entity. However, ModelEntity does flatten the Hierarchy and only shows the geometry that is visible and flattens (combines) it to be one and only one instance. Thats why I was only getting SIMD4 for geometry_0. So I went back and loaded the model i want to place as Entity, which inherits from "HasHierarchy" ans "HasTransform". After that, I can just call "Entity.children" and It works just fine and shows all of the child elements as Entities.

What a ride. Hope it might help others that face the problem aswell. :)

Access child components in USDZ with XCode?
 
 
Q