Exchange nodes of 3D Model in ARKit
I have a car model. This model contains a lot of groups (doors, tires, windows, body). I would like to change the model of the tires by button press. But I have trouble finding the correct node.
My current idea was to search through all the child nodes of the car and when I find the tires, replace them.
My code:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor is ARImageAnchor else { return }
guard let carNode = sceneView.scene.rootNode.childNode(withName: "Car", recursively: false) else { return }
node.addChildNode(carNode)
@IBAction func tireChangePressed(_ sender: UIButton) {
var exchangeTires = sceneView.scene.rootNode.childNode(withName: "Tires 2")
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "Tires"{
// here I try to exchange the node namend "Tires" with the optional node named "Tires 2"
node = exchangeTires
}
}
}
But it keeps throwing the error that node is a let constant. Should I use other method to iterate through all the child nodes or what could be the problem?
swift parent-child arkit ios12
add a comment |
I have a car model. This model contains a lot of groups (doors, tires, windows, body). I would like to change the model of the tires by button press. But I have trouble finding the correct node.
My current idea was to search through all the child nodes of the car and when I find the tires, replace them.
My code:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor is ARImageAnchor else { return }
guard let carNode = sceneView.scene.rootNode.childNode(withName: "Car", recursively: false) else { return }
node.addChildNode(carNode)
@IBAction func tireChangePressed(_ sender: UIButton) {
var exchangeTires = sceneView.scene.rootNode.childNode(withName: "Tires 2")
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "Tires"{
// here I try to exchange the node namend "Tires" with the optional node named "Tires 2"
node = exchangeTires
}
}
}
But it keeps throwing the error that node is a let constant. Should I use other method to iterate through all the child nodes or what could be the problem?
swift parent-child arkit ios12
add a comment |
I have a car model. This model contains a lot of groups (doors, tires, windows, body). I would like to change the model of the tires by button press. But I have trouble finding the correct node.
My current idea was to search through all the child nodes of the car and when I find the tires, replace them.
My code:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor is ARImageAnchor else { return }
guard let carNode = sceneView.scene.rootNode.childNode(withName: "Car", recursively: false) else { return }
node.addChildNode(carNode)
@IBAction func tireChangePressed(_ sender: UIButton) {
var exchangeTires = sceneView.scene.rootNode.childNode(withName: "Tires 2")
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "Tires"{
// here I try to exchange the node namend "Tires" with the optional node named "Tires 2"
node = exchangeTires
}
}
}
But it keeps throwing the error that node is a let constant. Should I use other method to iterate through all the child nodes or what could be the problem?
swift parent-child arkit ios12
I have a car model. This model contains a lot of groups (doors, tires, windows, body). I would like to change the model of the tires by button press. But I have trouble finding the correct node.
My current idea was to search through all the child nodes of the car and when I find the tires, replace them.
My code:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard anchor is ARImageAnchor else { return }
guard let carNode = sceneView.scene.rootNode.childNode(withName: "Car", recursively: false) else { return }
node.addChildNode(carNode)
@IBAction func tireChangePressed(_ sender: UIButton) {
var exchangeTires = sceneView.scene.rootNode.childNode(withName: "Tires 2")
self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if node.name == "Tires"{
// here I try to exchange the node namend "Tires" with the optional node named "Tires 2"
node = exchangeTires
}
}
}
But it keeps throwing the error that node is a let constant. Should I use other method to iterate through all the child nodes or what could be the problem?
swift parent-child arkit ios12
swift parent-child arkit ios12
asked Nov 28 '18 at 18:39
Jush KillaBJush KillaB
747
747
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
SCNNode and the node tree work analogously to UIView and the view tree, so instead of trying to set the existing node equal to something else you should:
- Get the parent node of the target with parent
- Remove the target node with removeFromParentNode()
- Add the new child node to the parent with addChildNode(_:)
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526030%2fexchange-nodes-of-3d-model-in-arkit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
SCNNode and the node tree work analogously to UIView and the view tree, so instead of trying to set the existing node equal to something else you should:
- Get the parent node of the target with parent
- Remove the target node with removeFromParentNode()
- Add the new child node to the parent with addChildNode(_:)
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
add a comment |
SCNNode and the node tree work analogously to UIView and the view tree, so instead of trying to set the existing node equal to something else you should:
- Get the parent node of the target with parent
- Remove the target node with removeFromParentNode()
- Add the new child node to the parent with addChildNode(_:)
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
add a comment |
SCNNode and the node tree work analogously to UIView and the view tree, so instead of trying to set the existing node equal to something else you should:
- Get the parent node of the target with parent
- Remove the target node with removeFromParentNode()
- Add the new child node to the parent with addChildNode(_:)
SCNNode and the node tree work analogously to UIView and the view tree, so instead of trying to set the existing node equal to something else you should:
- Get the parent node of the target with parent
- Remove the target node with removeFromParentNode()
- Add the new child node to the parent with addChildNode(_:)
answered Nov 28 '18 at 19:04
Josh HomannJosh Homann
9,01311421
9,01311421
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
add a comment |
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
Going to try this in a few! Instructions are clear so far but I did not understand what you meant by pointing out that SCNNode and node tree work analogously to UIView and view tree. Could you explain please?
– Jush KillaB
Nov 28 '18 at 19:19
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
You use the same pattern in CoreGraphics, UIKit, SceneKit and SpriteKit. To remove a node you tell it to remove itself. to add a node you call addChild on the parent. They are all Apple API's and share many common design patterns so if you have worked with one, working with the others should be familiar.
– Josh Homann
Nov 28 '18 at 19:22
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
Oh ok got it. Thanks so far. But how does the parent node know, that it should put the new child node exactly on the position the removed node was? In my example: When I delete the old tires, I would like to add the new tires to the exact same place afterwards.
– Jush KillaB
Nov 28 '18 at 19:24
1
1
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
In the case of SCNNode the children are inherently unordered, so as long as its a child of the same parent its effectively in the same place. For other things, like UIView or CALayer where the order matters they have insert:before: and insert:after: methods.
– Josh Homann
Nov 28 '18 at 19:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53526030%2fexchange-nodes-of-3d-model-in-arkit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown