0
$\begingroup$

In Blender 2.7x in the init.py file a generic class was instantiated for the purpose of holding data:

class HoldData(): 
    dat = None       

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_data = HoldData()

def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_data

In a subsequent file an outside python class is instantiated within the generic class, in this case a Pandas Dataframe:

import bpy
import pandas as pd

class OP_DoWork(bpy.types.Operator):
    bl_idname = "op.dowork"
    bl_label  = "Doing Work"

def execute(self, context):
    return{'FINISHED'}

def invoke(self, context, event):
    dat = bpy.context.scene.my_data
    dat.bh = pd.DataFrame()

Finally, the object (the Pandas Dataframe) is accessible to other classes within Blender:

class OP_DataCheck(bpy.types.Operator):
    bl_idname = "op.datacheck"
    bl_label = "Check Data"

    def __init__(self):
        self.dat = None

    def execute(self, context): 
        dat = bpy.context.scene.my_data
        print(dat.bh.columns)

In 2.8x the registration of the original HoldData() class throws an error:

class HoldData(bpy.types.Anytype):
AttributeError: 'RNA_Types' object has no attribute 'Anytype'

How can I save a class instance to the scene in Blender 2.8x?

$\endgroup$

1 Answer 1

2
$\begingroup$

This is what is often referred to as a monkey patch. Data will not be saved to blend file.

To monkey patch onto a scene type could simply

import bpy

class HoldData: 
    dat = None       

def register():
    bpy.types.Scene.my_data = HoldData()

def unregister():
    del bpy.types.Scene.my_data
    
if __name__ == "__main__":
    register()

Test in console

>>> C.scene.my_data.dat = "foo"
>>> C.scene.my_data.dat
'foo'

In as much as can do this, would favour simply

from foo import HoldData

my_data = HoldData()

and

from bah import my_data

To save some data to blender and use a third party for other recommend a PropertyGroup and PointerProperty set up. Not all members of the defined property group need be annotated properties.

The other issue mentioned is a typo, the class is bpy.types.AnyType (CamelCase ie capital T for type) Not sure where you're going with this either?

$\endgroup$
1
  • $\begingroup$ I should have been more clear. My last code snippet was a copy of the error displayed in the console when loading the add-on. Thanks for the answer- I've been beating my head on the wall for two days over this! $\endgroup$
    – joswhite
    Aug 12, 2020 at 12:42

You must log in to answer this question.

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