# How to track an unknown number of objects in a multi-module add-on

Trying to initialize an empty list that will later be manipulated by operators on button click (add, remove, get). The list can either be populated with objects or object location tuples.

Pseudocode

Ideally the code would look something like this:

# __init__.py
bpy.types.Scene.prop_name = bpy.props.list(
items = []
)

# some_operator.py
bpy.types.Scene.prop_name.append(obj.location)


Likely Solutions

Unless there's some methodology outside of bpy.props, EnumProperty or CollectionProperty are likely places to start, but no luck so far. Worst case scenario, I could use a string, then convert it later into tuples - but that seems excessively abstracted from the original goal.

## 2 Answers

@batFINGER suggested using a custom object property instead of maintaining a list - which is a good alternative for my use case and avoids bpy.props entirely. Below are implementations written with the help of devtalk.

Adding is fairly straightforward.

# add an object to list
obj["prop_name"] = 1


Getting a list can be done in a couple different ways depending on how unassignment is handled - adjusting the prop's value...

# remove an object from list
obj["prop_name"] = 0
# get list of objects
objs_list = [
obj for obj in bpy.data.objects
if "prop_name" in obj and obj["prop_name"] == 1
]


... or deleting it entirely.

# remove an object from list
del obj["prop_name"]

# get list of objects
objs_list = [
obj for obj in bpy.data.objects
if "prop_name" in obj
]


Either way, these 'get list' implementations avoid iterating through the full list of objects.

Update: Ended up using the other solution.

Other implementations were mentioned on devtalk, but bpy.props.StringProperty was quickest to implement.

# __init__.py
bpy.types.Scene.protected_object_names = bpy.props.StringProperty(
name = "Protected Objects",
description = "Objects that shouldn't be deleted."
)
# this prevents an unassigned error that bpy.props.StringProperty.default fails to solve
bpy.types.Scene.protected_object_names = ""

# persist_protected_object_names.py
for selected_index, selected_object in enumerate(bpy.context.selected_objects):
selected_object_names.append( selected_object.name )
obj_names_string = ','.join(selected_object_names)
bpy.types.Scene.protected_object_names = obj_names_string

# some_operator.py
protected_object_names = bpy.types.Scene.protected_object_names.split(",")

• Wouldn't do this. Can't see the reason to use a class property as a variable. and redefine it from bpy prop to list to string ... for a bpy.props defined class prop It's the instance eg context.scene.foo that is generally used in operators etc. This could be from my_addon import my_list ... or ... Could simply tag any object with a custom property ob["protected"] = True Oct 25 '19 at 1:24
• Tagging is a great idea. When I attempted importing, the list didn't maintain state across multiple files. Maybe you could post a solution. Oct 25 '19 at 15:33