Need a script pleeeease? > join by layer

Hi All,

I imported an assembly where each layer is a part, but they came as individual surfaces, not polysurfacse/solids. I’d love to have a quick way to run a script that looks at my file and joins all the surfaces on the same layer/sublayer? right now it looks like this (with lots of scrolling):

I’d appreciate the help!

Gustavo

Hi G,

Are you familiar with running python scripts?
Below the script that will automate what you want.
It is bare and maybe slow but that’s all I have time for right now.

I advice you to test it on a subset of your file first

import rhinoscriptsyntax as rs

def JoinByLayer() :
    layer_list = rs.LayerIds()
    
    for layer_id in layer_list:
        rs.UnselectAllObjects()
        
        rs.SelectObjects(rs.ObjectsByLayer(layer_id))
        rs.Command('_Join')
        
if( __name__ == '__main__' ):
        
    JoinByLayer()

HTH
-Willem

I tried all i know about programming:

and that doesn’t do it. please don’t judge :smile:

No worries,

EDIT I just now realized you had it almost correct in the macro editor:
in your macro it should be:

!_-RunPythonScript (

import rhinoscriptsyntax as rs

def JoinByLayer() :
    layer_list = rs.LayerIds()
    
    for layer_id in layer_list:
        rs.UnselectAllObjects()
        
        rs.SelectObjects(rs.ObjectsByLayer(layer_id))
        rs.Command('_Join')
        
JoinByLayer()

)

In the Python editor it goes like this:
Open Pyhton editor Command: _EditPythonScript

Next open new tab:

Paste the code and hit run button

HTH
else let me/us know

-Willem

Going to suggest a small improvement to Willem’s script… Cut the redraw to go a bit faster and a check to not run join if there are no objects selected (layer empty, all objects locked, etc…) or just one…

–Mitch

import rhinoscriptsyntax as rs

def JoinByLayer() :
    layer_list = rs.LayerIds()
    rs.EnableRedraw(False)
    for layer_id in layer_list:
        rs.UnselectAllObjects()
        rs.ObjectsByLayer(layer_id,True)
        objs=rs.SelectedObjects()
        if objs and len(objs)>1: rs.Command('_Join')
        
if( __name__ == '__main__' ):
    JoinByLayer()
1 Like

Hi all, no luck so far:

Willem, Your script asks me “Select Object to Join” in command line after running it.

Mitch, yours doesn’t do anything

:sob:

Hi G
On my phone so not much testing possible.
Did mitch.s script get copy pasted correct?

It might be some lines got wrapped.

Willen

Probably because it ran into a layer with only one object or none…

You need to add the

! _-RunPythonScript (

at the top

and

)

at the bottom (as in Willem’s example), I don’t normally post scripts with that added.

–Mitch

Yeah there are layers with just one object or none (parent layers of other sublayers).

this is what I’m running:

Oh yeah, I forgot, just remove the “if name == main” line, and out-dent the line below it. Doesn’t work in a toolbar button with “if name == main”. Sorry for the disinformation…

Like so:

! _-RunPythonScript (
import rhinoscriptsyntax as rs

def JoinByLayer() :
    layer_list = rs.LayerIds()
    rs.EnableRedraw(False)
    for layer_id in layer_list:
        rs.UnselectAllObjects()
        rs.ObjectsByLayer(layer_id,True)
        objs=rs.SelectedObjects()
        if objs and len(objs)>1: rs.Command('_Join')
        
JoinByLayer()
)