2
$\begingroup$

I am having an issue while working on a custom node tree. When I create a variable in self on a custom node it is not available in other functions of that class, for example:

from .base_node import  Base_Node
from bpy.types import Node


class Test_Node(Node, Base_Node):
    bl_label = "Test Node"

    def init(self, context):
        self.outputs.new("tree_mesh_socket_type", "Tree")
        self.inputs.new("tree_socket_type", "Tree In")
        self.Test_Bool = False

    def On_Execute(self):
        print(self.Test_Bool)

I get the error:

line 14, in On_Execute
print(self.Test_Bool)
AttributeError: 'Test_Node' object has no attribute 'Test_Bool'

This happens where ever I define the variable, it is only accessible from the function it is created in.

Am I just misunderstanding how the node classes work or is this a bug? Should I be using a different method to store the data?

Thanks for any help.

$\endgroup$
3
$\begingroup$

You need to define it outside of the functions to make it a property of the class. Try adding something like the following before your 'def's :

class Test_Node(Node, Base_Node):
    bl_label = "Test Node"
    
    #Define class-level property
    Test_Bool: bpy.props.BoolProperty() 

    def init(self, context):
        .....etc.

This will define a new class-level property 'Test_Bool' and this should then be available as 'self.Test_Bool' from any of the functions within the class.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Might be worth adding a few lines of code around that one line just to give a bit of context as to where, specifically, this line of code should be placed. $\endgroup$ – Ray Mairlot Jun 17 at 12:13
  • $\begingroup$ How would that work for an instance of a class? As in ``` Class_Instance = My_Class("Example parameter")``` $\endgroup$ – Dragonpeak Jun 17 at 12:40
  • $\begingroup$ Thanks@RayMairlot - good suggestion. $\endgroup$ – Rich Sedman Jun 17 at 12:43
  • $\begingroup$ @Dragonpeak - I'm not sure, but it should work the same. The key is to define it at class level rather than within the function. Perhaps that would be a question more suited to a Python group rather than Blender. $\endgroup$ – Rich Sedman Jun 17 at 12:47
  • 2
    $\begingroup$ @Dragonpeak It would be helpful if you could provide an example of your 'My_Class' situation. Perhaps ask a new question for that since the original one was specific to variables, not classes. $\endgroup$ – Rich Sedman Jun 17 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.