Python - Block Exploding Script To Work With GH Referenced Block

Hello,

I have this working python script I cobbled together that explodes a Rhino Block either top level or recursively until no block instances are found.

Currently it works as expected if the Block already exists in the Rhino document.

However, I am trying to modify it further to specifically work on referenced block instance geometry from grasshopper.

My current work around is to bake the block to the document as a hidden object and then explode said hidden object with the script. I want to skip the baking step and directly explode the referenced Model Block Instance in Grasshopper, returning the exploded, referenced geometry as the result.

I’m guessing the answer lies in using a method outside of RhinoScriptSyntax and working directly with the GH block instance but I cannot find the method needed for this.

Any help is greatly appreciated, thank you!

Graph Space:

Python Code:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

G = []

#Handle Component Message
if R == True:
    ghenv.Component.Message = "Recursive"
elif R == False:
    ghenv.Component.Message = "Top Level"
else:
    ghenv.Component.Message = None

#Set Context To Rhino Doc
sc.doc = Rhino.RhinoDoc.ActiveDoc

def explode_blocks(B, R):
    for Bi in B:
        if rs.IsBlockInstance(Bi):
            G.append(rs.ExplodeBlockInstance(Bi, R))
        else:
            G.append(Bi)
    return G

G = explode_blocks(B, R)

#Set Context Back To Grasshopper Doc
sc.doc = ghdoc

20230722_Recursive_Block_Explosion_Script_01a.gh (474.3 KB)

I’m deprecating this as the new “Explode Objects” node in R8 WIP does what I am after and has optional Recursion.

Thanks @AndyPayne

1 Like