I am working on a project that requires each object to have a list of custom class instances, so that I can get and set things from the list as needed.
I tried to do something like this (the code is on the end of the topic), but it gave me the following error when I tried to run the add_to_list
operator: TypeError: only floats, ints and dicts are allowed in ID property arrays
I guess I should find a way to change it to another type, like CollectionProperty, but I couldn’t do it.
I would appreciate it if you could help me.
Code (all registration is done outside these scripts automatically and surely):
script 1:
class list_item:
global_list_id = "list_id"
name: bpy.props.StringProperty()
texture: bpy.props.PointerProperty(type=bpy.types.Image)
def __init__(self, name, texture):
self.name = name
self.texture = texture
@staticmethod
def get_or_create_list(obj, context):
if list_item.global_list_id not in obj:
obj[list_item.global_list_id] = []
return obj[list_item.global_list_id]
@staticmethod
def append_to_object(obj: bpy.types.Object, append, context: bpy.context):
items_list = list_item.get_or_create_list(obj, context)
items_list.append(append)
obj(list_item.global_list_id] = items_list
return list_item.get_or_create_list(obj, context)
script 2:
import bpy
from ..utility.list_item import list_item
class add_to_list_operator(bpy.types.Operator):
bl_idname = "addon.add_to_list"
item_name : bpy.props.StringProperty(name="Name", default="New Item")
def execute(self, context):
obj = context.active_object
print(f"Operator executed with args: {self.item_name}")
new_image = bpy.data.images.new(name=self.texture_name, width=1920, height=1080)
new_item = list_item(self.texture_name, new_image)
list_item.append_to_object(obj=obj, append=new_item, context=context)
return {'FINISHED'}