0
$\begingroup$

Maybe the question is dumb, but (appart of IntVectorProperty) I didn't found anything in the API, so I was wondering about the best way to make a simple list/array.

I tried:

my_list_of_ints:CollectionProperty(type=IntProperty)

But says it cannot be registered.

IntVectorProperty is not useful in my case since I need the list to be dynamic (API says that the vector must be of a predefined size).

So I was wondering about the best approach of making that list of ints.

Edit: the context is to have that array/list of ints inside a class that inherits PropertyGroup.

class ObjectA(PropertyGroup):
    int_list:list[int] # This gives error (says property dont exist when I access it)
    int_list:list[int] = [] # This dont give an error, but the data is not saved
    int_list:IntVectorProperty() # I cant use that since the vector is not dynamic

Thank you so much for the help.

$\endgroup$
17
  • $\begingroup$ What's the context? What is the property for? Operator? Data-block? $\endgroup$ Commented Sep 12, 2023 at 10:41
  • $\begingroup$ @MartynasŽiemys a data block (I want the list to be saved with the file (like others bpy.props)) $\endgroup$
    – Ommadawn
    Commented Sep 12, 2023 at 12:17
  • 1
    $\begingroup$ Just assign it a value: bpy.context.scene["someProperty"] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] $\endgroup$ Commented Sep 12, 2023 at 12:28
  • $\begingroup$ @MartynasŽiemys thank you so much! I didn't know that was possible. In my case (sorry for not explaining it properly, I'm a bit of a newbie with add-ons) I want that every instance in a Collection Property (from a Property Group) have it's own array. Do you know if that would be possible? (I think I may could do something similar; my_object["my_array"] = [1,2,3,4...], but in that case would it be saved with the blend file? $\endgroup$
    – Ommadawn
    Commented Sep 12, 2023 at 13:41
  • $\begingroup$ They are supposed to be saved as custom properties, so yes, any data block properties should be saved with the file. It should be easy to test as well, just save, reopen and check. $\endgroup$ Commented Sep 12, 2023 at 14:30

1 Answer 1

1
$\begingroup$

CollectionProperty is what you need. First define your integer property class like so:

class MyIntProp(bpy.types.PropertyGroup):
    name: bpy.props.StringProperty(default="")
    value: bpy.props.IntProperty(default=0)

and register it:

bpy.utils.register_class(MyIntProp)

then define you collection:

my_array: CollectionProperty(type=MyIntProp)

now you can add elements to it:

first = self.my_array.add()
first.name = "first"
first.value = 1
$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – Ommadawn
    Commented Sep 13, 2023 at 9:15

You must log in to answer this question.

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