0
$\begingroup$

is there a way to detect what view global or local is active via python?

Can I set them directly somehow or at least always come back to global one? In a way where if script is executed view is moved to global no matter what view user is in.

$\endgroup$
1

4 Answers 4

2
$\begingroup$

Here is a trick:

import bpy
is_local_view = sum(bpy.context.space_data.layers[:]) == 0

In "local_view" mode all layers disabled, while in "regular" mode one of them always enabled.

$\endgroup$
4
$\begingroup$

It can be done this way:

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        space = area.spaces[0]
        if space.local_view: #check if using local view
            for region in area.regions:
                if region.type == 'WINDOW':
                    override = {'area': area, 'region': region} #override context
                    bpy.ops.view3d.localview(override) #switch to global view
$\endgroup$
1
  • $\begingroup$ Yes. It works. When I use bpy.ops.render.render(write_still=True, use_viewport=True), I still got images containing all objects. $\endgroup$
    – Haozhe Xie
    Sep 24, 2019 at 8:32
3
$\begingroup$

This check is using the context, assuming you are in the 3D view already, you can then exit with localview op;

if context.space_data.local_view:
    bpy.ops.view3d.localview()
$\endgroup$
1
  • $\begingroup$ AttributeError: 'SpaceConsole' object has no attribute 'local_view' in 2.79 $\endgroup$
    – Haozhe Xie
    Sep 24, 2019 at 8:28
1
$\begingroup$

Thanks to Юрий Сивальнев, I searched the API a little more. the Object type has a property "layers_local_view." It is an eight memeber list, all false if the object is not in local view. If you know a particular object you would like to check in the local view space, then you can use this property.

https://docs.blender.org/api/blender_python_api_current/bpy.types.Object.html?highlight=local_view#bpy.types.Object.layers_local_view

def is_local(ob): if ob.layers_local_view[0]: return True else: return False

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.