-1
$\begingroup$

How to find the node referenced by the update function of this BoolProperty property? I usually use self.id_data for other types of PointerProperty, but in this case I get the node_tree this node is contained in, I don't really understand how I can retrieve this node easily without having to create a super function.

Example:

def update_node_prop(self,context):
    node = self.id_data #How to get the node? this id_data is the node_tree (unfortunately for me)

class MyNodeProp(bpy.types.PropertyGroup):

    my_bool: bpy.props.BoolProperty(default = False, update = update_node_prop)


def register():
    bpy.types.Node.node_prop = bpy.props.PointerProperty(type=MyNodeProp)

More Info:

To make it clearer: Let's say I want to count the socket outputs of the node in the callback function, this seems really complicated to do at the moment.

I also thought about assigning a Unique value in a property that can be recognized via self.univoque_id, but this also seems complex to deal with and assign to all nodes.

Thanks to the comments, I forgot to include an important piece of information:

The context from which this property is called is not necessarily the context.space_data.edit_tree So I would like to get the node by simply using the self parameter and not the context

$\endgroup$
3
  • 3
    $\begingroup$ "Please work on the answer and not ask more questions. The question is well asked." No, it's not. No context is provided. No code that one can run to test the situation is provided. It's unclear what your goals are. The question is not communicated well at all and you have clear evidence of that - that's why it doesn't get answers for 2 and a half years now. You haven't provided enough information for anyone to be able to help you. $\endgroup$ Sep 21 at 10:31
  • 1
    $\begingroup$ yes it not well asked. also no explanation why u even need to do it. What problem are u trying to solve. XY problem? Downvoted. $\endgroup$
    – ManaTree
    Sep 21 at 13:43
  • $\begingroup$ I added further information to the question, the context is not necessarily from the shader editor so I wanted to work on the self parameter. Is there a way to change the Bounty note? $\endgroup$
    – Noob Cat
    Sep 21 at 16:38

1 Answer 1

1
$\begingroup$

I think I've found a pretty good way to answer this question.

In the meantime, I worked on a little function that gets the node from the self (which is PropertyGroup class)

Using this function you can locate the node that the PointerProperty is referring to at that exact moment when the callback function is called

def get_self_node(self):
    node_tree = self.id_data
    nodes = node_tree.nodes
    self_node = next((n for n in nodes if n.node_prop == self))
    return self_node

So this function can be called at the beginning of the callback function like this:

def update_node_prop(self,context):
    node = get_self_node(self)
    print(node.name)
    print(node.type)

Changing the boolean value will call the function from which we will obtain exactly the same node:

bpy.data.materials.node_tree.nodes['my_node'].node_prop.my_bool  = True

To safety, I suggest putting if statement in the callback:

def update_node_prop(self,context):
    node = get_self_node(self)

    if not node:  # <--
       return

    print(node.name)
    print(node.type)

and into the function:

def get_self_node(self):
    node_tree = self.id_data
    nodes = node_tree.nodes
    self_node = next((n for n in nodes if n.node_prop == self), None) # <--
    return self_node

This should do the trick, I've done various tests, it seems to always return the right node

The n.node_prop == self it always seems to be related to the node you are working on.

This function should avoid context problems, if for example you are not in the node tree context, and you want to showing the property in the 3D view for example in a panel in bl_space_type = "VIEW_3D"

$\endgroup$

You must log in to answer this question.

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