1

I'm trying to do something similar as in this post, but instead of all dimensions from each object I want to know to which collection they belong.

The part of the code I changed was:

selection = bpy.context.selected_objects

iterate through the selected objects

for sel in selection: # get collection to which the object belongs col = bpy.context.collection # write the selected object's name and dimensions to a string result += "%s, %s" % (sel.name, col.name)

However, this seems to loop over the collections that are inside of the selected objects instead of the other way around. How do I change my code to see the collection the objects belongs to?

Laura
  • 72
  • 5
  • Pretty close but the collection name would be the same for all objects (context.collection is the active one). How to get the collection of the object: https://blender.stackexchange.com/a/134789/31447 – brockmann Jul 23 '21 at 15:32
  • That works, thank you! – Laura Jul 23 '21 at 15:49

1 Answers1

0

check this out:

import bpy

for eachCol in bpy.data.collections:

print("Collection:", eachCol.name)

for eachObject in eachCol.objects:

    print("  ", eachObject.name)

Test result:

enter image description here

or if you only want the info about the selected objects:

import bpy

selected = bpy.context.selected_objects

for eachSel in selected:

for eachCol in bpy.data.collections:
    for eachObject in eachCol.objects:

        if eachObject == eachSel:

            print(eachObject.name, "is in", eachCol.name)

test result:

enter image description here

Chris
  • 59,454
  • 6
  • 30
  • 84