I am trying to pass a collection as parameter of an operator, but I have following issue :
AttributeError: bpy_struct: attribute "collection" from OBJECT_OT_example_op" is read-only
Here is my example code :
import bpy
class Item(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name="Name")
class ExamplePanel(bpy.types.Panel):
bl_label = "ExamplePanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Example"
@classmethod
def poll(self, context):
return True
def draw(self, context):
layout = self.layout
layout.operator("object.example_op", text="Test").collection = bpy.context.scene.collection
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)
return {'FINISHED'}
def register():
bpy.utils.register_class(Item)
bpy.types.Scene.collection = bpy.props.CollectionProperty(type=Item)
it = bpy.context.scene.collection.add()
it.name = "item1"
it = bpy.context.scene.collection.add()
it.name = "item2"
bpy.utils.register_class(ExampleOperator)
bpy.utils.register_class(ExamplePanel)
if __name__ == "__main__":
register()
Any suggestions ? Tried with PointerProperty, but don't know exactly how to use it, and got same error.
Thanks :)
for item in self.collection
tofor item in context.scene.collection
ie use the scene collection in the operator. You can however usebpy.ops.object.example_op(coll=[{"name":"Goose"}, {"name":"Herman"}])
to pass the collection to the operator...coll=scene.collection
will throw an error too. $\endgroup$