Книга: The Cycles Encyclopedia 1.4
Назад: Chapter 18: The Node Wrangler Addon
Дальше: Glossary

Chapter 19: Noodles and Snakes

For everyone interested, I prepared a Python and Nodes 101.

Nodes and Python

You can change pretty much any value of a node in your material by addressing it with Python. If you are new to python, I suggest to switch to the Scripting workspace, by hitting CTRL + RIGHT ARROW until you see a blue and black console and a text editor. In the console you can test out any Python command individually. If you want to use the code in the text editor, always start with

import bpy

This imports the Blender Python library that gives you access to all the Blender-specific functions in Python.

If you want to access the nodes of your material, there are several ways:

1. Address the material directly, for this type:

MyMaterial = bpy.data.materials[“Material Name”]

Replace Material Name by the name of the material you want to access, but keep the quotes.

2. Get the material of the active object. For this you first need to select the object that has the material assigned, which you want to access. Type in:

obj = bpy.context.active_object

MyMaterial = obj.data.material_slots[0].material

This stores the first material of the list. If you have several materials on your object, replace the 0 by the index of your material slot.

Note: Arrays or lists in programming languages start at number 0, so slot number 2 is actually material_slot[1].

Now that we have the material stored, we can access its node tree:

nt = MyMaterial.node_tree

if we want to address any node, we can now do so by typing:

MyNode = nt.nodes[“Node Name”]

To find out the name of a node, select it in the node editor and press N. The properties menu will open and display a bunch of information. The name of the node is a unique identifier, while the label is what will be displayed in the title of a node in the node tree, if not left blank.

We can now access the properties of the node, and change their values.

Connecting Nodes

You might think that the link between two nodes is stored in the nodes themselves and if you want to make a new connection this would be the way to go. Well - it isn’t. Links are part of the node tree, so if you want to link two nodes, you need to type:

nt.links.new(input, output)

An input is a socket on the left hand side of a node, so before we can link nodes, we need to have a look at the in- and outputs. Let’s say we want to connect two nodes:

node_out = nt.nodes[“Diffuse BSDF”]

node_in = nt.nodes[“Mix Shader”]

I f we were to use the node_in and node_out in the statement above, we would receive an error, because Cycles does not know which input to use for the mix shader. And even though there is only 1 output of the diffuse shader, it still wants to be told which one we mean.

So we could say:

output = node_out.outputs[“BSDF”]

input = node_in.inputs[1]

I deliberately used the name of the output for the upper and the index of the input for the lower line. This should show that you can do both. Since the mix node has two inputs named “Shader”, I find it easier to use the index to call them. The same goes for all sockets, because typing 0 is faster than “BSDF”, all you need to do is count and subtract 1.

So now the line

nt.links.new(input, output)

actually works as it is, because both variables input and output are actual sockets.

Helpful Parameters

You can find out all parameters of an object in Blender by typing it in as far as you know with a period behind it and pressing CTRL + SPACE. However there is no direct search function for a keyword. So it is very helpful to know where to start off. As mentioned above, you can get to a node by using:

MyMaterial = bpy.data.materials[“Material Name”]

nt = MyMaterial.node_tree

MyNode = nt.nodes[“node name”]

Y ou can then display the attributes of the node by using

MyNode. and CTRL + SPACE in the console. This function is called auto-complete and displays most of the sensible stuff that you could add at this point.

Here are the most important ones:

inputs : List of input sockets

outputs : List of output sockets

From an in- or output, e.g.

MyNode.inputs[“Roughness”].default_value = 1

w ill set the roughness of a node (make sure it does have that slider) to 1. Again, you can either use the name of the input or the index, but if you want to alter the value, you will not have to change the inputs[0] to any value (that would cause an error), but instead put .default_value behind it.

Modes of a node do not have an input socket, for example the distribution type of an reflective or refractive shader. The mode of the node can be accessed with this code:

MyNode.distribution = ‘GGX’ #or ‘BECKMANN’ or ‘ASHIKMIN_SHIRLEY’

w ill set the distribution type, or mode of a glass, glossy, anisotropic or refraction shader to GGX.

There is no difference between “ ” or ‘ ’, there are two types, so you can use quotes within quotes.

If your node is a color ramp it can be a bit tricky to find out how to alter the stops. They are stored under:

MyNode.color_ramp.elements

You can add new ones by using .new() or alter their color.

MyNode.color_ramp.elements[0].color = [1,1,1,1]

A color has to be a vector of 4 dimensions, RGBA. If you already chose a color, you can hover over it and press CTRL + C, then move your mouse to where your code is, press CTRL + V and it will insert the RGBA value of that color.

You can move the stops with

MyNode.color_ramp.elements[0].position = 0.5 # or any other number from 0 to 1

This will move the 1. slider to the middle of the ramp.

With a color mix node, you can use:

MyNode.blend_type = ‘ADD’

Type in the blend type in quotes and capital letters, eventual spaces have to be replaced by underscores.

Finally I’d like to share with you one line of code that saves render time in any situation: silent render. If your computer is not busy reloading the picture as the render progresses, it can significantly improve performance, and this is what silent render means.

Either create a script with these 2 lines:

import bpy

bpy.ops.render.render(animation=False, write_still=False, layer="", scene="Scene")

Or enter the lower line in the console. If you have renamed your scene at any time you will have to replace “Scene” by your new scene name, also in quotes. If you choose for write_still to be True, Blender will automatically save the rendered image at the destination you specified under “output” in the render tab.

Note: You cannot abort the silent render with the Escape key, when it renders, it renders! So try starting Blender from the command line or with the console active, so you can kill it and thereby Blender, if you want to abort. This implies: Be sure to save your work, before you start the scriptlet.


Назад: Chapter 18: The Node Wrangler Addon
Дальше: Glossary