Need a script which will rename object names based on the block they are in

Hello,
New to Rhino and the forum.

I have to work on a file which has a lot of objects and all of them have the same name.
I found that there is a hierarchy in the file which uses blocks. There is one main block and under it other blocks which contain both geometry and blocks which can contain again both blocks and geometry.

Is it possible to write a script that changes each object name based on the block it is in?

Thanks

Well, if you have nested blocks, at the bottom level, you will have objects in a single block which may then be nested inside other blocks. So I guess you want the object name to be that lowest-level block name and not one of the upper level blocks it’s nested inside?

–Mitch

Edit: you can try the following and see if it does what you want…

"""Renames objects inside nested blocks to the name of the first (lowest level)
block that contains them (via recursion).  Script by Mitch Heynick 28.01.15"""
import rhinoscriptsyntax as rs

def DiveIntoBlock(blk_ID):
    blk_name=rs.BlockInstanceName(blk_ID)
    objs=rs.BlockObjects(blk_name)
    for obj in objs:
        if rs.IsBlockInstance(obj):
            DiveIntoBlock(obj)
        else:
            rs.ObjectName(obj,blk_name)
    
def BlockNameToObjects():
    blks=rs.ObjectsByType(4096)
    if not blks: return
    for blk in blks:
        DiveIntoBlock(blk)
BlockNameToObjects()

Hello Mitch,
Thank you for your reply.

Please let me know if the following errors I get are due to me not installing or doing something right. Any idea why I get these?

I run the script and get the following error:

Source: Microsoft VBScript compilation error
Error: Expected end of statement
Line: 4
Car: 25
Code: import rhinoscriptsyntax as rs

If I remove this line and run the code again I get this:

Source: Microsoft VBScript compilation error
Error: Expected '='
Line: 9
Car: 12
Code: for obj in objs:

and then:

Source: Microsoft VBScript compilation error
Error: Cannot use parentheses when calling a Sub
Line: 13
Car: 39
Code: rs.ObjectName(obj,blk_name)

Would greatly appreciate if you could look into it.
Thanks

Hi,

This is a Python script, not a vb script. It’s also not designed to be drag and dropped. To test it, the easiest way is via the Python script editor using the EditPythonScript command, open the editor, paste the script in and hit the green arrow to run.

For other ways of installing it more permanently, [see this post][1]…

–Mitch
[1]: Script OneLayerOff

Thanks Mitch, this is of great help!

Please pm me your email if you would like me to buy you a beer, I would love to :smiley:

Thanks again!

Is it possible to create another script which does not interfere with this one.
The script would allow to select few blocks in the block tree and create layers with the names of those blocks and also put all objects which are within these blocks in the newly created layers?

I dont know how these requests work exactly, but I am more than happy to donate/pay for those scripts.

Thanks

Or better yet, and if easier, would be to have a script that would convert the block tree to a layer tree in the layers tab.

EDIT: I have found this, thought it might be helpful:

Thanks

This is considerably trickier, as block objects can reside on different layers originally and there can be overlapping layers of nesting. You can try the script below and see if it does what you want, make sure you check the results…

It will also try to delete any unused layers formerly occupied by the block objects.

–Mitch

BlockNameToLayers.py (1.7 KB)

Hello Mitch,

Thank you for your reply.

I run the script and get the following:

Exception Occured:
Message: 002 - SCREW - METRIC PAN HEAD does not exist in InstanceDefiniotionsTable

Traceback:
line 191, in BlockObjects,
"C:\Users\pc\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython
(814d908a-e25c-493d-97e9-ee3861957f49\settings\lib\rhinoscript\block.py"
line 9, in BlockNameToLayers,
“path to \BlockNameToLayers.py

and then many of these last two lines.

When I click OK, I see the script did create few layers though.

In my case the block tree looks like this:

+Block
  +Block
     +Block
        +GeometryObject
     +Block
        +GeometryObject

So each geometry object is alone in a block. And only the lowest blocks have geometry in them. All other blocks are just parents to child blocks.

EDIT:
Also everything is in the default layer. So each block and its objects can be only in it. It might be an option to make the script just recreate the block tree structure as a layer structure with the deepest layers containing each one geometry object.

Thanks

Doesn’t surprise me - I only have a test file here I created, but as I said, working with this stuff is tricky. Can you post or PM me the file you’re working in? I can probably figure out why it’s erroring out. I’m trying to clean out the unused block definitions as the script proceeds, otherwise you can get multiple identical objects…

–Mitch