Deleting geometry from blocks using Python

Hello everyone,

I’ve been trying to find a way to remove and add new geometry to nested blocks using Python. Using a recursive function I am able to change the properties of the nested geometry, but trying to remove or replace geometry does nothing. The DeleteObjects functions returns a success, but nothing happens to the block geometry. Example code below, highlighted line does nothing.

import rhinoscriptsyntax as rs

def RemoveBlockGeo():
    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 = []
    def BlockDrill(names):
        while True:
            if len (names) > 0 :
                name = names.pop()
            else: break
            done.append(name)
            temp = rs.BlockObjects(name)
            for tempid in temp: 
                if rs.ObjectType == 4096: 
                    **rs.DeleteObject(tempid)**
                    rs.Redraw()
            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__": RemoveBlockGeo()

This post was helpful to me when I was trying to figure out a similar thing. If you scroll down, there’s a python version. You;ll need to modify the block definition to remove the geometry.

https://discourse.mcneel.com/t/add-object-to-block-python-bypass-block-edit/16588

1 Like