How to build a Cycles material node tree from console?

I’m trying to automate some custom node group creation with an addon. I’m kinda stuck though; it seems that just about anything I try from the console to manipulate nodes in the node editor fails with an ‘incorrect context’ error.

So everything I do has to be put into my script, then I have to run it, and only then can I figure out whether my code is working properly. That’s kind of inefficient. Surely there’s a way to create, move, and link nodes from the console?

ALSO, I’m really curious: how on earth do I get information about the nodes in a material? Like their names, their sockets, their location in the editor window… I’m kind of at a loss there.

Never mind, found the answer :slight_smile: http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Nodes

I have another question.

How do I create a link between a not-yet-existent socket on a ‘Group Input’ node and a node inside the the group? Group Input sockets seem to get created when you connect the ‘CUSTOM’ socket to another node.

Using

active_mat = C.object.active_material
tree = active_mat.node_tree
nodes = tree.nodes
new = nodes.new
new_link = tree.links.new

new_link() doesn’t work on the ‘CUSTOM’ socket index, and new() doesn’t seem to work. I know the types are ‘RGBA’, ‘VECTOR’, ‘VALUE’, but nothing happens when I use the new() function.

EDIT: Never mind, maybe it does work on the ‘CUSTOM’ socket.

Ok, I have another question. I need to add a node group inside another node group. One way to do that would be to create the nodes, then use operators (bpy.ops.node.group_make), but I don’t want to do that because I need to access the location and inputs/outputs of the new node group.

If I use


inner_group1 = bpy.data.node_groups.new('Inner Group', 'ShaderNodeTree')

bpy.ops.node.add_node(type='ShaderNodeGroup', settings=[{"name":"node_tree","value":"bpy.data.node_groups['{}']".format(inner_group1.name)}])

That works, but I can’t easily access the node group it seems. It gets renamed in the active material’s node tree to “Group”. What would be a better way of doing this?

Edit: Found the solution. Gotta create a new node of ‘group’ type, then create a new node group in bpy.data.node_groups, then assign the group to the group node’s node_tree.

inner_group2 = new('ShaderNodeGroup')temp = bpy.data.node_groups.new('Group2', 'ShaderNodeTree')
inner_group2.node_tree = temp