1
$\begingroup$

I have defined an Operator class which has a CollectionProperty like this:

class LinkMakeProxy(bpy.types.Operator):
    ...
    my_list = bpy.props.CollectionProperty(type=BoolProperty)
    ...

First, the type of my_list is a tuple:

(<built-in function CollectionProperty>, {'type': <built-in function BoolProperty>})

which I am having trouble wrapping my head around, since I expected some mutable data-structure with data rather than functions.

My question is, how can I access and update this property from the exec method of the operator? Similar to what is done in the example given here.

$\endgroup$
2
$\begingroup$

The type parameter of CollectionProperty is expecting a subclass of bpy.types.PropertyGroup. So you would need something like:

class MyBool(bpy.types.PropertyGroup):
    bool = BoolProperty()

Then in your operator, you use that type in your CollectionProperty:

my_list = CollectionProperty(type=MyBool)

You add to your collection by using add():

mybool = self.my_list.add()

...which gives you an instance of the PropertyGroup and the properties it contains. Here is an example that makes a list of booleans based on the selected vertices in the active object and prints the list:

import bpy
from bpy.props import CollectionProperty, BoolProperty

class MyBool(bpy.types.PropertyGroup):
    bool = BoolProperty()

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    my_list = CollectionProperty(type=MyBool)

    def execute(self, context):
        obj = context.active_object
        for vert in obj.data.vertices:
            mybool = self.my_list.add()
            mybool.bool = vert.select
        for b in self.my_list:
            print(b.bool)
        return {'FINISHED'}

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

register()
$\endgroup$

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.