2
$\begingroup$

I'm working on an add-on where I want to have a static local boolean variable, but I'm not sure how to do it. The main bit of code is this, in the execute() function:

class TEST_PT_RunScript(bpy.types.Operator):
    bl_idname = "bk.test"
    bl_label = "Test"

    def execute(self, context):
        shift_lock = getattr(self, 'shift_lock', False) # Get the value of shift_lock

        if (context.scene.bk.keystroke == 'SHIFTLOCK'):
            shift_lock = not shift_lock # Flip the value of shift_lock
            setattr(self, 'shift_lock', shift_lock) # Store the value of shift_lock
            self.report({'INFO'}, '  Shift Lock:' + str(shift_lock))
        return {'FINISHED'}

I realize I am not doing this correctly with setattr() and getattr(), but I have researched a bunch of different ways for this function to remember the value of the shift_lock variable and I have yet to figure out how to do it within the context of a Blender add-on.

The entire add-on code follows. Put this code in a file called __init__.py and put that file in a directory called test in your add-ons directory to run it:

import bpy
import bmesh
import os
import sys
sys.path.append(os.path.dirname(__file__))

# Version history

bl_info = {
    "name": "Test",
    "author": "",
    "version": (1, 0, 0),
    "blender": (2, 80, 0),
    'location': 'View3D > Properties  > Test',
    "description": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"}

class TEST_PT_RunScript(bpy.types.Operator):
    bl_idname = "bk.test"
    bl_label = "Test"

    def execute(self, context):
        shift_lock = getattr(self, 'shift_lock', False) # Get the value of shift_lock

        if (context.scene.bk.keystroke == 'SHIFTLOCK'):
            shift_lock = not shift_lock # Flip the value of shift_lock
            setattr(self, 'shift_lock', shift_lock) # Store the value of shift_lock
            self.report({'INFO'}, '  Shift Lock:' + str(shift_lock))
        return {'FINISHED'}



# ------------------------------------
# Viewport panel.
# ------------------------------------
class TEST_PT_Main(bpy.types.Panel):
    bl_idname = "TEST_PT_Main"
    bl_label = "Test"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Test"

    def draw(self, context):
        row = self.layout.row(align=True)
        self.layout.prop(context.scene.bk, "keystroke")
        self.layout.operator("bk.test")


class TEST_TestProps(bpy.types.PropertyGroup):

    keystroke: bpy.props.EnumProperty(name="Keymap",
                                        description="",
                                        items=[
                                            ('SHIFTLOCK', 'Shift Lock', 'g')])



def register():
    bpy.utils.register_class(TEST_PT_RunScript)
    bpy.utils.register_class(TEST_TestProps)
    bpy.utils.register_class(TEST_PT_Main)
    bpy.types.Scene.bk = bpy.props.PointerProperty(type=TEST_TestProps)


def unregister():
    bpy.utils.unregister_class(TEST_PT_RunScript)
    bpy.utils.unregister_class(TEST_TestProps)
    bpy.utils.unregister_class(TEST_PT_Main)


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

0

You must log in to answer this question.

Browse other questions tagged .