1
$\begingroup$

How can i cleanly declare and edit variables for use in an addon the don't need to be registered like a PropertyGroup but also aren't stored in the blend file?

I'm working off of the basic addon structure like so:

def boolPropVarAccess(self, context):
    # Test accessing a 'global' variable declared at some other point in the addon structure

class MyProperties(PropertyGroup):

    my_bool: BoolProperty(
        name="Enable or Disable",
        description="A bool property",
        default = False,
        update=boolPropVarAccess
        )


class OBJECT_PT_CustomPanel(Panel):
    bl_label = "My Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Tools"


    @classmethod
    def poll(self,context):
        return context.object is not None

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool


        # Declare some 'global' addon variable like an object reference

        layout.prop(mytool, "my_bool")

        layout.separator()

# ------------------------------------------------------------------------
#    Registration
# ------------------------------------------------------------------------

classes = (
    MyProperties,
    OBJECT_PT_CustomPanel
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    bpy.types.Scene.my_tool = PointerProperty(type=MyProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.my_tool


if __name__ == "__main__":
    register()
$\endgroup$
1
$\begingroup$

The window manager

If you set up properties on the window manager using bpy.props they are available globally for the session, are not saved and revert to defaults on next. PRO: simple to add a UI, a lot of UI for operators uses wm.

The driver namespace

The driver namespace can be leveraged.

class Foo:
    def __init__(self, x):
            self.x = x

dns = bpy.app.driver_namespace
dns["var"] = Foo(3)

then anywhere

global_var = dns.get("var") # None if not set

Make a module

from my_module import my_global_variable

Declare a global

max_something = 10000

def update(self, context):
    global max_something

Using lists and dictionaries here often removes the need for the global declaration.

Getter Setter on bpy.props

In example in question code, if using a global instead of using update would set up properties with a get set method. See docs.

As in this example, Internal "get/set" function of property?

let the get or set methods retrieve or set your global respectively. The set method is akin to an update in this scenario.

$\endgroup$

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.