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
    • Register
    • Login

    Setting the name of a maxon.GraphNode via Python

    Cinema 4D SDK
    2023 python
    2
    3
    385
    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.
    • PoliigonP
      Poliigon
      last edited by

      Hi,
      I happily admit, I suck with the maxon node interface.
      Still, I achieved most of what I needed. But what seemed to me like the most trivial part, I seem not to be able to accomplish.
      I would like to set the name of a (true) maxon.GraphNode (similar to the user clicking the node title and renaming it).
      I found this attribute maxon.NODE.BASE.NAME, but I'm not really sure, where to use it.

      Any help would be much appreciated.
      Thanks in advance,
      Andreas

      Poliigon Software Engineering

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

        Hey Andreas, hope everything is fine on your end ! 🙂

        Regarding name you can achieve that by calling GraphNode.SetValue with maxon.NODE.BASE.NAME.
        Find bellow an example that set the name of all the selected nodes to "New Name"

        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()
        
            # Get all selected node
            # 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:
                for selectedNode in maxon.GraphModelHelper.GetSelectedNodes(graph, maxon.NODE_KIND.NODE):
                    
                    # There is currently an issue affecting some datatype like string, Id, InternedId
                    # So when you set such a datatype it is mandatory to copy the value first to a new variable since SetValue will move the data
                    # Do no use stringValue after SetValue have been called, its data may be gone due to the issue
                    stringValue = maxon.String("New Name")
                    selectedNode.SetValue(maxon.NODE.BASE.NAME, stringValue)
                    print(selectedNode)    
                    
                transaction.Commit()  
                
        
            # Pushes an update event to Cinema 4D
            c4d.EventAdd()
        
        
        if __name__ == "__main__":
            main()
        

        I would also really advice you to take a look at Open source wrapper for Octane/Redshift/Arnold in Cinema 4D you will probably find it handy and is also a great resource to learn the nodes API!

        Have a nice weekend.
        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 1
        • PoliigonP
          Poliigon
          last edited by

          Hey Maxime,
          thanks for the quick answer.
          Actually I thought, I had tried SetValue() with that ID. But either I did something wrong or I tried something else... anyway, thanks.
          Just checked: Yes, I did something wrong. I stupidly just passed a string instead of a maxon.String. With the latter it works, not that I doubted your proposal.

          Maybe would be good, if SetValue() could throw an error, if a wrong type gets passed instead of just doing nothing.

          Also thanks for the pointer to Dunhou's project, I was already aware. Nice project! And indeed I peeked into it regarding some Arnold peculiarities...

          Cheers,
          Andreas

          Poliigon Software Engineering

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