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
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)
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)
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…