Solved Redshift deleting using old information

Hi, I am deleting a User Data node from Redhshift using the code below:

# Remove Redshift UD node
    for item in spline_id:
        rs_mat = doc.SearchObject ("S" + str (item) + " Gas " + str (bake_name))
        rs_tag = rs_mat.GetTag(c4d.Ttexture)
        rs_mat = rs_tag.GetMaterial()
        outputNode = rs_mat[c4d.REDSHIFT_GRAPH_NODES]
        gvMaster = outputNode.GetNodeMaster()
        gvRoot = gvMaster.GetRoot()

        currentNode = gvRoot.GetDown()
        while currentNode is not None:
            if currentNode.GetName() == "Neon Redshift":
                currentNode.Remove()
                break

            currentNode = currentNode.GetNext()

The code works perfectly but when the node is deleted, for some reason, the settings for the other nodes, particlularly colour, are not what was inputted by the User Data node. Can you help?

Hi,

There are quite a few hoops to jump through, but this example should get you the idea.

import c4d

def get_port_descid(node, port):
    """Returns the DescID for a ``GvPort``.

    If all our ports would be for static description elements, this would be
    easy. It would be just ``port.GetSubID()``. Unfortunately we are dealing
    with a lot of dynamic descriptions in the context of Xpresso, and at
    least I am not aware of any builtin methods that would return the 
    ``DescID`` of a ``GvPort`` in a more sane way.

    This solution is especially lazy, a solution less prone to errors
    would be to iterate over the description of the ``GvNode`` and see which
    combination of ``m, s, u`` is in there.

    Args:
        node (``c4d.modules.graphview.GvNode``): The node of the port.
        port (``c4d.modules.graphview.GvPort``): The port to get the DescID for.
    
    Returns:
        ``tuple[int]`` or ``None``: The resolved DescID.
    """
    # The fragments (DescLevels) of the DescID of the port. The problem is
    # that all methods always return an integer-
    m, s, u = port.GetMainID(), port.GetSubID(), port.GetUserID()

    # Now we are just trying to access the node and see if it raises an
    # (attribute) error. If so, we just try the next combination.

    # There are no user data DescLevels below 1.
    if u > 0:
        try:
            node[(m, s, u)]
            # It is a user data DescID.
            return (m, s, u)
        except AttributeError as e:
            pass

    try:
        node[(m, s)]
        # It is a dynamic description DescID.
        return (m, s)
    except AttributeError as e:
        pass

    try:
        node[s]
        # It is a static description DescID.
        return s
    except AttributeError as e:
        pass

    return None

def main():
    """Entry point.
    """
    # This example is for a Xpresso GraphView instance, since I do not
    # have access to RedShift. I did use two constant nodes and a math 
    # node as an example scene.
    if not op or not op.GetTag(c4d.Texpresso):
        return 

    tag = op.GetTag(c4d.Texpresso)
    master = tag.GetNodeMaster()
    root = master.GetRoot()

    if not root:
        return

    nodes = root.GetChildren()
    if not nodes:
        return

    # I just took the first top-level node as the node to disconnect, 
    # which was one of the constant nodes connected to the math node.
    out_node = nodes[0]

    # Go over all output ports in our node to disconnect/remove.
    for out_port in out_node.GetOutPorts():
        # Get all input ports the current output port is connected to.
        connected_ports = out_port.GetDestination()
        # Severe all connections from our output port.  
        out_port.Remove()
        # Let Cinema catch up.
        c4d.EventAdd()
        # This one of the nastier parts. We will have to unwind the
        # DescID the output port has been build for. See the 
        # respective function for details.
        out_did = get_port_descid(out_node, out_port)
        # We couldn't resolve the DescID properly.
        if out_did is None:
            continue
        # Now we are just going over all input ports, get the DescID
        # they are pointing at and write the value from our output_node
        # to the input_node with these two DescIDs.
        for in_port in connected_ports:
            in_node = in_port.GetNode()
            in_did = (in_port.GetMainID(), in_port.GetSubID())
            in_did = get_port_descid(in_node, in_port)
            if in_did is not None:
                in_node[in_did] = out_node[out_did]

if __name__=='__main__':
    main()

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

Hi,

I am not quite sure if I do understand your question correctly. From what I am understanding, you have the graphview nodes A and B, where the nodes are the relationship A -> B, and you are deleting A, but would expect B to maintain its state after that?

Cinema does not do that, probably for the reason that it would contradict the state machine nature of a node based interpreted language. Your only option to maintain the states of the input ports formerly driven by your user data node, would be to iterate over all output ports of that user data node and cache their current states, then iterate over all input ports each output port is being connected to, disconnect them one by one and write the cached state to the BaseContainer of the node that input port is attached to.

Cheers
zipit

MAXON SDK Specialist
developers.maxon.net

Would it be possible for you to show me a few lines of code that demonstrate what you are talking about? Especially disconnecting the ports one at a time. Thanks.

Hi,

There are quite a few hoops to jump through, but this example should get you the idea.

import c4d

def get_port_descid(node, port):
    """Returns the DescID for a ``GvPort``.

    If all our ports would be for static description elements, this would be
    easy. It would be just ``port.GetSubID()``. Unfortunately we are dealing
    with a lot of dynamic descriptions in the context of Xpresso, and at
    least I am not aware of any builtin methods that would return the 
    ``DescID`` of a ``GvPort`` in a more sane way.

    This solution is especially lazy, a solution less prone to errors
    would be to iterate over the description of the ``GvNode`` and see which
    combination of ``m, s, u`` is in there.

    Args:
        node (``c4d.modules.graphview.GvNode``): The node of the port.
        port (``c4d.modules.graphview.GvPort``): The port to get the DescID for.
    
    Returns:
        ``tuple[int]`` or ``None``: The resolved DescID.
    """
    # The fragments (DescLevels) of the DescID of the port. The problem is
    # that all methods always return an integer-
    m, s, u = port.GetMainID(), port.GetSubID(), port.GetUserID()

    # Now we are just trying to access the node and see if it raises an
    # (attribute) error. If so, we just try the next combination.

    # There are no user data DescLevels below 1.
    if u > 0:
        try:
            node[(m, s, u)]
            # It is a user data DescID.
            return (m, s, u)
        except AttributeError as e:
            pass

    try:
        node[(m, s)]
        # It is a dynamic description DescID.
        return (m, s)
    except AttributeError as e:
        pass

    try:
        node[s]
        # It is a static description DescID.
        return s
    except AttributeError as e:
        pass

    return None

def main():
    """Entry point.
    """
    # This example is for a Xpresso GraphView instance, since I do not
    # have access to RedShift. I did use two constant nodes and a math 
    # node as an example scene.
    if not op or not op.GetTag(c4d.Texpresso):
        return 

    tag = op.GetTag(c4d.Texpresso)
    master = tag.GetNodeMaster()
    root = master.GetRoot()

    if not root:
        return

    nodes = root.GetChildren()
    if not nodes:
        return

    # I just took the first top-level node as the node to disconnect, 
    # which was one of the constant nodes connected to the math node.
    out_node = nodes[0]

    # Go over all output ports in our node to disconnect/remove.
    for out_port in out_node.GetOutPorts():
        # Get all input ports the current output port is connected to.
        connected_ports = out_port.GetDestination()
        # Severe all connections from our output port.  
        out_port.Remove()
        # Let Cinema catch up.
        c4d.EventAdd()
        # This one of the nastier parts. We will have to unwind the
        # DescID the output port has been build for. See the 
        # respective function for details.
        out_did = get_port_descid(out_node, out_port)
        # We couldn't resolve the DescID properly.
        if out_did is None:
            continue
        # Now we are just going over all input ports, get the DescID
        # they are pointing at and write the value from our output_node
        # to the input_node with these two DescIDs.
        for in_port in connected_ports:
            in_node = in_port.GetNode()
            in_did = (in_port.GetMainID(), in_port.GetSubID())
            in_did = get_port_descid(in_node, in_port)
            if in_did is not None:
                in_node[in_did] = out_node[out_did]

if __name__=='__main__':
    main()

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

Thanks, zipit, you are a Code God! Much appreciated. :)

Hmmm. I tried to implement the code (fixed indent errors in the first def) and the print out looks like it is catching what it should but the User Data is not being copied over to the inputs before it gets deleted. Any idea what I am doing wrong?

    def get_port_descid(node, port):
        m, s, u = port.GetMainID(), port.GetSubID(), port.GetUserID()
        if u > 0:
            try:
                node[(m, s, u)]
                return (m, s, u)
            except AttributeError as e:
                pass
            try:
                node[(m, s)]
                return (m, s)
            except AttributeError as e:
                pass
            try:
                node[s]
                return s
            except AttributeError as e:
                pass

        return None

    for item in spline_id:
        rs_mat = doc.SearchObject ("S" + str (item) + " Gas " + str (bake_name))
        rs_tag = rs_mat.GetTag(c4d.Ttexture)
        rs_mat = rs_tag.GetMaterial()
        outputNode = rs_mat[c4d.REDSHIFT_GRAPH_NODES]
        gvMaster = outputNode.GetNodeMaster()
        gvRoot = gvMaster.GetRoot()

        currentNode = gvRoot.GetDown()
        while currentNode is not None:
            if currentNode.GetName() == "Neon Redshift":
                for out_port in currentNode.GetOutPorts():

                    connected_ports = out_port.GetDestination()
                    out_port.Remove()

                    c4d.EventAdd()

                    out_did = get_port_descid(currentNode, out_port)
                    print out_did
                    if out_did is None:
                        continue

                    for in_port in connected_ports:
                        in_node = in_port.GetNode()
                        in_did = (in_port.GetMainID(), in_port.GetSubID())
                        in_did = get_port_descid(in_node, in_port)
                        if in_did is not None:
                            in_node[in_did] = currentNode[out_did]

                currentNode.Remove()
                break

            currentNode = currentNode.GetNext()

Because data is coming from User Data, do I need additional info as per this post?
http://www.plugincafe.com/forum/forum_posts.asp?TID=5188

Hi,

