0
$\begingroup$

I'm using Scene data block to store properties and use them later in my script, e.g.:

def register():
    Scene.bcp_return_result: BoolProperty = BoolProperty(default=return_result,
                                                         name="Return result",
                                                         description="Indicates if result of command should be returned")

And later:

return_result=bpy.context.scene.bcp_return_result

But content of Scene datablock is lost after opening a new scene or creating a new one. What content could I use to still access properties in this way and have them stored between scenes?

$\endgroup$
0
$\begingroup$

You use bpy.context.scene. which refers to your current active scene, e.g. bpy.data.scenes['Your Scene Name']. When you change the active scene in Blender the bpy.context.scene. starts to refer to another scene, e.g. bpy.data.scenes['Another Scene Name'] Context in bpy.context means "current". About Blender Properties like bpy.types.BoolProperty and the others - they are supposed to be used to dinamically change values of operators, not for storing any constant information.

If you want to store your properties you can:

1) Set a global variable outside all classes, right in your script body and call it from your functions and methods by marking it global (using global variables in big programs is not a very good idea because as your program grows it is getting hard to manage them):

bcp_return_result = None

def register():
    global bcp_return_result
    bcp_return_result = 1

print(bcp_return_result) # >>>None
register()
print(bcp_return_result) # >>>1

2) Set and change it as your class variable. It can be either Blender Operator class

import bpy
from bpy.utils import register_class, unregister_class

class RegisterOperator(bpy.types.Operator):
    bl_idname='reg.op'              # id_name for bpy.ops, e.g. bpy.ops.reg.op()
    bl_label = 'Register Operator'  # Operator name
    bcp_return_result = 0           # Operator class variable

    def execute(self, context):     # to work as an operator this method must be
                                    # called "execute" and have self and context
                                    # arguments

        op = bpy.types.REG_OT_op    # to access an operator as a class we type:
                                    # "bpy.types", add uppercased start of 
                                    # bl_idname, add _OT_ instead of dot and the
                                    # final part of bl_idname as it is
                                    # Example: 'reg.op' = 'REG_OT_op'

        op.bcp_return_result = 1    # change operator class variable with what
                                    # you need

        return {'FINISHED'}         # also necessary part of operator

register_class(RegisterOperator)    # register operator

op = bpy.types.REG_OT_op            # set variable with operator class

print(op.bcp_return_result)         # >>>0
bpy.ops.reg.op()                    # execute operator
print(op.bcp_return_result)         # >>>1

or just a common Python class:

class RegisterClass():
    bcp_return_result = 0           # Operator class variable

    def __init__(self):
        pass

reg = RegisterClass                  # create class instance

print(reg.bcp_return_result)         # >>> 0
reg.bcp_return_result = 1            # change class variable
print(reg.bcp_return_result)         # >>> 1
$\endgroup$
1
  • $\begingroup$ Thanks, I did not implement that yet, but it seems to be a comprehensive answer $\endgroup$ Mar 26 '20 at 17:45

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.