# Get python code from grasshopper node as plain text

**URL:** <https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840>\
**Category:** Grasshopper\
**Tags:** windows, rhinocommon, rhino7\
**Created:** [June 1, 2023, 12:27pm UTC](https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840 "2023-06-01T12:27:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephan\_Wegewitz](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/stephan_wegewitz/32/768105_2.png) [@Stephan\_Wegewitz](https://discourse.mcneel.com/u/Stephan_Wegewitz)\
**Post date:** [June 1, 2023, 12:27pm UTC](https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840/1 "2023-06-01T12:27:29Z")

</div>

Hello,

i’m part of a team which uses regularly grasshopper, Ironpython, and Rhinocommon in our workflow. We want to analyse our codebase, which is saved as python files but also as python within the Grasshopper script files. Is there a possibility to acces a \*.gh file from outside of Rhino with Python or C# and find Nodes within a \*.gh file, which consist of python code and extract this code as plain text file.

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/6/0/8/608c0131e8f1cdc26daa6a88e8ff0b2618646fa2.jpeg)

Like in this screenshot someone of our team coded something in a node with python. Can we extract this python code as textfile?

---

<div class="post-metadata">

**Author:** ![michaelvollrath](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/michaelvollrath/32/479405_2.png) [@michaelvollrath](https://discourse.mcneel.com/u/michaelvollrath)\
**Post date:** [June 1, 2023, 12:42pm UTC](https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840/2 "2023-06-01T12:42:31Z")

</div>

In Python Editor-\>File-\>Export As-\>Save as type: All Files and add .txt at the end of your file name

![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/4X/4/c/2/4c27858f41822eaaed4422d6cb6591282ebce260.png)

---

<div class="post-metadata">

**Author:** ![Joseph\_Oster](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/joseph_oster/32/549124_2.png) [@Joseph\_Oster](https://discourse.mcneel.com/u/Joseph_Oster)\
**Post date:** [June 1, 2023, 12:42pm UTC](https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840/3 "2023-06-01T12:42:48Z")

</div>

If you save your GH files as ‘.ghx’ _(XML)_ instead of ‘gh’ _(binary)_, you can open the files with a text editor and find the Python bits.

```auto
                    <item name="CodeInput" type_name="gh_string" type_code="10">"""
Populate Value List
    Inputs:
        Name: {item,str}
        Keys: {list,str}
        Values: {list,str}
    Outputs:
    Remarks:
        Author: Anders Holden Deleuran (BIG IDEAS)
        Rhino: 6.30.20266.14531
        Version: 210108
"""

ghenv.Component.Name = "PopulateValueList"
ghenv.Component.NickName = "PVL"

import Grasshopper as gh

if not Values:
    Values = Keys
if Keys and Values:
    for obj in ghenv.Component.OnPingDocument().Objects:
        if type(obj) is gh.Kernel.Special.GH_ValueList:
            if obj.NickName == Name:
                selected = obj.FirstSelectedItem.Name
                obj.ListItems.Clear()
                for k,v in zip(Keys,Values):
                    vli = gh.Kernel.Special.GH_ValueListItem(str(k),str('"' + v + '"'))
                    obj.ListItems.Add(vli)
                if selected in Keys:
                    obj.SelectItem(Keys.index(selected))
                obj.ExpireSolution(True)</item>
                    <item name="Description" type_name="gh_string" type_code="10">Populate Value List</item>

```

---

<div class="post-metadata">

**Author:** ![AndersDeleuran](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/andersdeleuran/32/476683_2.png) [@AndersDeleuran](https://discourse.mcneel.com/u/AndersDeleuran)\
**Post date:** [June 1, 2023, 12:50pm UTC](https://discourse.mcneel.com/t/get-python-code-from-grasshopper-node-as-plain-text/160840/4 "2023-06-01T12:50:23Z")

</div>

Have a look at the code I posted here, it demonstrates how to get the code of GHPython components and export this to .py files/make user objects:

> [@Python components def RunScript empty outputs](https://discourse.mcneel.com/t/python-components-def-runscript-empty-outputs/156991/5):
>
> I hadn’t seen this before, looks cool but also quite convoluted. Depending on what your requirements are, two components I developed back at CITA might be enough. I don’t think I ever did publish the code outside the Smart Geometry/Advances in Architectural Geometry workshops in 2016, but here it is: """ Iterates the GHPython components in the document and makes user objects of them if the their Category property matches the input Category parameter. Also grabs all the code and writes this to …
