Bake GH Components via Python

Bake_dimensions

1500 Nodes per iteration

The other day I needed to translate some GH dimensions to native rhino dimensions. Not only that , but it was a sequence of steps in a parametric model, which means that I had to repeat the same process for about a 1000 times. Obviously the old-fashioned right click wasn’t working for me,

Although, I already have some routines that can bake geometry directly to rhino (and export it) , the Dimension and Preview components of Grasshopper3d don’t have any geometry output. Nonetheless, i needed the leader(or marker ) tag utility.

I wrote this small snippet to bake, not only dimensions, but any component type and with a specific name , to a specific layer, without much hustling. I just put it out there for people to use, however I have to warn you that it was written in a couple of minutes, so its almost certain that it will contain many bugs.

Please let me know if you experience one, or if you have a possible suggestion or fix.

Enjoy a usual at the [Sub]Code Page. Python labeled defs.

#imports
import Grasshopper as gh
import Rhino as rc
import ghpythonlib.components as comp
from System import *
from System.Collections.Generic import *

#Active Rhinodoc
rcdoc = rc.RhinoDoc.ActiveDoc
#Active GH document
my_ghdoc = ghenv.Component.OnPingDocument()
attr =rc.DocObjects.ObjectAttributes()

# Bake the dimensions in the specified layer called Dimensions
#if it doesn't exist it used the default
attr.LayerIndex = rcdoc.Layers.Find(L,True)

# Iterate the active objects in the current GH file and find the one of the specific type
#if there are many look for the one with the name
objs = []
obj_types =[]
for obj in my_ghdoc.ActiveObjects():
 my_type = obj.GetType()
 obj_types.append(my_type)
 #uncomment the next line if you want to see all the component types in the document
 if str(my_type) == Comp_type :
 if Comp_name == None:
 objs.append(obj)
 print("found it")
 else:
 if obj.NickName == Comp_name:
 objs.append(obj)
 print("found it")
 #bake the component
 if Bake == True:
 for obj in objs:
 #create an empty GUID list
 ids = List[Guid]()
 obj.BakeGeometry(rcdoc,attr,ids)
 print("baked the dimension elements")

doc_components = obj_types

Unblocking GH add-ons + more a.k.a Why the add-on is not installed?

unblock_lib_title_DS

A really common question, of new or less new Grasshopper3d users, is why the 3rd party add-ons they’re trying to install are not appearing on the ribbon layout of the canvas. In the, not at all exaggerated, 99% of the cases, the issue lies in the fact that these files are “blocked” by windows.

You can briefly learn more about why windows block some files, especially downloaded from the web, here.

The common solution to this to prompt the user to right-click on the blocked .gha or .dll file, open the properties tab and click the unblock option on the bottom right corner of the properties window. Something that is efficient, but can take ages, if you don’t know which file is blocked, or have to unblock many files at the same time.

The code below saves all the fuss that could emerge from looking which files are blocked, or even by just clicking three times on a file to unblock it (yes I am kind of lazy..)

It also saves some people (GH forum users) the trouble explaining or troubleshooting the same scenario every single time.

So, by just clicking on the button toggle of this Grasshopper3d definition and executing the script you automatically unblock all the files in your local GH libraries directory. This ensures that when you restart Grasshopper, all the add-ons in this folder will be loaded onto the ribbon tab.

I also added an extra option that will install a special key to your registry, so that an complementary option of unblocking files is going to appear on the dropdown right – click menu. This of course means that you can select multiple files and unblock the simultaneously. Info on how to get rid of this extra option is provided while running the component. As you may have guessed, adding new keys to your registry relies heavily on your admin rights.

Following is a simple illustrated breakdown of the process.

unblock_lib_snap01 unblock_lib_snap02 unblock_lib_snap03 unblock_lib_snap04 unblock_lib_snap05If your IT department has block your admin privileges don’t expect this to work. However it has been tested successfully on personal pc’s, on Windows 7, 8.1 and 10. Special thanks to Angel, Mateusz, Michael, Stamatis and Andrea for beta testing.

I will upload the c# code on my github account. So any takes and tweaks are more than welcome.

The code utilizes extracts from these two discussions

Beware that this code might not be bug free (most probably is full of bugs) and I do not take any responsibility if you destroy your computer.

You can grab the def and an extra GH user object to keep this snippet constantly in your GH toolbox here.

unblock_lib_def

Enjoy. 😉

 

Dynamic Display Mode Attributes Rhino / GH

hex-grids_dif_widths_dsI dug out an old C# code snippet to change the display widths of curves in the Rhino viewport, by dynamically altering the visual style attributes.

It was an answer for a question on the Grasshopper3d forum (see the thread here), so I thought of tidying up the code and posting it here (probably just to convince myself that I still have the time to be posting things).

Not much to say, just change how your curves look in the rhino viewport through Grasshopper. I am sure that someone might find it useful.

Code in Python below and the file as usual at the [Sub]Code page.

d_disp_snap01_DS

#[Dynamic Display Attributes] was written by Marios Tsiliakos on 21 / 06 / 2015 in grasshopper 0.9.0076
#This definition is for the pubic domain feel free to use it , share it and modify it, by providing proper citing.
#The author, Marios Tsiliakos of digitalsubstance.wordpress.com , makes no warranty, expressed or implied,
#as to the usefulness of the software and documentation for any purpose.If this tool is used for commercial purposes please notify the author.
#This work is licensed under a Creative Commons Attribution-Share Alike 3.0
#for more information visit www.digitalsubstance.wordpress.com.

import Rhino.Display as rd

# create some empty arrays
display_modes  = []
display_modes_names  = []
#retrieve the list of Display modes on the document
display_modes =  rd.DisplayModeDescription.GetDisplayModes()
#find out their names
for i in range(len(display_modes)):
    display_modes_names.append(display_modes[i].EnglishName)

#pick the display style that you want
for j in range(len(display_modes)):
    if display_modes_names[j] == mode:
        disp_index = j
    else:
        continue
selected_mode = display_modes[disp_index]

if execute == True:
    selected_mode.DisplayAttributes.CurveThickness = width
    rd.DisplayModeDescription.UpdateDisplayMode(selected_mode)
    print("Changed the display option settings for visual style :  " + y)
else:
    selected_mode.DisplayAttributes.CurveThickness = 1
    rd.DisplayModeDescription.UpdateDisplayMode(selected_mode)
    print(y+ " Display style reset to default settings")