After creating a collection with object.link_to_collection, how do you get the collection that was created? it’s an operator, so the return type is just a status string. the collection type doesn’t have an index. the list of collections in bpy.data.collections is just an unordered prop_collection. bpy.context.collection doesn’t change until the user actively selects the collection in the outliner. I could try to find it by name but there’s no guarantee the name is what I called it in the operator because it could have been auto-renamed due to a collision.
I’m at a loss as to what we’re supposed to do with this on the addon side of things. The only thing I can think of is to either somehow scrape the string from INFO output and parse the name out of it, or make a list of all the collections before I make mine and then diff it to find the one that I made. As an example- this works, but uhhh… feels super wrong.
before = [c.name for c in bpy.data.collections]
bpy.ops.object.link_to_collection(collection_index=0, is_new=True, new_collection_name=collection_name)
diff = list(set([c.name for c in bpy.data.collections]) - set(before))
new_collection_name = diff[0]
As a tangentially related side note- any reason why the Scene Collection is called “Master Collection” in bpy.data.collections? Seems like the name got changed on the user-facing side of things and never updated… should this be logged as a bug?