How can i use this python script to define a block in grasshoppper?

import Rhino
import scriptcontext

def CreateBlock():
    # Select objects to define block
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt( "Select objects to define block" )
    go.ReferenceObjectSelect = False
    go.SubObjectSelect = False
    go.GroupSelect = True

    # Phantoms, grips, lights, etc., cannot be in blocks.
    forbidden_geometry_filter = Rhino.DocObjects.ObjectType.Light | Rhino.DocObjects.ObjectType.Grip | Rhino.DocObjects.ObjectType.Phantom
    geometry_filter = forbidden_geometry_filter ^ Rhino.DocObjects.ObjectType.AnyObject
    go.GeometryFilter = geometry_filter
    go.GetMultiple(1, 0)
    if go.CommandResult() != Rhino.Commands.Result.Success:
        return go.CommandResult()

    # Block base point
    rc, base_point = Rhino.Input.RhinoGet.GetPoint("Block base point", False)
    if rc != Rhino.Commands.Result.Success: return rc

    # Block definition name
    rc, idef_name = Rhino.Input.RhinoGet.GetString("Block definition name", False, "")
    if rc != Rhino.Commands.Result.Success: return rc
    # Validate block name
    idef_name = idef_name.strip()
    if not idef_name: return Rhino.Commands.Result.Nothing

    # See if block name already exists
    existing_idef = scriptcontext.doc.InstanceDefinitions.Find(idef_name, True)
    if existing_idef:
        print "Block definition", idef_name, "already exists"
        return Rhino.Commands.Result.Nothing

    # Gather all of the selected objects
    objrefs = go.Objects()
    geometry = [item.Object().Geometry for item in objrefs]
    attributes = [item.Object().Attributes for item in objrefs]

    # Add the instance definition
    idef_index = scriptcontext.doc.InstanceDefinitions.Add(idef_name, "", base_point, geometry, attributes)

    if idef_index<0:
        print "Unable to create block definition", idef_name
        return Rhino.Commands.Result.Failure
    return Rhino.Commands.Result.Failure


if __name__=="__main__":
    CreateBlock()

It seems a python script for Rhino environment.

Just go to Rhino, select Tool palete, open Python Script, Edit and paste the script. Then you can run it and follow the steps.

Regards

I see. I was looking for something like this for grasshopper . I don’t know why this script to create block in rhino that already have all the commands to manage blocks.

HI @lopez

Try Elefront plugin for grasshopper. Using it you can create blocks.

Regards!

I would suggest using the “Define Block” component from the Human-plugin. It seems to use a more native way of create the block definition. Human doesn’t handle point, text or dimensions though, but Elefront does.

Elefront also doesn’t create the Block Definition until you bake the block instance:

You need to change the scriptcontext when targeting the Rhino document from a GHPython script. See this thread:

Edit: Here’s a quick implementation:


220324_MakeBlock_00.gh (3.1 KB)

1 Like

@AndersDeleuran yeah that would be the direct implementation but now I’m unsure if @lopez wants to define a block from a Rhino selection or from Grasshopper geometry. The code in the original post is just the sample code from here: Create Block Definition with C#, Python, VB

1 Like

The latest version of eleFront does allow you to create just the definition, without baking, if that’s the aim.

1 Like

My aim is very simple: create a cube, define a block from this cube named “cube”, move 2 times this block that is a block just in grasshopper, finally bake the 2 moved cubes and have in rhino 2 copies of the same block named “cube”…so far so easy…but I can’t understand why grasshopper doesn’t do this native.
I wouldn’t be linked to a plugin of a plugin hoping that the developer will update it for the next release (it’s just my opinion of course).

Lopez, Here’s David’s reply to Blocks in Grasshopper back in 2009.

GH1 has become far more than it was ever intended, it was more of a blank slate that the community has built something completely different than initially envisioned.

Personally I respect too much all the work did by David Rutten (a genius to me inventing grasshopper) and I ‘m sure that his reply have good raison to be as usual. At the same time , if grasshopper, after 13 years from that reply, is become other thing that was intended for at his born, I think Mcneel could be very happy about this, and try to work on it to grow it as “other thing” (from another space)

Indeed, there appears to be the OP explicit question of how to implement the posted example code in GHPython (i.e. that is solved above). And then a much fuzzier/larger discussion about blocks in Grasshopper. Either way, it’s probably all quite scriptable in anyone’s language of choice :tipping_hand_man:

hi, Anders,
I was thinking that the script I’ve found was usable in grasshopper (not in rhino) to make blocks in the same way elefront do, as keyan told.

@AndersDeleuran yes there is a much bigger debate about blocks and Grasshopper but from my standpoint Grasshopper supports RhinoCommen so anything goes. I have around 11 components all focused of defining, reading, managing and analysis of blocks. Just in C# you know :wink:

@lopez you’re exactly right, the code you posted is exactly what I based my block-define component on, just the C# version.

@krahimzadeh great to hear! Even less reason to code one’s own stuff.

So It’s possible to define these components using Python too?

Amen to that. I’d totally script the heck out this as well. All my Grasshopper definitions are pretty much pure RhinoCommon implemented in GHPython these days :raised_hands:

Yes, again, did you not see the post above? You can do whatever you want/require with C# and GHPython.

1 Like

Not whatever I want/require…I need to have the Rhino Common commands, at least.
So the question was if I can do the same did from Morten in Python instead of C# or if Python have some limitation about this…I’ve understood yes, but because it’s not specifically my domain, I would like to be sure not to study this thing for nothing

@lopez the code you posted is already a good base. You need to replace all the Rhino-command Get-code (GetMultiple, GetPoint, GetString, etc.) with inputs into a GhPython component. As @AndersDeleuran writes there is NO limitation in Grasshopper by doing Python, somethings just have to be done a little different (like setting the script context @AndersDeleuran showed above).

If you need a good base overlook into how to do Python in Grasshopper I would recommend this tutorial series: Python Scripting in Grasshopper 1/5 - Introduction - YouTube or this one: GhPython Flash Workshop - Beginner Level. Episode 1 (English) - YouTube

You might already be Python proficient, but this will show you how to define component inputs and outputs.

1 Like

Thank you, Morten. I’ve already done some Python scripts in grasshopper but basic to understand it. Now I will go deeper to do this. Thanks, Roy

1 Like