I did not really test the user data stuff, I just included it, since I knew you are going to need it. But looking at my code now, the user data stuff does not make much sense. It should probably be node[(m, u)] or node[(s, u)]to test for user data ids (probably the first one). User data DescIDs follow the form ID_USERDATA, x, so for example 700, 1 for the first element. You have to poke around a little bit to find out what is what, but my guess would be node[(m, u)].

If you keep running into errors, you should try the description stuff I mentioned in the function docstring instead. The approach of the function is not the safest ;)

PS: You also do not need the line in_did = (in_port.GetMainID(), in_port.GetSubID()), this is just some garbage I forgot to delete before I realised that I was going to need a dedicated function for this ;)

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

The original code yields image 1:
Screen Shot 2020-05-31 at 5.13.22 AM.png
m, u yields image 2:
Screen Shot 2020-05-31 at 5.14.23 AM.png
s, u yields image 3:
Screen Shot 2020-05-31 at 5.15.05 AM.png
It doesn't look like any of the actual User Data Values (3 of which are colour) are coming through. Here are images of the node and the UD:
Screen Shot 2020-05-31 at 5.18.21 AM.png
Screen Shot 2020-05-31 at 5.18.46 AM.png
Neither m,u or s,u causes the actual User Data to transfer to the nodes before the User Data node gets deleted. Is there something I am missing?

Hi,

I actually meant doing that inside the get_port_descid function. However, I just did it for you and found out that for user data DescID elements the port returns some gibberish for m, s, u like for example 20000000 1000 -1 for a port for the first user data element for a null (we would at least expect 700 and 1 to pop up in there).

The problem is that ports for user data elements and the DescID they are being build for always have been a bit weird (see GvNode.AddPort). To build a port for the first user data element of a node, we would initialise it with the DescID (700, 5, 1) (the weird part being the 5, which is the integer for the symbol DTYPE_SUBCONTAINER). The other problem is that ports do not really expose the element they point at to the outside world.

I am afraid that you will have to wait for MAXON to shed some light on the topic, but I would not hold my breath for them coming up with a solution. It might very well be the case that you cannot infer the DescID for ports pointing at user data elements.

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

Cool. I will try a different approach. Thanks for all your help on this, zipit. :)

I tried inputting the User Data directly into a RS port using Python and this code:

import c4d

def main():

    obj = op.GetObject()
    neon_col_INPUT = obj[c4d.ID_USERDATA,834]

    # List: Spline, Colour, Power, Gas, Blend.
    spline_UD = {1: 618}

    for item in spline_UD:
        rs_mat = doc.SearchObject ("S" + str (item) + " Gas")
        rs_tag = rs_mat.GetTag(c4d.Ttexture)
        rs_mat = rs_tag.GetMaterial()
        outputNode = rs_mat[c4d.REDSHIFT_GRAPH_NODES]
        gvMaster = outputNode.GetNodeMaster()
        gvRoot = gvMaster.GetRoot()

        colour = obj[c4d.ID_USERDATA,spline_UD[item]]
        print colour

        power = obj[c4d.ID_USERDATA,619]
        gas = obj[c4d.ID_USERDATA,65]
        blend = obj[c4d.ID_USERDATA,66]

        currentNode = gvRoot.GetDown()
        while currentNode is not None:
            if currentNode.GetName() == "RS Material":
                RSMaterial[c4d.REDSHIFT_SHADER_MATERIAL_EMISSION_COLOR] = colour
                break

Now my code causes C4D to hang. Can you help?

I figured out a way to get this to work. They key is separating the User Data out of the RS XPresso node but still maintaining a connection to this node through a regular XPresso node. Cinema won't let me drag Redshift nodes from the RS XPresso window to the C4D XPresso window. Nor can I drag them from the AM.

The solution is to delete the User Data from the RS XPresso window, paste it into a new C4D Xpresso window, then use Set Driven (Absolute) on the RS nodes I need what is essentially double access to. Cinema then automatically creates a new XPresso node, which I then cut the contents from and paste them into my UD Xpresso node.

My only question is what is the difference between Set Driven (Absolute) and Set Driven (Relative)? Does it make a difference? Thanks.

Hi,

sorry, I did not see your replies. But I cannot help you as I do neither have access to Redshift nor am very fluent when it comes to Cinemas features.

Cheers,
zipit

MAXON SDK Specialist
developers.maxon.net

@Swinn said in Redshift deleting using old information:

My only question is what is the difference between Set Driven (Absolute) and Set Driven (Relative)? Does it make a difference?

Both setups add a Range Mapper node. In case of 'Relative', this range mapper creates an offset. See the online help.

Thanks, PluginStudent! :)

hi,

your question is sometimes not obvious :D
Even with the scene it's a bit compicated.

What i can say about your code is that you should be more defensive in your code. Always check the value before continue.

For example what if the object is not found ? You should continue to the next item or stop ?

for item in spline_UD:
        rs_mat = doc.SearchObject ("S" + str (item) + " Gas")
        if rs_mat is None:
            # Because we don't have object to continue, we iterate to the next item.
            continue
        ...

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer