Decoupling x-ray and limit selection to visible

I have a workaround that might help ease the pain of those who need this feature. (But I think this functionality should be part of blender, properly implemented.)
I wrote myself a small script some time ago, I guess it’s not much different than what’s included in the suggested add-ons.
It’s a macro that in the moment of box selection it triggers x-ray and hides the back wires. When the selection is finished x-ray is turned off. It doesn’t interfere with regular x-ray usage.

oskar_select_through_small
Limitations:

  • in the moment of box selection the face dots are used instead
  • occasionally when you click, drag and release very fast (usually when deselecting) it might not turn off the x-ray. It gets fixed on the next selection.

The same idea can be used for lasso selection.

I made a 2 min video showing how to install it and add it to your keymap.

Here is the script, just copy this to a new text file and save it as a python script.
(oskar_select_through.py for example)

# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####


import bpy
from bpy.types import Operator, Macro

bl_info = {
    'name': 'Select Through',
    'description': 'Box select components in shaded mode that are not visible. Optional/default behavior in most 3D software.',
    'author': 'Oskar',
    'license': 'GPL',
    'version': (0, 1),
    'blender': (2, 80, 0),
    'category': 'Interface'
    }

class OSR_OT_xray_on(bpy.types.Operator):
    """Turn xray on"""
    bl_idname = "osr.xray_on"
    bl_label = "OSR: XRay On"
    
    
    # ==== CHANGE SETTINGS HERE ====
    
    # This controls the alpha of the shaded geometry, while box selecting.
    xray_alpha = 1
    
    # This controls the opacity of the wires that are behind (not visible in shaded mode). 
    # If you want to see a bit of the wires and components behind while selecting set it above 0 to max 1.
    backwire_opacity = 0
    # ==============================
    
    
    def execute(self, context):
        scene = bpy.context.scene
        # There seems to be a problem. Sometimes the macro is not executed properly when we click very fast. 
        # Instead of all classes in the macro, just the first one is executed.
        prop = scene.get("osr_select_through_macro_fail", None)
        
        # If xray_off did not fail
        if prop is None or prop == False:
            scene["osr_xray_last_state"] = context.space_data.shading.show_xray
            scene["osr_xray_last_alpha"] = context.space_data.shading.xray_alpha

        if scene["osr_xray_last_state"] == False:
            context.space_data.shading.show_xray = True
            context.space_data.shading.xray_alpha = self.xray_alpha
            context.space_data.overlay.backwire_opacity = self.backwire_opacity
        
        # We consider the macro failed unless xray_off is executed
        scene["osr_select_through_macro_fail"] = True
        
        return {'FINISHED'}
        
class OSR_OT_xray_off(bpy.types.Operator):
    """Turn x-ray off"""
    bl_idname = "osr.xray_off"
    bl_label = "OSR: XRay Off"
    
    def execute(self, context):
        
        scene = bpy.context.scene
        prop = scene.get("osr_select_through_macro_fail", None)
        
        if prop is not None:
            context.space_data.shading.show_xray = scene["osr_xray_last_state"]
            context.space_data.shading.xray_alpha = scene["osr_xray_last_alpha"]
        
        # Set the default backwire_opacity
        context.space_data.overlay.backwire_opacity = .5
        
        scene["osr_select_through_macro_fail"] = False
        return {'FINISHED'}


class OSR_OT_select_through_macro(Macro):    
    bl_idname = 'osr.select_through'
    bl_label = 'OSR: Select Through'
    bl_options = {'REGISTER', 'UNDO'}


classes = (
    OSR_OT_xray_on,
    OSR_OT_xray_off,
    OSR_OT_select_through_macro,
    )


def register():
    for c in classes:
        bpy.utils.register_class(c)
    
    OSR_OT_select_through_macro.define("OSR_OT_xray_on")
    OSR_OT_select_through_macro.define("VIEW3D_OT_select_box")
    OSR_OT_select_through_macro.define('OSR_OT_xray_off')
    

def unregister():
    for c in reversed(classes):
        bpy.utils.unregister_class(c)


if __name__ == "__main__":
    register()

    

Command for the keymap:

osr.select_through
8 Likes