Change path to block via script

I am starting to daydream about a script that swaps out block instances for low-detail, perhaps already meshed, equivalents or vice versa back to the OG NURBS version.

Logic would be something like

step through all the objects in the scene
is it a linked block? if yes
does it’s name contain a code like starting with hi- (etc.) if yes
get it’s path and look for same name but with lo- or whatever. if fount then
swap the paths
next

One initial question… I can see how to get the block’s path… but nothing about changing it? Do I have to delete the block definition and insert the replacement as a new definition? Would I then lose all the instances? Need some help on the logic before I dig into this.

Actually I guess it’s probably easier if the model has definitions for both versions… makes swapping the instances in and out way simpler, right?

when it finds a "victim"
grab the xform
deleteObject
InsertBlock with the low res replacement

Hi Ryan,

if the low and high resolution blocks are both instances of the same document and one of them is placed mutliple times you might just do the swapping using _ReplaceBlock in a macro using the desired block names.

So there would be no need to change any reference paths. Or do i misunderstand the task ?

c.

Hi Ryan,

I posted a script that should help a while ago - see if it does the trick:

I use it for swapping low-high detail blocks.
As long as you keep your naming organized, you can mass-change many of the block types at once.

PS. to ‘remove’ prefix or suffix, use “Replace” method in the script and just feed it with empty strign [Enter].

hth,

–jarek

Thanks @clement @Jarek

I’ll take a look.

I guess this applies to any script (sorry I’m green)

But is it possible to apply this to all the block instances in the model, regardless of layer visibility?

Or as a rule do you have to sortof force your user to select the objects?

In other words, what could I do instead of Rhino.GetObjects that would give me the array of all the block instances in the model

You would need to use: Rhino.GetObjectsByType(4096) - this way you would get all instances in the document.

-j

OK think I’ve got this how I want it.

No user prompts. Name the low poly blocks ending in -lp and the detailed blocks -og. It just swaps everything in the file. No frills. It just skips blocks that don’t have a -lp or -og suffix.

SwapBlocksToHigh.rvb (1.0 KB)
SwapBlocksToLow.rvb (1.0 KB)

Hrmm… if I wanted to restore groupings… is that a big effort or should I give up on that?

In the original script there was a line that was matching the attributes from the original block to the newly inserted replaced block. Looks like one of the attributes is grouping, too.
So you may want to use this:

Call Rhino.MatchObjectAttributes (Rhino.InsertBlock(blockName, xForm),strObject)

hth,

–jarek

Perfect. Thanks Jarek.