6
$\begingroup$

Is it possible to listen to changes in object visibility via a Python add-on in Blender?

Being a beginner, I only know of listing all objects and doing obj.hide_get(), but I at least know that doing that in a loop is very bad.

$\endgroup$
3
  • 2
    $\begingroup$ The next less bad thing would be a modal timer operator (maybe with 0.2s). The probably best option would be the depsgraph_update_post handler. $\endgroup$
    – Leander
    Sep 30, 2020 at 7:39
  • $\begingroup$ Depending on what you want to do with the information, ie modify another object's properties, you could use a driver $\endgroup$
    – Gorgious
    Sep 30, 2020 at 7:40
  • $\begingroup$ maybe this can help which uses depsgraph_update_post blender.stackexchange.com/questions/283247/… $\endgroup$ Jan 9, 2023 at 9:16

1 Answer 1

2
$\begingroup$

I had a similar query and here's the updated script that will print out a check of what has changed in visibility using depsgraph_update_post application handler:

import bpy

def on_visibility_change(obj):
    print("visiblity changed for object:", obj.name)

def on_depsgraph_update(scene):
    global previous_visible_states
    states = set(o.name for o in bpy.context.scene.objects if o.hide_viewport or o.hide_render or o.hide_get())
    for obj in bpy.context.scene.objects:
        changed = obj.name not in states and obj.name in previous_visible_states
        if changed or (obj.name in states and obj.name not in previous_visible_states):
            on_visibility_change(obj)
    previous_visible_states = states

bpy.app.handlers.depsgraph_update_post.clear()
bpy.app.handlers.depsgraph_update_post.append(on_depsgraph_update)
previous_visible_states = set(o.name for o in bpy.context.scene.objects if o.hide_viewport or o.hide_render or o.hide_get())
$\endgroup$

You must log in to answer this question.

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