2
$\begingroup$

I made a small script that is supposed to select objects that have a material with an Image Texture.

import bpy

scene = bpy.context.scene
bpy.context.view_layer.objects

#first make sure everything is unselected
for obj in bpy.data.objects:
    obj.select_set(False)

for ob in bpy.data.objects:
    if ob.type == "MESH": #select only meshes
        for mat_slot in ob.material_slots:
            # if mat_slot.material:
            if mat_slot.material.node_tree:
                # print("material:" + str(mat_slot.material.name))                
                for x in mat_slot.material.node_tree.nodes:
                    if x.type=='TEX_IMAGE':
                        ob.select_set(True) #select if object has texture

When running the script for a particular scene it gives me the following error:

RuntimeError: Error: Object 'pape01t.002' can't be selected because it is not in View Layer 'ViewLayer'!

After some digging I found the objects in current file collection

enter image description here

There is no option to turn them on (visible) so

  1. How come these objects are in the current file collection but not visible in the actual object list [view layer] or [scene collection]?
  2. How would I be able to ignore such objects or even better, find these hidden objects and delete them by script?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

Instead of using bpy.data.objects, use bpy.context.scene.objects to make sure you only loop through the objects that are currently in your active scene. The difference between these is explained in this thread

import bpy

for o in bpy.context.scene.objects:
    o.select_set(False)
    if o.type != "MESH":
        continue
    for mat_slot in o.material_slots:
        for x in mat_slot.material.node_tree.nodes:
            if x.type == 'TEX_IMAGE':
                o.select_set(True)

If you go under Orphan Data (broken heart symbol) under the Outliner, you will notice that there is a shield icon next to Collection which means there is a Fake User referencing all the data blocks within this collection that is why it didn't get removed when you did a File > Clean Up. Just click that Fake User icon to remove the fake user and then when doing a File > Clean Up it will remove these data blocks as well.

enter image description here

$\endgroup$
2
  • $\begingroup$ First of all, thank you for the elaborate reply. The script works perfect. First time I see 'continue' used so ill look that up. As for unused data blocks. In my case I can only remove it with [Unused Local Data Blocks] and not [Unused Data Blocks]. In fact multiple times. First it removes the data from Collections and moves it into a Objects tab, after that I click again and it removes only 2. So I have to click the button multiple times before it gets removed. I assume that is not normal? $\endgroup$ Commented Jul 14, 2022 at 23:12
  • $\begingroup$ @JeeperCreeper continue can help reduce excessive nesting of code. thats odd coz i did Unused Data Blocks and Recursive Unused Data Blocks and it was clean. $\endgroup$
    – Harry McKenzie
    Commented Jul 15, 2022 at 1:06

You must log in to answer this question.

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