Exchange nodes of 3D Model in ARKit












1















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?










share|improve this question



























    1















    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?










    share|improve this question

























      1












      1








      1


      1






      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 18:39









      Jush KillaBJush KillaB

      747




      747
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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:




          1. Get the parent node of the target with parent

          2. Remove the target node with removeFromParentNode()

          3. Add the new child node to the parent with addChildNode(_:)






          share|improve this answer
























          • 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












          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          2














          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:




          1. Get the parent node of the target with parent

          2. Remove the target node with removeFromParentNode()

          3. Add the new child node to the parent with addChildNode(_:)






          share|improve this answer
























          • 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
















          2














          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:




          1. Get the parent node of the target with parent

          2. Remove the target node with removeFromParentNode()

          3. Add the new child node to the parent with addChildNode(_:)






          share|improve this answer
























          • 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














          2












          2








          2







          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:




          1. Get the parent node of the target with parent

          2. Remove the target node with removeFromParentNode()

          3. Add the new child node to the parent with addChildNode(_:)






          share|improve this answer













          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:




          1. Get the parent node of the target with parent

          2. Remove the target node with removeFromParentNode()

          3. Add the new child node to the parent with addChildNode(_:)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks