Objective: script to add a number of materials, assign colours using hex codes and be able to tweak afterwards in the Shader Editor using nodes.
Progress: Thanks to others (links in the file) I’ve got as far as creating materials and assigning colours from hex codes, as well as setting roughness and metallic values.
Problem: If I want to tweak a material afterwards by enabling nodes in the Shader Editor, all the values are lost immediately. I would like to add a normal map and be able to fine tune later, so this is why I am interested in trying to incorporate the BSDF shader from the outset.
I was kind of hoping that when a material was created that the Principled BSDF was there by default, as is the case when a new material is created using bpy.ops.materials.new() (either by clicking ‘New Material’ or typing that into the console).
If I try
bpy.data.materials["yellow"].node_tree.nodes["Principled BSDF"].inputs[0].default_value = (0.8, 0, 0, 1)
in the console (or script) then I get the error:: AttributeError: ‘NoneType’ object has no attribute ‘nodes’.
Similarly, if I try
mat_1.node_tree.nodes["Principled BSDF"].inputs[0].default_value = (0.8, 0, 0, 1)
#mat_1 is defined in the code below
then I get the same error. I presume because it is looking for the BSDF shader and nodes are not switched on. If I turn on nodes and re-run the code I get the same error, so I’m not sure.
This morning I thought I had got the solution:
mat_1 = bpy.ops.materials.new()
bpy.data.materials[0].name = "yellow"
Nope! This does work in the console but not as a script.
Disclaimer: I’m clearly not a coder and more like a bull in a china shop.
The code so far;
experiment 170522.blend (4.4 MB)