0
$\begingroup$

I need to get list of mesh objects in collection and ignore excluded collections inside:

enter image description here enter image description here

import bpy

all_visible_meshes = []

for obj in bpy.data.collections["Collection"].all_objects:
        if obj.type == "MESH" and obj.hide_viewport == False:
            all_visible_meshes.append(obj)

print(all_visible_meshes)

Result: [bpy.data.objects['Cube'], bpy.data.objects['Sphere']]

I need only [bpy.data.objects['Cube']]

$\endgroup$
2
  • 1
    $\begingroup$ Hello ! Have you explored bpy.context.selectable_objects ? eg if obj not in bpy.context.selectable_objects: continue $\endgroup$
    – Gorgious
    Commented Dec 20, 2022 at 10:50
  • $\begingroup$ Hello. Edited: Thanks! $\endgroup$
    – APEC
    Commented Dec 20, 2022 at 10:52

1 Answer 1

2
$\begingroup$
import bpy

collection_objects = set(bpy.data.collections["Collection"].all_objects)
visible_objects = set(bpy.context.visible_objects)
visible_objects_in_collection = collection_objects.intersection(visible_objects)
print(visible_objects_in_collection)

Be advised that it will create a set so member access is not deterministic. You can convert it to a list.

visible_objects_in_collection = list(visible_objects_in_collection)
print(visible_objects_in_collection)

Link to the docs

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .