Hi All,
I am trying select objects not by a specific layer but with anything containing a string (w/python).
For instance: if the object has ‘barrier’ somewhere in attribution all barriers would be selected within the drawing.
Any assistance is appreciated!
PS I was intending on modifying the following script
import Rhino
import scriptcontext
import System.Guid, System.Drawing.Color
def SelLayer():
# Prompt for a layer name
layername = scriptcontext.doc.Layers.CurrentLayer.Name
rc, layername = Rhino.Input.RhinoGet.GetString(“Name of layer to select objects”, True, layername)
if rc!=Rhino.Commands.Result.Success: return rc
# Get all of the objects on the layer. If layername is bogus, you will
# just get an empty list back
rhobjs = scriptcontext.doc.Objects.FindByLayer(layername)
if not rhobjs: Rhino.Commands.Result.Cancel
for obj in rhobjs: obj.Select(True)
scriptcontext.doc.Views.Redraw()
return Rhino.Commands.Result.Success
if name==“main”:
SelLayer()
I’m using regex patterns in Grasshopper to filter layer names…
1 Like
Hi JP. I assumed you are searching for objects in layers with ‘barrier’ in the layer name (I’ve commented out extra conditions to only select objects with the text the object name or in some User Text value), but try this:
import Rhino
import scriptcontext
import System.Guid, System.Drawing.Color
import rhinoscriptsyntax as rs
def select_objs_from_layer_search():
# Prompt for a search string
search_str = ''
rc, search_str = Rhino.Input.RhinoGet.GetString("Name of layer to select objects", True, search_str)
if rc!=Rhino.Commands.Result.Success: return rc
layers = [layer for layer in rs.LayerNames() if search_str in layer.lower()]
# Get all of the objects on the layers. If search_str is bogus, you will
# just get an empty list back
rhobjs = [obj for layer in layers for obj in rs.ObjectsByLayer(layer)]
# to search by name: if search_str in rs.ObjectName(obj)
# to search by user text: any(search_str in rs.GetUserText(obj, key) for key in rs.GetUserText(obj))
if not rhobjs: Rhino.Commands.Result.Cancel
rs.SelectObjects(rhobjs)
scriptcontext.doc.Views.Redraw()
return Rhino.Commands.Result.Success
if __name__=="__main__":
select_objs_from_layer_search()
1 Like
thank you!! both answers were extremely helpful! 
1 Like