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?
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()
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]…
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.
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.
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.
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.
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…
Well, this thread is 8 years old and the script was never finished in 2015 because the OP never posted a file to test/debug with. Here I can’t even download the script from the link above.
As mentioned above, this can be very complicated with nested blocks.