1
$\begingroup$

I'd like to hide and unhide several objects at once. In particular, I'd like to toggle the eye icon on and off. The monitor icon works fine, but the eye icon doesn't work.

import bpy
obj_list = ["Cone","Cube","Cylinder","Sphere"]
objects = bpy.data.objects
for obj_name in obj_list:
    if objects[obj_name].hide_set:
        objects[obj_name].hide_set(False)
    else:
        objects[obj_name].hide_set(True)

enter image description here

Q: How can I control the visibility of e.g. Cube, Cone and Sphere?

$\endgroup$
0
4
$\begingroup$

In order to get the visibility state per object (eye), you'd have to use Object.hide_get().

Also I'd suggest iterate through the objects of the current scene in context in this case (C.scene.objects) rather than all objects of the current blend file (D.objects).

import bpy

C = bpy.context
candidates = ("Cube", "Camera")

# Toggle the visibility state (eye icon) per object
for obj in [o for o in C.scene.objects if o.name in candidates]:
    if obj.hide_get():
        obj.hide_set(False)
    else:
        obj.hide_set(True)

Viewport visibility (monitor) is just a bool hence one can use the xor operator to toggle:

# Toggle viewport visibility state (monitor icon) per object
for obj in [o for o in C.scene.objects if o.name in candidates]:
    obj.hide_viewport ^= 1
$\endgroup$
8
  • 1
    $\begingroup$ I learned quite a bit from your answer. Thanks a lot! $\endgroup$
    – minia
    Sep 13 '21 at 1:38
  • 1
    $\begingroup$ @minia Possibly the most important part is if an object has a method ob.some_method() then if ob.some_method: will always be true. ... this being the basic issue with the question. Will (for the last time) recommend debugging first in python console eg swap true and false or print some values. $\endgroup$
    – batFINGER
    Sep 13 '21 at 10:25
  • 1
    $\begingroup$ @brockmann or without the if obj.hide_set(not obj.hide_get()) also suggest this for obj in filter(None, (scene.objects.get(name) for name in candidates)): will be way more efficient. Found about 5 dupes for this, think I pasted a lesser. If not Ok with close give heads up and will re-open. $\endgroup$
    – batFINGER
    Sep 13 '21 at 13:47
  • 1
    $\begingroup$ @batFINGER Just a minor detail: Don't you think using a generator like (o for o in C.scene.objects if o.name in candidates) is equal to filter(None, (C.scene.objects.get(name) for name in candidates)), makes it more readable... Apparently a rabbit hole and Mr. Architect even considered to remove the filter(): stackoverflow.com/questions/3013449/… $\endgroup$
    – brockmann
    Sep 14 '21 at 7:22
  • 1
    $\begingroup$ for the OP both our methods won't throw a key error on a dud candidate name. $\endgroup$
    – batFINGER
    Sep 14 '21 at 9:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.