How to script ReplaceBlock?

I am trying to script ReplaceBlock and not having any luck. If I run _-ReplaceBlock I actually get a command line option to “SelectFromBlockDefinitionList”, which then opens a dialog where I can select a Block definition. The same happens for the replacement block.

So how can I script that with no user interaction? Can we not just enter the old definition name and new definition name?

How else would I be able to replace all instances of one block with the definition of another block using scripting. I am starting to think that this is not possible with no user interaction…

(Assuming I undestrand correctly what you need :slight_smile: )

Just a simple test script replacing one object:

import rhinoscriptsyntax as rs

def main():
  oldb = rs.GetObject( 'Block to replace ?' )
  newb = rs.GetObject( 'Reference block ?' )
  newn = rs.BlockInstanceName( newb )
  oldx = rs.BlockInstanceXform( oldb )
  rs.DeleteObject( oldb )
  rs.InsertBlock2( newn, oldx )

main()

I think we only need to loop over the old block instances to replace them all …
And obviously the block names can be obtained in different ways.

HTH

Thanks for the script. It is more or less what I ended up using in the end.

I still think it would be very elegant if we could use rs.ReplaceBlock(old_name, new_name). I mainly find it odd that there actually is no silent version of ReplaceBlock, even with _-ReplaceBlock you have to use the UI.

1 Like

Hi @seltzdesign,

If you want to script the ReplaceBlock command, then something like this might be the ticket.

_-ReplaceBlock _-SelBlockInstanceNamed "Block1" _Enter _BlockDefinitionName "Block2"

– Dale