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!

Accepted Reply

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

  • Many thanks! I thought about going down that road, but it seemed kinda weird. :D But it makes total sense when i think about it.

    I tried it and it debugs:

    Instance(id: "/Vase_FP/model/model_geometry-0", model: "/Vase_FP/model/model_geometry", transform: simd_float4x4([[100.0, 0.0, 0.0, 0.0], [0.0, -1.1920929e-05, -99.99999, 0.0], [0.0, 99.99999, -1.1920929e-05, 0.0], [2.0692042e-07, 0.0, -1.0926352e-06, 1.0]]))

    which seems like some sort of vector 4 location / transform which is great :D i just need to find out more about them and how to use this info, but i guess this helps me with figuring it out. Weird thing is it only seems to show geometry-0, which is my main object. But i'll work on it further. Thanks for your help so far, mate!

Add a Comment

Replies

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

  • I exported from blender to fbx with all of the children attached. Then I used the Reality Converter to convert it into usdz. I am not getting any errors, I can place the usdz model in the scene. When i try to access the child elements with modelEntity.children to get a child collection, it doesn't seem to have any child elements.

    However, when i preview the usdz file in xcode, i can see the empty axis in the preview. If you give me a moment, i can screenshot it. :)

Add a Comment

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.

  • well, what does your code look like?

Add a Comment

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

  • the output gets me this:

    ComponentSet(entity: ▿ 'Vase' : ModelEntity   ⟐ Transform   ⟐ SynchronizationComponent   ⟐ ModelComponent )

    Maybe I need to search further in the nested components, but I tried and wasn't really successfull ..

Add a Comment

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

  • Many thanks! I thought about going down that road, but it seemed kinda weird. :D But it makes total sense when i think about it.

    I tried it and it debugs:

    Instance(id: "/Vase_FP/model/model_geometry-0", model: "/Vase_FP/model/model_geometry", transform: simd_float4x4([[100.0, 0.0, 0.0, 0.0], [0.0, -1.1920929e-05, -99.99999, 0.0], [0.0, 99.99999, -1.1920929e-05, 0.0], [2.0692042e-07, 0.0, -1.0926352e-06, 1.0]]))

    which seems like some sort of vector 4 location / transform which is great :D i just need to find out more about them and how to use this info, but i guess this helps me with figuring it out. Weird thing is it only seems to show geometry-0, which is my main object. But i'll work on it further. Thanks for your help so far, mate!

Add a Comment

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. :)

  • good to know. i didn't look other methods of importing, just the one you used which the documentation didn't explain much other than "it blocks your application while importing"

    i haven't gotten into reality kit, but this will be useful when/if i do. the documentation seems to have a lot of holes (like usual)

Add a Comment