What can Python do that GH cannot?

Hi, I am new.

I use grasshopper for pretty much everything, but often feel that it can become cumbersome. I have taken some intro python training, but have a long way to go. I would like to ask some opinions as to what I could use python for that grasshopper is not suited for.

Thank you in advance for your comments.
Erik

Making your own personalized Rhino commands that can be called from aliases or toolbar buttons, for example? Making stuff that is Mac Rhino compatible? Operations that edit objects that exist in the document, like selecting all curves with a certain linetype and changing that to another, or trimming everything inside/outside a set of closed curves while retaining their attributes like color, layer, etc.?

Certainly there is a lot of overlap and there are lots of things that are easier to set up in GH. But there are also things that are just faster/easier with a script, plus, you don’t need to have another window open, you can call up your functions just by typing a couple of letters on the keyboard.

–Mitch

Loops and recursion are two code concepts you cannot do in Grasshopper.

Well most grasshopper components have implied looping, that is you feed a list of objects into it and creates the loop for you and does the operation on each item in the list.

However, recursion is not possible, at least with base GH. ISTR thare was a plug-in for that, or you can write your own python, vb.Net, or C# component. But then we’re back to scripting again… Anyway, in order to understand recursion, one must first understand recursion… :smile:

–Mitch

@ Helvetosaur - thank you for your reply. Those concepts you speak of are very desirable. Now I sometimes use a GH component for linetypes - ‘Human’, but it is not the same as you speak of.

@ menno - that seems to be what everyone says, but what about ‘Anemone’? Are python loops different/easier to utilize than looping in GH?

You can run python scripts on Mac Rhino

Are python loops different/easier to utilize than looping in GH?

Yes it is different. If you have some experience programming, doing some things in GH seem difficult to do. An example I ran into the other week: If I want to take the last element from a list, in grasshopper you need to chain three blocks:

  • get the length of the list
  • subract 1
  • get the element with that index from the list

or maybe two:

  • reverse list
  • get first element

In Python it is

lastElement = mylist[-1]
1 Like

use an item block and set the number to -1. Keep wrap to true. There you go in one block :slight_smile:

1 Like

aside from what @stevebaer said (i’m on mac), i had a client that wanted a random hole pattern for steel table tops… they did a prototype in which it was drawn manually but i couldn’t bring myself to draw these things manually :slight_smile:

that said, maybe this could be done in grasshopper?

import rhinoscriptsyntax as rs
import math
import random


width = 32
length =96
edge = 2  # distance from edge to first row of centerpoints
space = 2 # maximum allowable distance between centerpoints
smallest = .1875  # smallest circle diameter
largest = .8125 # largest circle diameter
missing = 30 # percentage of filled holes


def holemaker():

    plane = rs.WorldXYPlane()
    rs.AddRectangle(plane, width, length)

    xcpt = math.ceil((width - (edge * 2)) / space)
    xspace = (width - (edge * 2)) / xcpt
    ycpt = math.ceil((length - (edge * 2)) / space)
    yspace = (length - (edge * 2)) / ycpt

    for i in range(0, int(ycpt) + 1):

        for q in range(0, int(xcpt) + 1):

            diam = random.uniform(smallest, largest)
            rad = diam / 2
            if missing == 0:
                rs.AddCircle((edge + (xspace * q), edge + (yspace * i), 0), .5)
                continue
            randtest = random.uniform(0, 100 / missing)
            if randtest <= 1:
                continue
            rs.AddCircle((edge + (xspace * q), edge + (yspace * i), 0), rad)


holemaker() 

Grasshopper actually excels at this kind of stuff, and plus you get a “live” preview of the result and can adjust visually to taste before you “bake”…

–Mitch

ouch… back to wishing GH was on mac then :smile:




(offtopic_ish)
at first, i tried to take it in a different direction using paneling tools but they weren’t having it. :smile:

The reality is that both gh and py can be considered programming languages and are equally able to do ‘things’. It is just a matter of ease of completing tasks where one may work better than the other.

1 Like

@menno and @Helvetosaur allude to it, but just so it’s clear, with the GHPython component installed you’ll get python embedded in Grasshopper. Best of both worlds!

Ladybug & HoneyBee are great examples of full blown Grasshopper component sets written in (GH)Python.

GHPython
LadyBug-HoneyBee

Probably the best answer along with the mac portability thing.
Yet, I would still like to give an example where Python clearly surpasses Grasshopper and that is: When handling huge and very detailed models.
I am not talking about a bunch of geometry in a Rhino file. I am talking about large scale architecural models detailed with every single screw.

I think a very nice concept of Grasshopper is that GH actually implies consistency through recomputing the solution every time a parameter changes (unless you disable the solver). But when it comes to these kinds of models this implicit consistency simply is too time consuming. I have recently worked on an architecural GH definition where the size of the main definition was about ~160 megs, countless of external referenced clusters etc.
We were at a point where we would actually wait half an hour just for the file to open. Each time you made edits you had to disable the solver and I could go on but I don’t want to bore you :wink:

My point is: If we had done all this building a project specific library of scripts to be executed on the model, always regenerating certain parts and stuff, everything would have been a lot better to work on, since we would not have to compute the whole model all over again just because we changed the diameter of one screwhole type. The downside of this is: We would not have had implicit consistency. So if we would have forgot to regenerate a certain part of the model after some values changed we would have had lots of wrong parts.

All in all: Working on crazy-huge Grasshopper definitions (even with state of the art machines) is just no fun anymore. So it really depends on the size of the project and the context if Grasshopper or Python makes more sense.