Python ExplodeBlockInstance not working as expected

Hi

I’m trying to use rs.ExplodeBlockInstance(object_id, explode_nested_instances=False) with this second argument to entirely flatten a block hierarchy. The documentation for this command is here in the API:

http://developer.rhino3d.com/5/api/RhinoScriptSyntax/win/#block-ExplodeBlockInstance

This is my code:

blockContents = rs.ExplodeBlockInstance(objectId, True)

But I get an error saying:

Message: ExplodeBlockInstance() takes exactly 1 argument (2 given)

Traceback:
  line 29, in RunCommand, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\PythonPlugins\Syse {863cf680-d131-479f-b639-5034403f3eea}\dev\BlocksToLayers_cmd.py"
  line 53, in <module>, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\PythonPlugins\Syse {863cf680-d131-479f-b639-5034403f3eea}\dev\BlocksToLayers_cmd.py"

Seems this function does not work as documented?

Hi @bjornsyse,

i can repeat the error using Rhino 5 SR12. The source of the rs.ExplodeBlockInstance(id) method is located in this file at line 235:

C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\block.py

It seems to accept only the object_id and no other options. To fix this, you might replace the method with this code, it works with nested instances:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
    
def CustomExplodeBlockInstance(object_id, explode_nested=True, delete=True):
    io = rs.coercerhinoobject(object_id, True, True)
    rc = sc.doc.Objects.AddExplodedInstancePieces(io, explode_nested, delete)
    return rc
    
def DoSomething():
    id = rs.GetObject("Select instance", rs.filter.instance, True, False)
    if not id: return
    
    ids = CustomExplodeBlockInstance(id, True, True)
    if ids: rs.SelectObjects(ids)
    
DoSomething()

_
c.

Hi,

Thanks for looking into it, I’m on Rhino 5 SR14. The workaround suggested works well!

Best regards

  • Björn

Actually, I got an error referencing to a 000-somethinge id in that coercerhinoobject-method so I ended up using this instead:

rs.SelectObject(objectId)

        # Select block instance
        rs.SelectObject(objectId)
        
        # Explode block
        #blockContents = CustomExplodeBlockInstance(objectId,True, True)
        print "Exploding block ", blockName, "..."
        
        rs.Command("_ExplodeBlock",False)
        blockContents = rs.LastCreatedObjects(False)

here is the error:

Message: 00000000-0000-0000-0000-000000000000 does not exist in ObjectTable

Traceback:
  line 604, in coercerhinoobject, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\utility.py"
  line 49, in RunCommand, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\PythonPlugins\Syse {863cf680-d131-479f-b639-5034403f3eea}\dev\BlocksToLayers_cmd.py"
  line 68, in <module>, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\PythonPlugins\Syse {863cf680-d131-479f-b639-5034403f3eea}\dev\BlocksToLayers_cmd.py"
  line 525, in ObjectLayer, "C:\Users\TMGxbs\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\object.py"

I tried manually exploding the object in question and checked all object id´s with What-command but none of them were 0:ed out.

Hi @bjornsyse,

does your object pass rs.IsBlockInstance(id) ? If so can you post the object to repeat this ?

_
c.

Hi again

Yes, I check for that right before…

if not rs.IsBlockInstance(objectId):
            print "Not a block instance: ", objectId
            break

I’ll export the problematic part and send it to you in a PM.

Hi Björn,

if i use above script on your part and iterate over the resulting object ids without selecting them, it lists 3 of the empty guids:

00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
0026d06d-6dd9-4c6f-a77f-03aa818e6e77
00b76093-9133-4485-9e45-0403aab06e5a
00f89755-1db1-4b02-ac80-5a66501337fa

If i pass the list of guids to eg. rs.SelectObjects(ids) i get the error you report in rs.coercerhinoobject() which is called from rs.SelectObjects(). One way to prevent the error is to filter out empty guids in the function like this:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
import System
    
def CustomExplodeBlockInstance(object_id, explode_nested=True, delete=True):

    io = rs.coercerhinoobject(object_id, True, True)
    rc = sc.doc.Objects.AddExplodedInstancePieces(io, explode_nested, delete)
    if rc:
        filtered_ids = filter(lambda id: id != System.Guid.Empty, rc)
        return filtered_ids
    else:
        return

@dale, if you read this, should AddExplodedInstancePieces handle removal of empty guids ?
_
c.

Yes it should.

https://mcneel.myjetbrains.com/youtrack/issue/RH-42305

– Dale

@bjornsyse, @clement,

The next Rhino WIP for Windows will have the fix for RH-42305. Thanks.

1 Like

Thanks!

RH-42305 is fixed in the latest BETA