Hello Everyone and @pascal .
I’m trying to make a Python Script called “Kill Layer” the intent is to select a layer and delete all of it’s contents and then delete the layer itself. my biggest hurdle now is some blocks contains elements in that layer that prevents purging, Is there a line in Python “Select the blocks instances that has specific objects in that layer”?
From there, delete these instances and continue purging.
Thank you in advance.
No, not in one go as far as I know, you would need to make your own small definition for that. Its’s not complicated, one would simply need to get all the block definitions in the file to see if they have objects on that layer. If any are found, then use rs.DeleteBlock(definition_name)
which handily will delete all the instances plus the definition. After this, the layer itself should be delete-able.
Something like this maybe (not thoroughly tested):
import rhinoscriptsyntax as rs
def GetBlkDefsOnLayer(layer_name):
blk_names=rs.BlockNames()
if blk_names:
match_defs=set()
for blk_name in blk_names:
obj_ids=rs.BlockObjects(blk_name)
for obj_id in obj_ids:
if rs.ObjectLayer(obj_id)==layer_name:
match_defs.add(blk_name)
break
return list(match_defs)
def TestFunction():
layer_name="LayerToDelete"
blk_defs=GetBlkDefsOnLayer(layer_name)
if blk_defs:
for blk_def in blk_defs:
rs.DeleteBlock(blk_def)
rs.DeleteLayer(layer_name)
TestFunction()
1 Like
@Helvetosaur Thank you Sir! I’m going to include these functions in my script.
Thanks again
Tay