4

just as a disclaimer, I'm a beginner in terms of Python scripting or programming in general, but after doing my research for such a long time, I decided to ask the experts here.

I want to change the Principled BSDF node subsurface method from the default "Random Walk" to "Random Walk (Fixed Radius)" of multiple materials of my selected object.

So instead of changing each node by hand, I decided to look for a script that finds all the principled BSDF nodes of the active object and performs the changes.

I have found multiple posts with python codes that show you how to change, for example the metallic value etc. (How to change a certain parameter of all principled shaders in my blend file?)

import bpy

for mat in bpy.data.materials: if hasattr(mat.node_tree, "nodes"): for node in mat.node_tree.nodes: if node.type == 'BSDF_PRINCIPLED': for input in node.inputs: if input.name == 'Metallic': input.default_value = 0.7

This works for all the inputs like roughness, alpha etc. BUT I have trouble targeting the subsurface method [Random Walk to Random Walk (Fixed Radius)].

enter image description here

I was hoping that I could have figured it out by myself but so far it seems it's not as straightforward as changing

if input.name == 'Metallic':
    input.default_value = 0.7

to

if input.name == 'Subsurface Method':
    input.default_value = 'RANDOM_WALK_FIXED_RADIUS'

The issue might be that it is not an "input" like "Metallic" but I don't know how to modify it otherwise.

Hope someone can help and even better, maybe give me a pointer on what the things are I would need to learn and understand to maybe find the solution myself.

Cheers!

Big Newbie
  • 43
  • 2

1 Answers1

5

Node.inputs are sockets, those things with dots on the side. You can change the Subsurface Method with Node.subsurface_method.

import bpy

for mat in bpy.data.materials: if mat.node_tree: for node in mat.node_tree.nodes: if node.type == 'BSDF_PRINCIPLED': node.subsurface_method = 'RANDOM_WALK'

You can find this stuff out by turning on Python Tooltips in the preferences and hovering over the thing you want to change. It will tell you how to access it in Python.

p2or
  • 15,860
  • 10
  • 83
  • 143
scurest
  • 10,349
  • 13
  • 31
  • Thank you for your help! So there is no need to check for the name of the value I want to change? Also great to know about the tooltip tip. Cheers! – Big Newbie Dec 23 '21 at 03:39
  • 1
    No. There is no need to for inputs either, you can just do node.inputs["Metallic"].default_value = 0.7. – scurest Dec 23 '21 at 04:10