Selecting objects by type on layer

When I was looking at using ObjectsByLayer, I noticed there was no option to filter out the type of object.
Is there another way to do this while maintaining the layer selection??

I saw this built into the RhinoScript syntax but not the python version

You can do this in one or two lines with Python/rhinoscriptsyntax:

filt=rs.filter.curve
objs=[obj for obj in rs.ObjectsByLayer("Layer Name") if rs.ObjectType(obj)==filt]

I basically want to run this across the entire document to join all the meshes by layer.
I tried to write the code into a for loop, but the join is failing…
What am I doing wrong here?

layerID = rs.LayerNames()

filt=rs.filter.mesh

for layerObj in layerID:
objcts = [obj for obj in rs.ObjectsByLayer(layerObj) if rs.ObjectType(obj)==filt]
rs.JoinMeshes(objcts)

Yep, this might be confusing. First,

rs.JoinMeshes(objcts) without a second argument will make a joined copy and leave the originals. So I think you want

new_meshes=rs.JoinMeshes(objcts,True) - which will delete the originals.

And, rs.JoinMeshes() also puts the new joined mesh on the current layer. If that happens to be further down the list than where you are in the script, those will also get added and joined to the other meshes on that layer - resulting in a possible mess.

So, something like this, which throws the new joined mesh back onto the original layer is maybe better:

import rhinoscriptsyntax as rs

layerID = rs.LayerNames()
filt=rs.filter.mesh

for layerObj in layerID:
    objcts = [obj for obj in rs.ObjectsByLayer(layerObj) if rs.ObjectType(obj)==filt]
    if objcts and len(objcts)>1:
        new_mesh=rs.JoinMeshes(objcts,True)
        rs.ObjectLayer(new_mesh,layerObj)

HTH, --Mitch

1 Like

This shouldn’t happen as the full list is built before you start joining, so the new meshes do not get added to the objcts list.

It does actually, because the list of meshes on each layer is built inside the loop. So if new meshes are added to the current layer and the current layer is further down the list than where you currently are in the loop, they will be included in the selection for that layer.

But that’s really because it’s a poorly written script, a more robust way would be to get the mesh objects on each layer outside the loop…

Ahh yes I see. Sorry I w s just looking at the inner loop :octopus:

Is there a way of seperating the mesh objects first then grouping it by layer?
I just knew of that one way of cycling through the layers…

Still new to the python / rhino scene