Can add methods to the propertygroup definition
Something that is very handy IMO is the ability to add methods to the property group.
Here I've added copy to make a copy of itself to the collection, and add to add an object to the collection. In both cases the object name is used as the collection item name.
Remember that 2.8 uses annotations for properties.
import bpy
class ObjectCopy(bpy.types.PropertyGroup):
object: bpy.props.PointerProperty(type=bpy.types.Object)
def copy(self):
self.object = self.id_data.copy()
self.name = self.object.name
return self.object
def add(self, ob):
self.object = ob
self.name = ob.name
return self.object
def register():
bpy.utils.register_class(ObjectCopy)
bpy.types.Object.copies = bpy.props.CollectionProperty(type=ObjectCopy)
def unregister():
bpy.utils.unregister_class(ObjectCopy)
del(bpy.types.Object.copies)
if __name__ == "__main__":
register()
Some testing in the python console.
>>> C.object.copies.clear()
>>> for o in C.selected_objects:
... C.object.copies.add().add(o)
...
bpy.data.objects['Cube.002']
bpy.data.objects['Cube.001']
bpy.data.objects['Cube']
>>> C.object.copies.add().copy()
bpy.data.objects['Cube.005']
>>> C.object.copies['Cube'].object
bpy.data.objects['Cube']