Change layer of block *definition*?

OK, I added a line to Pascal’s script ( I hope Pascal doesn’t mind )
to also move the selected block instances to the chosen layer.

HTH

import rhinoscriptsyntax as rs


def MoveBlockObjectsToLayer():
    
    ids = rs.GetObjects("Select block instances", 4096, preselect=True)
    if not ids:return
    
    targ = rs.GetLayer()
    if not targ:return
    
    names = list(set([rs.BlockInstanceName(id) for id in ids]))
    done = []

    # EDIT: also move block instances to the selected layer
    rs.ObjectLayer( ids, targ )
    # /EDIT
    
    def BlockDrill(names):
        while True:
            if len (names) > 0 :
                name = names.pop()
            else: break
            
            done.append(name)
            temp = rs.BlockObjects(name)
            rs.ObjectLayer(temp, targ)
            
            for tempId in temp:
                if rs.IsBlockInstance(tempId):
                    tempName = rs.BlockInstanceName(tempId)
                    if tempName not in names and tempName not in done:
                        names.append(tempName)
                        BlockDrill(names)
            
    BlockDrill(names)
    
if __name__ == "__main__": MoveBlockObjectsToLayer()

EDIT:
Huh … forgot nested blocks …

import rhinoscriptsyntax as rs


def MoveBlockObjectsToLayer():
    
    ids = rs.GetObjects("Select block instances", 4096, preselect=True)
    if not ids:return
    
    targ = rs.GetLayer()
    if not targ:return
    
    names = list(set([rs.BlockInstanceName(id) for id in ids]))
    done = []

    # EDIT: also move block instances to the selected layer
    rs.ObjectLayer( ids, targ )
    # /EDIT
    
    def BlockDrill(names):
        while True:
            if len (names) > 0 :
                name = names.pop()
            else: break
            
            done.append(name)
            temp = rs.BlockObjects(name)
            rs.ObjectLayer(temp, targ)
            
            for tempId in temp:
                if rs.IsBlockInstance(tempId):

                    # EDIT: also move nested instances to the selected layer
                    rs.ObjectLayer( tempId, targ )
                    # /EDIT

                    tempName = rs.BlockInstanceName(tempId)
                    if tempName not in names and tempName not in done:
                        names.append(tempName)
                        BlockDrill(names)
            
    BlockDrill(names)
    
if __name__ == "__main__": MoveBlockObjectsToLayer()
2 Likes