Adding new objects from Python while in local-view


#1

In 2.8 how should addons add new objects while the user is in local-view mode and have them show up immediately?

In 2.7x this was accomplished by calling layers_from_view on the newly added object: https://docs.blender.org/api/current/bpy.types.ObjectBase.html?highlight=layers_from_view#bpy.types.ObjectBase.layers_from_view

Example Scenario:

  • User is focused on a particular object in local-view
  • User invokes an addon to wrap the selected object in a new Lattice object
    lattice = bpy.data.lattices.new('dc_lattice')
    lattice_object = bpy.data.objects.new('dc_lattice', lattice)
  • This new lattice object should be visible in local-view immediately (as-if the user had selected both objects before going into local-view)

The api no longer seems to exist and I’m unable to find an equivalent when searching. Should the addon manually go in/out of local-view mode and perform the selection itself?

Note: Objects added by calls to bpy.ops.mesh.primitive_* are shown in local view immediately. Somehow, intentionally or not, they are handling this mode (which is a good thing™)


#2

Well for now I’m doing the following inside the op:

    # Toggle out of local-view mode and re-enter to ensure lattice shows up
    if context.space_data.local_view is not None:
        bpy.ops.view3d.localview()
        lattice_object.select_set(True)
        bpy.ops.view3d.localview()