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

    Paste JSON Values on the Node Network?

    Cinema 4D SDK
    r25 python
    3
    7
    977
    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,

      Recently learned that you can copy a node network (not just a node but a network) and paste it to a text editor, and it shows a JSON format.

      So I wondering if its possible to retrieve this JSON format offdisk and load (i.e. paste it into the network) and consequently have the node network ready to go.

      Performing a lot of AddChild and Connect could work but using the already tried and proven network from the JSON file is much more easier and easier to debug.

      Is there a way around this?

      iplaiI 1 Reply Last reply Reply Quote 0
      • iplaiI
        iplai @bentraje
        last edited by iplai

        @bentraje
        I wrote the c4djson module to do what you described for ordinary c4d objects, but not for xpresso and node network(Neutron and node material). If it's impletmented, the json format maybe like this:

        {
            "Node Graph Root": {
                "Node Identifier 1": {
                    "Attr1": "Value1",
                    "Attr2": "Value2",
                    ...
                    "In Ports": {
                        "Port 1": {},
                        "Port 2": {},
                        ...
                    },
                    "Out Ports": {
                        "Port 1": {},
                        "Port 2": {},
                        ...
                    },
                },
                "Node Identifier 2": {...},
                ...
                "Connections": [ (OutPort, InPort), (OutPort, InPort), ... ]
            }
        }
        

        There's not a decent way for the parser to define or find the ports. And some parameters dynamicly occur when connections made. So, the sequence of node creating parameters setting and ports connecting must be carefully handled.Then too much more information is needed for node system, making things more complex.This goes against my original vision. From a certain point of view, the node system is essentially a kind of programming language, using json to serialize it seems stupid and superfluous.

        C4DJSON -- a useful module to load an dump c4d objects in python dict format (similar to standard python json module).
        Examples on Notion.

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

          Thanks for the response. @iplai

          your solution is definitely the ideal one and would have probably use it as a basis to create a node creation-json parser.
          but at this time, I'm a bit pressed of time so I'm not any leisure to explore 😞

          I was hoping there is a load json method like hidden somewhere.
          I mean the likehood that there is already an existing parser is high since you can copy and paste json already directly to the node editor, out of the box.

          iplaiI 1 Reply Last reply Reply Quote 0
          • ferdinandF
            ferdinand
            last edited by ferdinand

            Hello @bentraje,

            Thank you for reaching out to us. What you want to do is not possible in the Python API, and at least not trivial in the C++ API.

            Background

            What you see there when you copy and paste the nodes, is the JSON serialization data format of the maxon API. It works similar to popular abstracted data type serialization systems, as for example the attribute driven XML serialization of C#. I.e., it can serialize almost any data structure and its values, not just nodes. The closest related concept in the classic API would be Resources, although they could only be used to deserialize GUIs and did not store any values (apart from strings), i.e., just stored the structure.

            Realized is this with the interface contract DescribeIo, with which data can both be serialized (written from memory to disk) and deserialized (the other way). DescribeIo is a fairly low-level concept of the maxon C++ API. The more high-level concept is DataDescriptionInterface. While some parts of this have been wrapped for Python, it has not been done to the extent required here. Added to this is that neither GraphModelInterface nor GraphNode are such DataDescriptionInterface and are instead serialized over NodeTemplateInterface. So, in the end, you cannot instantiate a GraphNode directly from a JSON file, even in C++. NodeTemplateInterface has not been wrapped for Python, but I showed in the Asset API C++ docs how to create and load node assets. The loading part can be done in Python, the creating part not, due to some methods not being wrapped. But this all works over the serialized data stored in asset databases, not the raw JSON, but what a node template effectively stores is such JSON data.

            What you can do

            Not much, unfortunately, direct node JSON serialization and deserialization is neither supported in the public C++ nor public Python API. You can of course try to start deserializing data yourself with the json module of Python, but we cannot provide any help with that.

            The most realistic thing would be a request for us to support NodeTemplateInterface in Python, so that you can load and store node setups programatically in a dummy or proper user database.

            Cheers,
            Ferdinand

            MAXON SDK Specialist
            developers.maxon.net

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

              Hi @ferdinand

              Thanks for the confirmation.
              No worries found a hack way to implement through python keyboard simulation

              See illustration below. I can't upload MP4 so just dropbox link.
              dl.dropbox.com/s/pp0lq208etyvix0/c4d312_load_json_c4d.mp4

              Maybe if I have extra time near the end I can use @iplai library for node json creation. Or maybe the C4D team has already implement one by then hehe

              1 Reply Last reply Reply Quote 0
              • iplaiI
                iplai @bentraje
                last edited by

                @bentraje
                I misunderstood your question in my previous reply.
                This is my trial:

                import c4d
                
                with open(r"Path\to\your\file.json") as f:
                    c4d.CopyStringToClipboard(f.read())
                    # Paste command in Node Editor
                    # Command ID can be got by Script Log
                    c4d.CallCommand(465002305)
                

                1.gif

                C4DJSON -- a useful module to load an dump c4d objects in python dict format (similar to standard python json module).
                Examples on Notion.

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

                  @iplai

                  ah yep. I'll probably use this one since it does not require third party library. haha.
                  Thanks for the illustration! 🙂

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