10

I'm a seasoned programmer, but fairly new to blender and 3D-modeling in general. So to get an understanding of different properties I want to write python scripts to create multiple renderings with different settings of various values.

In the example below I have figured out how to change the Volume Absorption Density, ex:

bpy.data.materials['MyMaterial'].node_tree.nodes['Volume Absorption'].inputs['Density'].default_value = 100

But not the RGB-node. It seems I cant even get the current values out of it. Some things I have tried:

bpy.data.materials['MyMaterial'].node_tree.nodes['RGB'].color
bpy.data.materials['MyMaterial'].node_tree.nodes['RGB'].color.r
bpy.data.materials['MyMaterial'].node_tree.nodes['RGB'].color.outputs['Color']

How do I get and set output values of the RGB-node in py python-script?

enter image description here

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
UlfR
  • 237
  • 3
  • 8
  • just fyi here's a script showing how you can build your nodes directly from python: https://blender.stackexchange.com/q/35436/5334 and here's another https://blender.stackexchange.com/q/34609/5334 – uhoh Feb 18 '19 at 17:06

2 Answers2

15

Welcome to Blender.SE, UlfR.

Finding out a property in Blender is relatively easy. Make sure, you have python tooltips enabled (in the user preferences).

Then hover your mouse cursor over the property (in this case the color). A tooltip containing the python command will appear.

tooltip

The property you seek seems to be outputs[0].default_value.

def_rgb = bpy.data.materials['MyMaterial'].node_tree.nodes['RGB'].outputs[0].default_value

It is a bpy_prop_array by default, so you may want to convert it to a list.

float_values = list(def_rgb)
Leander
  • 26,725
  • 2
  • 44
  • 105
  • Miceterminator beat me to it by 3 minutes. I will leave this answer up, as it mentions the python tooltip and his doesn't you could accept his answer as the correct one however. – Leander Feb 18 '19 at 12:00
  • my problem was that bpy.data.materials[1].node_tree.nodes['RGB'].outputs[0].default_value = (1.0,1.0,0.5,1.0) works but bpy.data.materials[1].node_tree.nodes['RGB'].outputs[0].default_value produces nothing in the Python console. Converting it to list then displayed the hidden values. – rob Feb 18 '19 at 12:07
  • 1
    You can also access the individual values directly from the bpy_prop_array via their indices, such as def_rgb[0]. – Leander Feb 18 '19 at 22:21
9

As you already found you way to the node tree, the "Copy data path" option is generally very helpful:

Right click on the field you want to access and use the "Copy data path" option.

bpy.data.materials['MyMaterial'].node_tree.nodes['RGB'].outputs[0].default_value

You can also look at the answers here and here

How to get data access to the data path

miceterminator
  • 3,624
  • 2
  • 20
  • 32