“”"
AutoHighlightObjectLayers.py
Automatically highlights (selects, in the Layers panel) the layer(s)
of whatever object is currently selected in the viewport, without
needing to press a hotkey or run a command manually.
Based on the logic from HighlightObjectLayers.py by Dale Fugier (McNeel),
wrapped in an Idle-event listener so it runs continuously in the background.
Toggle on/off by running this script again (e.g. bound to a hotkey via
"! _-RunPythonScript ").
Works with Rhino 7/8 PythonScript (IronPython).
“”"
import Rhino
import System
import scriptcontext as sc
_last_selection_ids = set()
def _get_selected_object_layers():
settings = Rhino.DocObjects.ObjectEnumeratorSettings()
settings.ActiveObjects = True
settings.ReferenceObjects = True
settings.NormalObjects = True
settings.HiddenObjects = False
settings.LockedObjects = False
settings.DeletedObjects = False
settings.SelectedObjectsFilter = True
rh_objects = sc.doc.Objects.FindByFilter(settings)
if not rh_objects:
return None
layers_indices = set()
for rh_obj in rh_objects:
layers_indices.add(rh_obj.Attributes.LayerIndex)
return layers_indices
def _highlight_layers_for_selection():
layers_indices = _get_selected_object_layers()
if not layers_indices:
return
for layer_index in layers_indices:
layer = sc.doc.Layers[layer_index]
if not layer:
continue
parent_id = layer.ParentLayerId
while parent_id != System.Guid.Empty:
parent = sc.doc.Layers.FindId(parent_id)
if parent:
if not parent.IsExpanded:
parent.IsExpanded = True
parent_id = parent.ParentLayerId
else:
parent_id = System.Guid.Empty
sc.doc.Layers.Select(list(layers_indices), True)
def _on_idle(sender, e):
global _last_selection_ids
try:
selected = sc.doc.Objects.GetSelectedObjects(False, False)
current_ids = set(obj.Id for obj in selected) if selected else set()
if current_ids != _last_selection_ids:
_last_selection_ids = current_ids
if current_ids:
_highlight_layers_for_selection()
except Exception:
pass
def StartAutoHighlightLayers():
old_handler = sc.sticky.get(“auto_highlight_layers_handler”)
if old_handler is not None:
Rhino.RhinoApp.Idle -= old_handler
Rhino.RhinoApp.Idle += _on_idle
sc.sticky["auto_highlight_layers_handler"] = _on_idle
sc.sticky["auto_highlight_layers_active"] = True
print("AutoHighlightObjectLayers: ON")
def StopAutoHighlightLayers():
old_handler = sc.sticky.get(“auto_highlight_layers_handler”)
if old_handler is not None:
Rhino.RhinoApp.Idle -= old_handler
sc.sticky[“auto_highlight_layers_handler”] = None
sc.sticky["auto_highlight_layers_active"] = False
print("AutoHighlightObjectLayers: OFF")
def ToggleAutoHighlightLayers():
is_active = sc.sticky.get(“auto_highlight_layers_active”, False)
if is_active:
StopAutoHighlightLayers()
else:
StartAutoHighlightLayers()
if name == “main”:
ToggleAutoHighlightLayers()