algorithmic modeling for Rhino

How do you find the grasshopper filename in python? I want to build a python version of David's script here:

The script is written in VB and has the following lines

File = GrasshopperDocument.FilePath
Folder = IO.Path.GetDirectoryName(GrasshopperDocument.FilePath) & IO.Path.DirectorySeparatorChar

But I cant figure out how to call up the current GH_Document with Python.


Views: 3626

Replies to This Discussion

Hi Ryan,

Try File = ghenv.LocalScope.ghdoc.Path

ghenv.LocalScope should be only used for scope manipulation. Of course, given that it is the local scope, it will contain all variables of it. Including ghenv. So you could write ghenv.LocalScope.ghenv.LocalScope... But please rather use ghdoc.Path :)

Giulio Piacentino
for Robert McNeel & Associates

A little wordy, but:


should work.

Ha! Mostapha beat me to it with a more concise answer :D

Haha! Well I just learned where can I find the right OnPingDocument method for the GHPython component... Thank you for that. :)

Hey d00dz

I'm trying to pull a bunch of metadata from different locations - here's what I've got:

"""Displays document metadata
f: file name
d: document description
a: file creator
c: creation date
p: file path
r: revision count
v: Grasshopper version
l: list of add-ons used

def Main():
global f, d, a, c, p, r, v
f = ghenv.LocalScope.ghdoc.Name
d = ghenv.Component.Attributes.Owner.OnPingDocument().Properties.Description
a = gh.CentralSettings.AuthorName
c = ghenv.Component.Attributes.Owner.OnPingDocument().Properties.Date
p = ghenv.LocalScope.ghdoc.Path
r = ghenv.Component.Attributes.Owner.OnPingDocument().Properties.Revisions.Count
v = gh.Versioning.Version
l = 1


The first question is that I'm bouncing between gh. and ghenv. for different things - can I/should I be more consistent? Are there consequences?

The second question is that I'm trying to pull the list of add-ons (variable "l") used in a definition. This data exists in the ghXML in the Library chunk:

<chunk name="Library" index="0">
<items count="4">
<item name="Author" type_name="gh_string" type_code="10"></item>
<item name="Id" type_name="gh_guid" type_code="9">9d96da9c-9354-ef32-7983-0acb11a3d493</item>
<item name="Name" type_name="gh_string" type_code="10">LunchBox</item>
<item name="Version" type_name="gh_string" type_code="10">2014.4.27.0</item>

Any ideas on how to get that bad boy?



Oh there's an "import Grasshopper as gh" in there btw

Hi Brian,

this post is old; maybe may I ask to start a new discussion next time?
On the part that you wrote, it can slightly be shortened to:

f = ghdoc.Name
d = ghenv.Component.OnPingDocument().Properties.Description
a = gh.CentralSettings.AuthorName
c = ghenv.Component.OnPingDocument().Properties.Date
p = ghdoc.Path

About i, I would wait for David to tell the right approach, as there might be some Grasshopper-SDK-specific method to query, which I am not aware of. Or maybe we have to just go though all document objects and check if they belong to external libraries. Let's see what David says on this,

Giulio Piacentino
for Robert McNeel & Associates

It's not particularly easy, but it is reasonably straightforward. The attached file contains a C# script component which outputs all libraries used in the current file (separated into core and add-on libs).


David Rutten


Same in Python, following David's sample.

Giulio Piacentino
for Robert McNeel & Associates


Thank you David and Giulio!

Giulio, I started a new forum post with a few follow-up questions here:

Hi Ryan

contains a reference to the current Grasshopper document (solution). By accessing its Path attribute, we could get the location where the file is saved. As you can see in other answers, all of which are working and are good, other ways are possible. They are side-effects of providing a reference to the component (which can understand where it is placed) and ghenv.LocalScope, which shows which variables are defined in the last iteration (scope). So,

import os

if ghdoc.Path:
    a = os.path.dirname(ghdoc.Path)

should get your folder.

I hope it helps,
Giulio Piacentino
for Robert McNeel & Associates






  • Add Photos
  • View All


  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service