SOLVED Visibility Toggle

I'm adding a script, added as a valuable shortcut in my everyday workflow here :

It basically allows you to select any object, and assigned to the letter H, Hide or Reveal your selected object. Like in Maya. Very Useful.

Not sure it is the best way to do it, that's why I'm posting it here.

import c4d
from c4d import gui

def main():

    allobj = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    obj = object()
    for obj in allobj:
        editor = obj[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR]
        render = obj[c4d.ID_BASEOBJECT_VISIBILITY_RENDER]

        if editor == 1 or render == 1:
            newstate = 2
        else:
            newstate = 1

        obj[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = newstate
        obj[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = newstate


if __name__=='__main__':
    main()
    c4d.EventAdd()

The line

obj = object()

is useless.

And to handle editor and render visibility, can can use dedicated functions: GetEditorMode(), GetRenderMode(), etc. and the explicit values c4d.MODE_ON, c4d.MODE_OFF, and c4d.MODE_UNDEF.

See also the C++ docs: BaseObject Manual

hi,
thanks @PluginStudent 🙂

you can do shorter by doing a module (%) and add one. (instead of 0-1 you will return 1-2)
Also obj = object() is not necessary as you define it in the loop after.

import c4d
from c4d import gui

def main():

    allobj = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    for obj in allobj:
        obj[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] = obj[c4d.ID_BASEOBJECT_VISIBILITY_EDITOR] % 2 + 1
        obj[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] = obj[c4d.ID_BASEOBJECT_VISIBILITY_RENDER] % 2 + 1


if __name__=='__main__':
    main()
    c4d.EventAdd()

or

import c4d
from c4d import gui

def main():

    allobj = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    for obj in allobj:
        obj.SetEditorMode(obj.GetEditorMode() % 2 + 1)
        obj.SetRenderMode(obj.GetRenderMode() % 2 + 1)


if __name__=='__main__':
    main()
    c4d.EventAdd()

Cheers,
Manuel

hi,

without any feedback from you, I'll set this thread as solved tomorrow

Cheers,
Manuel