0

I'm trying to access the keys of a particular node of inputs and outputs. to access the use property keys:

n = my_node #(From node_tree.nodes)

for key in n.bl_rna.properties.keys():
    attributes = getattr(n, key)

now I would like to access the attributes of n.inputs and n.outputs

but this does not produce any results:

for key in n.outputs.bl_rna.properties.keys():
    attributes = getattr(n.outputs, key)

I'm pretty confused about this, I'm probably wrong

Edit: For more details, this would serve to copy the nodes and their properties. But when I have to access the inputs and outputs properties I have difficulties at the moment, post a small test that I am running, for now it does not work properly, but perhaps it makes better understand what I am trying to do

for n in bpy.context.scene.node_tree.nodes:
    tipo_di_nodo = n.bl_rna.identifier ##Ty @batFINGER
    nodo_copiato = bpy.context.scene.node_tree.nodes.new(tipo_di_nodo)

    #This copies the node exactly, with some data         
    for key in n.bl_rna.properties.keys():
        source_attribute = getattr(n, key)
        if not n.is_property_readonly(key):
            setattr(nodo_copiato, key, source_attribute)

    #I would like it to copy and assign value inputs
    #For now this is not working
    for val in n.inputs.values():
        for key in val.bl_rna.properties.keys():     
            in_source_attribute = getattr(val,key)
            if not val.is_property_readonly(key):
                setattr(nodo_copiato, key, in_source_attribute)

Note: I want to clarify, because i don't want to create further confusion, that the question is not "How to copy an entire tree of nodes" but is how to access the values n.inputs [#].default_values (and other) in complete automation, as it is done in the line of my code in the part where the attributes are set via setattr / getattr As I want to get there by myself to the copy of a tree of knots, I am trying to study the situation, trying to better understand Blender's API thanks to the help of the community

Noob Cat
  • 1,222
  • 3
  • 20
  • 61
  • 3
    Could you give a specific example. Eg here is all the names of the bsdf inputs https://blender.stackexchange.com/a/160069/15543 – batFINGER Jan 23 '20 at 14:11
  • I just edited the question to better explain the situation – Noob Cat Jan 23 '20 at 14:49
  • 2
    The edit doesn't really help here. What you'd like to do and what's your question? You want to copy the whole tree or just some of the nodes? – brockmann Jan 23 '20 at 15:36
  • I apologize for the confusion, I'm trying to copy the whole node tree, but I need to get and set the attributes of the inputs and outputs as they can have floats that need to be copied. I don't know if I'm going the right way – Noob Cat Jan 23 '20 at 15:43
  • 2
    From experience I'd say use a dict to store all the values and create the tree from scratch per execution. – brockmann Jan 23 '20 at 16:09
  • @brockmann Thanks for the tip, however, I would like to understand how to do it and why, at the moment I am a beginner with this type of situation, so I should go into detail before going to examine that script / addon that you created. – Noob Cat Jan 23 '20 at 16:31
  • What do you mean by "go into detail before..."? – brockmann Jan 23 '20 at 16:44
  • @brockmann I mean, that I have to learn well how all of this works, your script looks good, but I don't understand it much because I don't have the necessary knowledge now to decipher it and understand it – Noob Cat Jan 23 '20 at 17:03
  • 1
    I take it the big picture is "How to copy a node tree?" Or "How to copy a node" One suggested small change to above tipo_di_nodo = n.bl_rna.identifier Enjoying getting my Aussie accent around the Espanol – batFINGER Jan 24 '20 at 04:31
  • 1
    Ok, thanks. If that operator is too hard to understand, take my advice and remove all nodes of the shader in the first place then create the shader each time from scratch to save you headaches. One big issue is that all default values of each node type are 0. There is no way to "reset" the values of any node and you have to create a new node to get the default values anyway... and really, the API of the node editor isn't the greatest part @Pastrokkio – brockmann Jan 24 '20 at 09:45
  • @brockmann Thanks for the links, all very interesting and useful, but if you notice in my script, the node is copied exactly with its parameters (width height, and much more), for example if the node was the "Composite" output and had "use_alpha" False, this would be copied exactly with use_alpha False, but I would like to access the keys to also set n.inputs [1] default_values, my problem is that I want to assign the old attributes inputs to the new node. In your example it seems to me that you are "resetting" – Noob Cat Jan 24 '20 at 10:09
  • If you really want to go this way for whatever reason, I suggest set and get the socket values by their socket names instead of using the index operator to be on the safe side @Pastrokkio – brockmann Jan 24 '20 at 10:36
  • @brockmann Sorry if I continue to comment, but my English is not very good and sometimes I find it difficult to understand if I explained myself well / if I understood correctly. Technically I would like to access and copy these attributes, exactly as I did by copying the node attributes with for key in n.bl_rna.properties.keys (): (using if .is_property_readonly (key)) , this is ok. now I would like to access the "Sub attributes", [outputs, inputs] in the same way, I have not understood if it is possible to do it as it was done in the first attributes – Noob Cat Jan 24 '20 at 10:43
  • I suggest take the time and and put more effort into your question(s). You can also make it broader like "How can I copy a node and their properties" as suggested before if that's what you want... I don't know @Pastrokkio For now it seems like you just copied a random snippet of code from stackexchange land. – brockmann Jan 24 '20 at 11:08
  • @brockmann Thank you for your interest, but I would like to insist, if you notice in the first part of the code, the nodes are copied exactly with some of their attributes (for example, location, some Booleans, and the size of the node) this is very convenient as I can direct these attributes where I want (In this example I'm making a copy in the same node_trees). But I can't do the same for the other attributes – Noob Cat Feb 02 '20 at 20:20

0 Answers0