GH: How to bake Block Instances with python Script?

I made a script that sucessfully bakes Breps, but when I try using the same script to bake Block instances (geometry) it fails. Why does it fail, and what code to use to properly “bake” Block instances?

This code fails for blocks but works fine for Breps :

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

# Set Name and Bake GH objects.

if active:
    if G:
        i = StartIx
        for obj in G:
            
            # Script context = Grasshopper
            sc.doc = ghdoc 
            
            # Get the geometry info for later baking
            dobj = rs.coercerhinoobject(obj)
            geom = dobj.Geometry
            attribs = dobj.Attributes
            
            # Script context = RhinoCAD
            sc.doc = Rhino.RhinoDoc.ActiveDoc
            
            
            # "BAKE" (sort of)
            new_ref = sc.doc.Objects.Add(geom, attribs)
            
            # NAME
            new_name = BaseName + "_" + str(i)
            rs.ObjectName(new_ref, new_name)
            i += 1

            # LAYER
            rs.ObjectLayer(new_ref, LayerName)
            
        out = G

See also

// Rolf

Hi @RIL

can you tell exactly what “it fails for blocks” means, given that there is no concept of blocks -but only geometry- in Grasshopper?

Can you also please always provide a Grasshopper definition you use in your tests?
Thanks!

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

I found out that the Block components came from the Elefront plugin (just typing F4 + “Blocks” doesn’t tell which plugin a component belongs to, so…), any way, I also learned that Elefront has a dedicated component for baking Blocks.

So, the problem seems to be solved, see the following thread in which I had some other questions related to Blocks, which I think will be answered in an example I was directed to. GH/Elefront: Block variant difference? - #7 by fraguada

// Rolf

Oh, I see.

You can use the 'Ctrl+Alt+Click' combo to see where a component is placed on the toolbar, and then right-click on it and choose "Component Info" to see the plug-in :slight_smile:

I hope this helps, kind regards,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com