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.
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)
@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
The latest version of eleFront does allow you to create just the definition, without baking, if thatās the aim.
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
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
@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
Yes, again, did you not see the post above? You can do whatever you want/require with C# and GHPython.
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.
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