Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    FindNodesByName Not Working As Expected

    Cinema 4D SDK
    2023 python
    3
    10
    1.6k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      bentraje
      last edited by

      Hi,

      Based on this thread, variable gets lost when grouping nodes. So I have to reestablish connection to my variables. Hence using the FindNodesByName.

      For some reason, the FindNodesByName gives me an empty list despite the name matching the exact name of the node.

          with graph.BeginTransaction() as transaction:
              scale_node = graph.AddChild("", "net.maxon.node.type", maxon.DataDictionary())
              scale_node.SetValue(maxon.NODE.BASE.NAME, maxon.String("scale_node"))
      
              input_node = []
              maxon.GraphModelHelper.FindNodesByName(graph, "scale_node", maxon.NODE_KIND.ALL_MASK, maxon.PORT_DIR.INPUT, True, input_node)
              print (input_node) # Should give me the Value Node named scale node. Instead Gives me an empty list. Already tried all the PORT_DIR options. Still gives me an empty list. 
      
      
      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by m_adam

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • ManuelM
          Manuel
          last edited by Manuel

          Hi @bentraje,

          I tried your code, and it is working as expected. I tested on 2023.0 and 2023.1

          1532354c-9a06-4cb9-85cd-443e04bd19dc-image.png

          I am using the same code as in the other thread you provided.

          I must emphasis here that searching nodes by name should be used carefully. This is the case where you can use it, as you define the node name. But remember that we can change the Nodes name, and it depends on the language chosen by the user.

          Cheers,
          Manuel

          import c4d
          import maxon
          
          
          def main():
              # Retrieve the selected BaseMaterial
              mat = doc.GetActiveMaterial()
              if mat is None:
                  raise ValueError("There is no selected BaseMaterial")
          
              # Retrieve the reference of the material as a node material.
              nodeMaterial = mat.GetNodeMaterialReference()
              if nodeMaterial is None:
                  raise ValueError("Cannot retrieve node material reference")
          
              # Retrieve the current node space Id
              nodespaceId = c4d.GetActiveNodeSpaceId()
          
              # Retrieve the Nimbus reference for a specific node space
              nimbusRef = mat.GetNimbusRef(nodespaceId)
              if nimbusRef is None:
                  raise ValueError("Cannot retrieve the nimbus ref for that node space")
          
              # Retrieve the graph corresponding to that node space.
              graph = nimbusRef.GetGraph()
              if graph is None:
                  raise ValueError("Cannot retrieve the graph of this nimbus ref")
          
              # Get the root of the GraphNode
              root = graph.GetRoot()
          
              # Retrieve all nodes, child of the root node
              nodes = []
              root.GetChildren(nodes, maxon.NODE_KIND.NODE)
          
              # Create a list of the selected ones.
              selectedNodes = []
              maxon.GraphModelHelper.GetSelectedNodes(graph, maxon.NODE_KIND.NODE, selectedNodes)
           
              # Group all the selected nodes in an empty node.
              groupRoot = maxon.GraphNode()
          
              # To modify a graph, modification must be done inside a transaction. After
              # modifications are done, the transaction must be committed.
              with graph.BeginTransaction() as transaction:
                  scale_node = graph.AddChild("", "net.maxon.node.type", maxon.DataDictionary())
                  scale_node.SetValue(maxon.NODE.BASE.NAME, maxon.String("scale_node"))
          
                  input_node = []
                  maxon.GraphModelHelper.FindNodesByName(graph, "scale_node", maxon.NODE_KIND.ALL_MASK, maxon.PORT_DIR.INPUT, True, input_node)
                  print (input_node) # Should give me the Value Node named scale node. Instead Gives me an empty list. Already tried all the PORT_DIR options. Still gives me an empty list. 
                  #groupRoot = graph.MoveToGroup(groupRoot, maxon.Id("idOfMyGroup"), selectedNodes)
                  #maxon.GraphModelHelper.CreateInputPort(groupRoot, "someid", "new port name")
                  transaction.Commit()
          
              # Pushes an update event to Cinema 4D
              c4d.EventAdd()
          
          
          if __name__ == "__main__":
              main()
          

          MAXON SDK Specialist

          MAXON Registered Developer

          B 1 Reply Last reply Reply Quote 0
          • B
            bentraje @Manuel
            last edited by

            @manuel

            Uhm not sure if its bug but it does work then in some cases it doesn't, even though there are no other variables changed.

            as proof, see the illustration below.
            works for four times but on the fifth try it gives me a black list. and I didn't changed anything.

            2023-01-20_23-22-17.gif

            1 Reply Last reply Reply Quote 0
            • ManuelM
              Manuel
              last edited by

              i confirm that sometimes the function does not find the node. i need to investigate a bit more what is happening here.

              Cheers,
              Manuel

              MAXON SDK Specialist

              MAXON Registered Developer

              1 Reply Last reply Reply Quote 1
              • B
                bentraje
                last edited by

                Hope all is well.

                Just wondering if there is a fix on this on this.
                Thanks.

                1 Reply Last reply Reply Quote 0
                • ManuelM
                  Manuel
                  last edited by Manuel

                  Hi,

                  sorry, we have a company meeting next week, i will not be able to check again this issue until 15 of May.
                  many things were fixed in 2023.2.

                  Cheers,
                  Manuel

                  MAXON SDK Specialist

                  MAXON Registered Developer

                  1 Reply Last reply Reply Quote 1
                  • ManuelM
                    Manuel
                    last edited by

                    Hi,
                    I tested the following code and while i got some error on a Redshift material in 2023.0 and 2023.1 i have no error anymore in 2023.2.1

                    Standard renderer seems to always works. So this seems to not be an issue with the function FindNodesByName itself.

                    There were lots of improvement in the node API and i can just guess what have been fixed.
                    Can you still reproduce the issue in 2023.2.1 ?

                    import c4d
                    import maxon
                    import time
                    
                    
                    def main():
                        # Retrieve the selected BaseMaterial
                        mat = doc.GetActiveMaterial()
                        if mat is None:
                            raise ValueError("There is no selected BaseMaterial")
                    
                        # Retrieve the reference of the material as a node material.
                        nodeMaterial = mat.GetNodeMaterialReference()
                        if nodeMaterial is None:
                            raise ValueError("Cannot retrieve node material reference")
                    
                        # Retrieve the current node space Id
                        nodespaceId = c4d.GetActiveNodeSpaceId()
                    
                        # Retrieve the Nimbus reference for a specific node space
                        nimbusRef = mat.GetNimbusRef(nodespaceId)
                        if nimbusRef is None:
                            raise ValueError("Cannot retrieve the nimbus ref for that node space")
                    
                        # Retrieve the graph corresponding to that node space.
                        graph = nimbusRef.GetGraph()
                        if graph is None:
                            raise ValueError("Cannot retrieve the graph of this nimbus ref")
                    
                        settings: maxon.DataDictionaryInterface = maxon.DataDictionary()
                        settings.Set(maxon.nodes.UndoMode, maxon.nodes.UNDO_MODE.START)
                        
                        for loopId in range(0, 100):
                    
                            with graph.BeginTransaction(settings) as transaction:
                                input_node = []
                                scale_node = graph.AddChild("", "net.maxon.node.type", maxon.DataDictionary())
                                scale_node.SetValue(maxon.NODE.BASE.NAME, maxon.String("scale_node"))
                                maxon.GraphModelHelper.FindNodesByName(graph, "scale_node", maxon.NODE_KIND.ALL_MASK, maxon.PORT_DIR.INPUT, True, input_node)
                                print (input_node)
                                if len(input_node) < 1:
                                    print("cannot find the node")
                                transaction.Commit()
                                
                            with graph.BeginTransaction(settings) as transaction:
                                input_node = []
                                maxon.GraphModelHelper.FindNodesByName(graph, "scale_node", maxon.NODE_KIND.ALL_MASK, maxon.PORT_DIR.INPUT, True, input_node)
                                for graphnode in input_node:
                                    graphnode.Remove()
                                transaction.Commit()
                        # Pushes an update event to Cinema 4D
                        c4d.EventAdd()
                    
                    
                    if __name__ == "__main__":
                        main()
                    

                    Cheers,
                    Manuel

                    MAXON SDK Specialist

                    MAXON Registered Developer

                    1 Reply Last reply Reply Quote 0
                    • M
                      m_adam
                      last edited by

                      Hello @bentraje,

                      without further questions or postings, we will consider this topic as solved by Friday 02/06/2023 and flag it accordingly.

                      Thank you for your understanding,
                      Maxime.

                      1 Reply Last reply Reply Quote 0
                      • B
                        bentraje
                        last edited by bentraje

                        @m_adam

                        slr. can confirm the FindNodesByName now works as expected on the illustration code I used previously.

                        Thanks

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post