I was working on an animation project. When I was done modelling the scene and objects in it I started working on its animation. I started by hiding everything and only leaving the objects that I was animating at the moment. I keyframed these object visibility in render for my animation but I leave their visibility in the viewport to be checked. At the time I decided to don't hide them in the viewport so I could find and select them through viewport instead of using object outliner. But now that I have progressed through the animating process my scene is a mess and I can't get a proper preview of animation without rendering. here's my question: Is there a way to set up the scene so every object's viewport visibility just be the same as its render visibility and also follow its changes? I'm pretty good at Python programming though I have never written a script in Blender. But even if the solution includes writing scripts it is also a solution and acceptable.
Just add a driver to your viewport visibility like this:
Then copy the driver and paste it to every object you need. After each copy just adapt the new pasted driver in this way: change the object (in my case cube) to that object you just added the driver to.
… but better is @batFINGER solution here: How can you make it automatically invisible to render if its already invisible in viewport?