Accessing SCNSphere Properties

183 Views Asked by At

I am trying to access / modify the properties of an SCNSphere that I have in a SCNScene. I have the scene pre-set as a file called "spaceScene.scn". I am loading the scene as such

self.sceneView.scene = [SCNScene sceneNamed:@"spaceScene.scn"];
self.sceneView.allowsCameraControl = YES;
self.sceneView.scene.rootNode.camera = [SCNCamera camera];
SCNSphere *earth = (SCNSphere *)[self.sceneView.scene.rootNode childNodeWithName:@"earth" recursively:NO];

NSMutableArray *materials = earth.materials;
NSLog(@"Materials of earth from scene: %@", materials);

I can not seem to get past reading the materials property of the SCNSphere earth. I keep getting an instance error:

-[SCNNode materials]: unrecognized selector sent to instance 0x1701c5550

Feeling a little silly with this issue but please someone just tell me how to access the sphere properties? thanks

2

There are 2 best solutions below

0
On BEST ANSWER

SCNSphere does not inherit from SCNNode. You should rather retrieve the node's geometry which can be a sphere.

1
On

You are casting SCNNode to SCNSphere when you are creating your earth object.

If you take a look at the documentation, the function you are using is returning SCNNode.

- (SCNNode *)childNodeWithName:(NSString *)name 
                   recursively:(BOOL)recursively;

With the casting, you can fake that the object is SCNSphere, however, it is not. And when you are sending the materials message to the object, it crashes, because it is an unrecognized selector on SCNNode.

I would recommend not to force cast, and look for an other way to retrieve you object.