3
$\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$
1
  • $\begingroup$ my 0.02c worth: Using TestNode and test_bool would IMO sit better with blenders API style convention. docs.blender.org/api/current/… $\endgroup$
    – batFINGER
    Commented Jun 17, 2020 at 13:50

1 Answer 1

4
$\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.

$\endgroup$
7
  • 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$ Commented Jun 17, 2020 at 12:13
  • $\begingroup$ How would that work for an instance of a class? As in ``` Class_Instance = My_Class("Example parameter")``` $\endgroup$
    – Dragonpeak
    Commented Jun 17, 2020 at 12:40
  • $\begingroup$ Thanks@RayMairlot - good suggestion. $\endgroup$ Commented Jun 17, 2020 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$ Commented Jun 17, 2020 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$ Commented Jun 17, 2020 at 14:23

You must log in to answer this question.

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