I've spent far too long googling for this answer. :)

I have a Cycles shader node group called ProteinVR that I created using the node editor. It has two inputs: enter image description here

This same node group is used by multiple users (i.e., different objects in the scene use the same node group to generate their materials), but with different inputs. So a different object might look like this: enter image description here

I then load the scene from the command line like this:

/my/blender/exec/blender -b /my/blender/file.blend -P /my/blender/script.py

Within the /my/blender/script.py file, I want to be able to determine the input values (color or glossiness) of the node group for each user (object). I've found how to access the node group itself and explore its structure and default values programmatically, but I can't get the specific values being used for each user/object.

Any help you could offer would be most appreciated! First question on stack exchange... hope it goes well. :)

P.S. Blender 2.78, in case it's helpful.


1 Answer 1

import bpy

# get the object
ob = bpy.data.objects.get("NAME")

# get the material in 1st material slot
if ob and ob.data.materials:
    mat = ob.data.materials[0]

    # get the material nodes
    nodes = mat.node_tree.nodes

    # get the instance of the node_group by name of the node (not label, name)
    protein_vr = nodes.get("ProteinVR")
    # if there are multiple instances in one material, others will likely be ProteinVR.001, ProteinVR.002, etc..

    # get the inputs if node exists
    if protein_vr:
        color = protein_vr.inputs[0].default_value
        gloss = protein_vr.inputs[1].default_value

I compiled the answer from these 2, there is more in them:

  • $\begingroup$ When I print(color), I get this: <bpy_struct, NodeSocketColor("Color or Image")>. But I need the RGB values of the color (if it's a color) or the filename of the image (if it's an image-texture input). dir(color) gives ['__doc__', '__module__', '__slots__', 'bl_idname', 'bl_rna', 'default_value', 'draw', 'draw_color', 'enabled', 'hide', 'hide_value', 'identifier', 'is_linked', 'is_output', 'link_limit', 'name', 'node', 'rna_type', 'show_expanded', 'type']. I can't for the life of me figure out how to get the actual values from those properties. Thanks! $\endgroup$
    – jdurrant
    Apr 8, 2017 at 14:48
  • $\begingroup$ @jdurrant I am super sorry, there needs be .default_value of course. That will give you a vector type with RGBA $\endgroup$ Apr 8, 2017 at 17:58
  • $\begingroup$ Perfect! I should have figured the .default_value thing out myself. Thanks for your help. I accepted your answer. $\endgroup$
    – jdurrant
    Apr 8, 2017 at 20:44
  • $\begingroup$ If you've got some extra time, I posted a related question here: blender.stackexchange.com/questions/77365/… :) $\endgroup$
    – jdurrant
    Apr 8, 2017 at 21:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .