"""
Collection of functions to manage blender collections
"""
import bpy
[docs]def collection(name, parent=None, children=None):
"""Creates a new collection or greps an existing one if
a collection with given name exists.
If the collection already exists the remaining parameters are ignored.
Parameters
----------
name : str
``.name`` of the collection.
parent : bpy.types.Collection (default=None)
Parent collection to link newly generated collection to.
If ``None``\, collections get linked to bpy.context.scene.collection.
children: list of strings or lists (default=None)
``.name`` of children that will be generated when generating a new collection.
If an entry is a list, the first string will be the child's ``.name`` and the others
the child's children's ``.name``\s.
Returns
-------
bpy.types.Collection
Reference to the created Blender collection.
"""
if name in bpy.data.collections:
col = bpy.data.collections[name]
else:
col = bpy.data.collections.new(name)
if parent is None:
parent = bpy.context.scene.collection
parent.children.link(col)
if children:
for child in children:
if type(child) is str:
collection(child, parent=col)
else:
collection(child[0], parent=col, children=child[1:])
return col