3
$\begingroup$

I am adding properties to the Emission shader node type:

bpy.types.ShaderNodeEmission.myaddon= PointerProperty(type=MyEmissionSettings)

It seems to work fine, each Emission node gets its own new properties. However, I have an update function for one of these properties and I'm trying to get the node, but self.id_data returns the Node Tree:

<bpy_struct, ShaderNodeTree("Shader Nodetree") at 0x00000225C68E1CC8>

How can get the node from its property?

EDIT: here is a bit more code example:

def update_color(self,context):
    color = self['color']
    # I want to now get the node that owns that property self['Color']
    node = self.id_data
    # This is incorrect, self.id_data returns the Shader Node Tree
    # I want to get the node itself to be able to assign that color to one of its input
    node.inputs[2] = color
  
class MyEmissionSettings(bpy.types.PropertyGroup):
    color: bpy.props.FloatVectorProperty(
        subtype='COLOR',
        min=0.0, max=1.0, size=4,
        default=(1.0,1.0,1.0,1.0),
        update=update_color,
        )   
$\endgroup$
2
  • $\begingroup$ Without testing, you can access any custom property of any object by using myObject['MyPropertyName']. If I completely misunderstood you, maybe shader parts of your script? $\endgroup$ Mar 9, 2021 at 17:33
  • $\begingroup$ It's the opposite, I have the property but not "myObject", in that case, the node. I will update the question with a bit more code. $\endgroup$
    – chafouin
    Mar 10, 2021 at 4:55

1 Answer 1

9
$\begingroup$

Wire it up in a setter.

Touched on this in answer to How to Update NodeSocketString Value inside a node group in a attribute node using python

As noted in question self.id_data is the node tree, not the node, and self is the property group. Even without being defined a property group has a name property.

Will use this to, when trying to set a value, look over the node tree first and assign any emmission shader's name to its "myaddon" pointer property name, then use this to look up what the node is.

Sounds confusing? have added a simple example and tested in py console.

import bpy

def wire_up(node_tree, nodetype):
    nodes = (n for n in node_tree.nodes if n.type == nodetype)
    for n in nodes:
        n.myaddon.name = n.name

def update_color(self, context):
    print("Updating Value")
    
  
def get_node(self):
    print("Getting Value")
    return self.get("color", (1.0,1.0,1.0,1.0))

def set_node(self, value):
    print("Setting Value")
    wire_up(self.id_data, 'EMISSION')
    self['color'] = value
    node = self.id_data.nodes.get(self.name)
    if node:
        print(node)
    else:
        print("Node not found")
            
class MyEmissionSettings(bpy.types.PropertyGroup):
    color: bpy.props.FloatVectorProperty(
        subtype='COLOR',
        min=0.0, max=1.0, size=4,
        default=(1.0,1.0,1.0,1.0),
        #update=update_color,
        get=get_node,
        set=set_node,
        ) 

bpy.utils.register_class(MyEmissionSettings)
        
bpy.types.ShaderNodeEmission.myaddon = bpy.props.PointerProperty(type=MyEmissionSettings)

Test Run

>>> mat = bpy.data.materials.new("Test")
>>> mat.use_nodes = True
>>> mat.node_tree.nodes.new("ShaderNodeEmission")
bpy.data.materials['Test'].node_tree.nodes["Emission"]

>>> mat.node_tree.nodes.new("ShaderNodeEmission")
bpy.data.materials['Test'].node_tree.nodes["Emission.001"]

>>> en1 = mat.node_tree.nodes['Emission']
>>> en2 = mat.node_tree.nodes['Emission.001']
>>> en1.myaddon.color[:]
Getting Value
(1.0, 1.0, 1.0, 1.0)

>>> en1.myaddon.color = (1, 0, 0, 1)
Setting Value
<bpy_struct, ShaderNodeEmission("Emission") at 0x7fa0bddf8c08>

>>> en1.myaddon.color[:]
Getting Value
(1.0, 0.0, 0.0, 1.0)

>>> en2.myaddon.color = (0, 0, 1, 1)
Setting Value
<bpy_struct, ShaderNodeEmission("Emission.001") at 0x7fa096f38e08>
$\endgroup$
3
  • 1
    $\begingroup$ What a day! 4 upvotes in 1 hour on this niche question, great! $\endgroup$
    – brockmann
    Mar 10, 2021 at 9:24
  • $\begingroup$ lol, will try and answer all my questions at 7pm Wednesdays (local time) $\endgroup$
    – batFINGER
    Mar 10, 2021 at 9:34
  • 2
    $\begingroup$ Thanks a lot @batFINGER ! $\endgroup$
    – chafouin
    Mar 10, 2021 at 9:42

You must log in to answer this question.

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