Import blocks from another file using InsertBlock?

I am trying to import block definitions from another file using Python. I actually found someone asking a very similar question before, but I simply cannot get it to work.

Previous question: Insert block from other file... with python script

I am trying to do something very similar:

def import_target_blocks(target_block_names, cell_set_file):
    """Import the target block definitions from the cell set file."""
    for target_block_name in target_block_names:
        # Insert the block at the origin (0, 0, 0)
        insertion_point = [0,0,0]
        instance_ref = rs.InsertBlock(cell_set_file, target_block_name, insertion_point)

        # Delete the block instance as we only need the block definition
        if instance_ref is not None:
            rs.DeleteObject(instance_ref)

# Test the function
cell_set_file = rs.OpenFileName("Select the cell set file")
import_target_blocks(target_block_names, cell_set_file)

I keep getting the error: Message: invalid literal for float(): SL1_CNC_13

If I check the script syntax for InsertBlock now it says:

InsertBlock

Inserts an instance of a block whose definition already exists in the document.

So now I am wondering if using InsertBlock to import a block definition is even still possible, becasue it specifically say the definition has to already exist in the document!?

Can anyone help out? What am I doing wrong?

Hi @seltzdesign

From looking at your code sample it looks like you have the arguments to the rs.InsertBlock function in the wrong order, which is probably the reason for the error you are getting.

As you correctly mention, this method won’t allow you to import block definitions from other files anyway and a quick search through the whole documentation does not seem to show any function that does offer that functionality.

So probably it’s best if you script the _Insert command as recommended also in the post that you linked.

Hope to help, Lando

Thanks for the support! I really appreciate it.

Yes, it seems like that might have worked back in the day, but now it does not seem like there is any way to easily do this.

I ended up importing the whole file using Python, get the transforms of all instances I want to replace, use InsertBlock2 to add the new instances with the old transforms and then delete the old instances.

It could all have been so easy. ReplaceBlock(old_block, new_block) could have been 1 line of code, but now is more like 30.

Working with Blocks in Rhino is a real pain, no matter if you do it in the UI or in code as I have discovered. Of course it doesn’t help that GH doesn’t offer native block support, but apparently they are working on that, even for GH1. Let’s see.

I agree that this functionality is underdeveloped. From what I’ve seen about the new gh components in Rhino8 it looks like those could be a solution to your problem in the future.

Fingers crossed :slightly_smiling_face:

1 Like

Hi,
That is possible, the way I would do it would be the following :

  • Have a loader script running on your mainfile.3dm
  • Have it ask the user for the block name and file path
  • Run a standalone .py script (external to Rhinoceros) that will copy the file, open it, remove everything except blockname it asked for before
  • Close and save the copy file
  • The main loader script, that is still running on your mainfile.3dm will then be basically the same as is now, and import the copiedfile.3dm as a block

It’s kind of a workaround but works on paper, not very efficient though, but effective.
That being said, the RhinoCommon api have no idea to do what you desire, so if you really want to do it that’s one possible solutions.
It’s not hard to do, its just a very ugly solution.

Farouk

1 Like