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:
1 2 |
object.hide_viewport # False |
Let’s create an operator class to change this property of an object:
1 2 3 4 5 6 7 8 9 10 11 |
from bpy.types import Operator class OBJECT_hide_viewport(Operator): bl_idname = 'object.hide_viewport' bl_label = 'Hide viewport' bl_description = 'Globally disable in viewport' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): context.object.hide_viewport = True return {'FINISHED'} |
And register it in the Blender Python API:
1 2 3 4 5 6 7 |
from bpy.utils import register_class, unregister_class def register(): register_class(OBJECT_hide_viewport) if __name__ == '__main__': register() |
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:
1 |
bpy.ops.object.hide_viewport() |
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
1 |
context.object.hide_viewport = True |
with the following code:
1 2 |
for obj in context.selected_objects: obj.hide_viewport = True |
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”:
1 2 3 4 5 6 7 8 9 10 11 |
class OBJECT_hide_viewport_clear(Operator): bl_idname = 'object.hide_viewport_clear' bl_label = 'Clear viewport hide' bl_description = 'Globally cler hiding in viewport' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): for obj in context.blend_data.objects: if obj.hide_viewport: obj.hide_viewport = False return {'FINISHED'} |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import bpy from bpy.types import Operator from bpy.utils import register_class, unregister_class class OBJECT_hide_viewport(Operator): bl_idname = 'object.hide_viewport' bl_label = 'Hide viewport' bl_description = 'Globally disable in viewport' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): for obj in context.selected_objects: obj.hide_viewport = True return {'FINISHED'} class OBJECT_hide_viewport_clear(Operator): bl_idname = 'object.hide_viewport_clear' bl_label = 'Clear viewport hide' bl_description = 'Globally cler hiding in viewport' bl_options = {'REGISTER', 'UNDO'} def execute(self, context): for obj in context.blend_data.objects: if obj.hide_viewport: obj.hide_viewport = False return {'FINISHED'} def register(): register_class(OBJECT_hide_viewport) register_class(OBJECT_hide_viewport_clear) def unregister(): unregister_class(OBJECT_hide_viewport_clear) unregister_class(OBJECT_hide_viewport) if __name__ == '__main__': register() |
*.blend file with example and *.py file with code for my Patreon subscribers