Operators for globally hiding objects in all project scenes

To hide an object in the 3D Viewport window for the current scene we use the “object.hide_view_set” operator, which calls when we clicking the icon with an “eye” in the outliner. However, to hide the object in the viewport for all scenes of the project at once (clicking on the icon with the “monitor” image) a special operator is not provided. We can make it ourselves.

 

An object property is used for showing or hiding it in the viewport globally for all scenes in the project is the following:

Let’s create an operator class to change this property of an object:

And register it in the Blender Python API:

Now calling this operator the currently active object will be hidden in the viewport for all scenes in the project.

To do this, just type in the Python console:

To make this more convenient, we can set up a key combination to call it. Open the Preferences window and switch to the KeyMap tab. In the drop-down menu open the section “3D View – Object Mode – Object Mode (Global)” and press the “New” key to create a new keymap.

In the operator field, type “object.hide_viewport”, and assign a convenient keyboard shortcut to invoke it, for example, the “Y” key.

Now when we press the “Y” key in the 3D Viewport window, the active object becomes hidden for all scenes of the project.

To hide not only the active object but all the selected objects in the scene, which is much more convenient to use, let’s slightly change the code.

Instead of changing the “hide_viewport” property only for the active object, we will loop through all the selected objects and change this property for each of them.

Replace the line

with the following code:

That’s all, now we will hide all selected objects with our operator.

Let’s create one more operator that will perform the opposite function – return visibility for all objects hidden for all scenes.

We can named it “hide_viewport_clear”:

It also needs to be registered in the API with the “register_class” function.

Also, let’s set up a quick call for it with a keyboard shortcut, for example, “alt + Y”, as we do earlier for the “hide_viewport” operator:

Full code:


*.blend file with example and *.py file with code for my Patreon subscribers

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comment
Inline Feedbacks
View all comments