The Batch Rename feature of the blender's Outliner does not appear to work for the collection, so I want to write a script myself. There are a lot of heavy mesh in the collections, so I'd like to change the name while excluded. Is there a way to find out the selected excluded collections list like the attached image by Python ?
1 Answer
There is no direct way to get the collections in selection neither any collection attribute to get the exclude state (harder as you might think). For now, the only thing you can get for free is all items in selection by using Context.selected_ids
(undocumented for now).
Notice that the context of selected_ids
attribute is restricted to the Outliner so you'd have to implement an Operator. I'd suggest use the bl_rna.identifier
attribute to test against the actual type of struct and filter all selected collections based on that, then get the ViewLayer
in context to figure out whether each selected collection is excluded/disabled:
import bpy
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "outliner.simple_operator"
bl_label = "Simple Outliner Operator"
@classmethod
def poll(cls, context):
return context.area.type == 'OUTLINER'
def execute(self, context):
#print (context.selected_ids)
selected_collections = [c for c in context.selected_ids if c.bl_rna.identifier == "Collection"]
for c in selected_collections:
l = context.view_layer.layer_collection.children.get(c.name)
if l.exclude:
print (c.name)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
if __name__ == "__main__":
register()