3
$\begingroup$

I have some automated tests setup for one of my add-ons. However, one of my operators initializes its own collection property parameter upon invoke() - which doesn't get called when running an operator from a script. How do I initialize and set the collection property parameters for a given execution?

Here is example code, derivative of Blender API : pass collection as parameter of python operator:

import bpy

class Item(bpy.types.PropertyGroup):
    is_true: bpy.props.BoolProperty()

class ExampleOperator(bpy.types.Operator): 
    bl_idname = "object.example_op"
    bl_label = "Example Operator"
    bl_options = {'REGISTER'}   

    collection: bpy.props.CollectionProperty(type=Item)

    @classmethod
    def poll(self, context):
        return True

    def execute(self, context):
        for item in self.collection:
            print(item.name, item.is_true)
        return {'FINISHED'}

def register():
    bpy.utils.register_class(Item) 
    bpy.utils.register_class(ExampleOperator)


if __name__ == "__main__":
    register()

Any suggestions ? Tried with PointerProperty, but don't know exactly how to use it, and got same error. Thanks :)

$\endgroup$
2
  • 1
    $\begingroup$ FWIW you can execute the invoke method of an operator from code with bpy.ops.object.example_op("INVOKE_DEFAULT") see docs.blender.org/api/current/bpy.types.Operator.html $\endgroup$
    – Gorgious
    Aug 13, 2023 at 17:00
  • $\begingroup$ True, you can. In my case, I wanted to change the default values in the collection properties to test different situations. $\endgroup$ Aug 27, 2023 at 16:51

1 Answer 1

2
$\begingroup$

You can pass a dict which the operator will use to initialize the collection parameter. Here's how I called the operator in my script:

import bpy

bpy.ops.object.example_op(
    collection=[{'name': 'first_key', 'is_true': True},  {'name': 'second_key', 'is_true': False}]
)
$\endgroup$
2
  • $\begingroup$ Wow this is a very hidden, obscure and awesome feature !! May I ask where you found this information ? I don't remember seeing this anywhere in the docs or else. Trial and error ? $\endgroup$
    – Gorgious
    Aug 13, 2023 at 17:09
  • 1
    $\begingroup$ From this comment by batFINGER: blender.stackexchange.com/questions/48513/… It didn't end up being what the user wanted, but it saved me time. $\endgroup$ Aug 27, 2023 at 16:53

You must log in to answer this question.

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