# Python script to Rhino button

**URL:** <https://discourse.mcneel.com/t/python-script-to-rhino-button/38806>\
**Category:** Rhino for Windows\
**Created:** [November 16, 2016, 8:16am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806 "2016-11-16T08:16:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Et\_Rec](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/et_rec/32/757525_2.png) [@Et\_Rec](https://discourse.mcneel.com/u/Et_Rec)\
**Post date:** [November 16, 2016, 8:16am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/1 "2016-11-16T08:16:11Z")

</div>

From another thread jeff\_hammond was kind to share a python script [[! \_Line \_BothSides](https://discourse.mcneel.com/t/-line--bothsides/37555/19)] . I’m trying to create a button for it. Viewed the script in the Rhino Python editor, but don’t know what to paste in the button window. Have zilch experience with scripts and little in creating my own button.  
A bit off topic , but I would also like to create another button for the test commands in Rhino 5 " \_TestRemoveNakedMicroLoops and \_TestRemoveAllNakedMicroLoops " . Preferably in one one button, left and right click for the 2 commands …Thanks for any help

---

<div class="post-metadata">

**Author:** ![Helvetosaur](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/helvetosaur/32/635974_2.png) [@Helvetosaur](https://discourse.mcneel.com/u/Helvetosaur)\
**Post date:** [November 16, 2016, 8:19am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/2 "2016-11-16T08:19:43Z")

</div>

Create a new blank button, open the editor then put in the following:

```auto
! _-RunPythonScript (

<paste entire script in here>

)

```

HTH, --Mitch

---

<div class="post-metadata">

**Author:** ![Et\_Rec](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/et_rec/32/757525_2.png) [@Et\_Rec](https://discourse.mcneel.com/u/Et_Rec)\
**Post date:** [November 16, 2016, 8:33am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/3 "2016-11-16T08:33:04Z")

</div>

The script seems very long and I’m not sure what to copy and paste. Thanks for the reply

---

<div class="post-metadata">

**Author:** ![Helvetosaur](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/helvetosaur/32/635974_2.png) [@Helvetosaur](https://discourse.mcneel.com/u/Helvetosaur)\
**Post date:** [November 16, 2016, 8:48am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/4 "2016-11-16T08:48:52Z")

</div>

> [@Et\_Rec](#):
>
> The script seems very long

Length doesn’t matter, just paste it all in there.

> [@Et\_Rec](#):
>
> not sure what to copy and paste.

If the script was posted as a .py file and it runs from the script editor, you don’t need to add anything. Open the file in a text editor, Ctrl+A to select all, Ctrl+C to copy the whole text, switch to the button editor. Type in the first line or copy it from here :

`! _-RunPythonScript (`

Then return to get a new line and Ctrl+V to paste the text in.

Then another return and type a close parentheses alone on one line, that will be the end.

Then OK to close the button editor.

One thing to watch out for - some people have the habit of ending their Python scripts with

```auto
if __name__ == ' __main__':
    MyScriptName()

```

This runs in a script editor, but unfortunately does not work inside a button. so you will have to **_remove_** the entire `if __name__ == ' __main__':` line **_and_** outdent the `MyScriptName()` all the way to the left (usually 4 spaces) so the last line of the script looks like this:

```auto
MyScriptName()

```

HTH,

–Mitch

Edit: here is a typical script to paste into a button to test

```python
! _-RunPythonScript (

"""Hello test script"""
import rhinoscriptsyntax as rs

def HiThere(name):
    if name:
        return "Hello {}, this script runs fine.".format(name)

def RunMyScript():
    your_name=rs.StringBox("What is your name?")
    if your_name:
        ret_msg=HiThere(your_name)
    else:
        ret_msg="Sorry, I didn't get your name"
    rs.MessageBox(ret_msg)
RunMyScript()

)

```

---

<div class="post-metadata">

**Author:** ![Et\_Rec](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/et_rec/32/757525_2.png) [@Et\_Rec](https://discourse.mcneel.com/u/Et_Rec)\
**Post date:** [November 16, 2016, 8:52am UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/5 "2016-11-16T08:52:21Z")

</div>

Thanks for the help

---

<div class="post-metadata">

**Author:** ![okeating](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/okeating/32/683235_2.png) [@okeating](https://discourse.mcneel.com/u/okeating)\
**Post date:** [August 27, 2019, 8:05pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/6 "2019-08-27T20:05:28Z")

</div>

I know I’m bringing back an old thread but this seems to be doing exactly what I’m trying to do.  
I’m trying to add a python script to a button and I’ve followed the advice here but it’s throwing an error. Here’s the script:

```
    ! _-RunPythonScript (
import rhinoscriptsyntax as rs
from collections import defaultdict
from collections import Iterable
import Rhino

"""
# notes
- doesn't handle laser curf currently. to do this, simple scale alternating
  cylinders along the intersection axis

"""

import finger
reload(finger)

def main():
  # first, select objects in three orthogonal planes
  positive = rs.GetObjects("select plane 1 objects", filter=16) # polysurface
  negative = rs.GetObjects("select plane 2 objects", filter=16)

  subdivisions = rs.GetInteger(message="enter subdivisions (odd)", number=3, minimum=2, maximum=None)
  finger.make_fingers(positive, negative, subdivisions)

  finger.perform_subtraction()

if __name__ == ' __main__':
  main()
)

```

This is the error I’m getting.

 ![27%20PM](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/e/a/ea4b075f635230862b54d2b1a43348c33bcd1cc5.png)

---

<div class="post-metadata">

**Author:** ![pascal](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/pascal/32/479395_2.png) [@pascal](https://discourse.mcneel.com/u/pascal)\
**Post date:** [August 27, 2019, 9:16pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/7 "2019-08-27T21:16:22Z")

</div>

Hello - I need your finger script to test, but one thing is, for running on a button, remove the

```auto
if __name__ == ' __main__ : 

```

from the last lines. Just

```auto
main()

```

-Pascal

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 27, 2019, 9:23pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/8 "2019-08-27T21:23:23Z")

</div>

Hello,

You need to have your ‘finger’ script on the Python path. Run this on a button to print a list of valid directories where you could save finger.

-Graham

```python
import sys
print sys.path
```

---

<div class="post-metadata">

**Author:** ![okeating](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/okeating/32/683235_2.png) [@okeating](https://discourse.mcneel.com/u/okeating)\
**Post date:** [August 28, 2019, 1:35pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/9 "2019-08-28T13:35:16Z")

</div>

I changed the ending on the button as you suggested but it’s still not working. Here’s the script I’m working with.  
[two.py](https://discourse.mcneel.com/uploads/short-url/5VKJcjIACyBnm3J8lEiIUmkDaqN.py) (714 Bytes)

---

<div class="post-metadata">

**Author:** ![Helvetosaur](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/helvetosaur/32/635974_2.png) [@Helvetosaur](https://discourse.mcneel.com/u/Helvetosaur)\
**Post date:** [August 28, 2019, 1:37pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/10 "2019-08-28T13:37:04Z")

</div>

> [@okeating](#):
>
> I changed the ending on the button as you suggested but it’s still not working.

That’s because it can’t find ‘finger’… where is it located?

---

<div class="post-metadata">

**Author:** ![okeating](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/okeating/32/683235_2.png) [@okeating](https://discourse.mcneel.com/u/okeating)\
**Post date:** [August 28, 2019, 3:31pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/11 "2019-08-28T15:31:36Z")

</div>

Oh I see. I didn’t realize that script had a dependancy. That finger script Is in a folder on my desktop.[finger.py](https://discourse.mcneel.com/uploads/short-url/2LCoFN1XsMadJ1dEuN4FTQAcjUz.py) (3.0 KB)

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 28, 2019, 5:46pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/12 "2019-08-28T17:46:01Z")

</div>

I refer you to my answer above to find a better location 😉

---

<div class="post-metadata">

**Author:** ![okeating](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/okeating/32/683235_2.png) [@okeating](https://discourse.mcneel.com/u/okeating)\
**Post date:** [August 28, 2019, 6:05pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/13 "2019-08-28T18:05:07Z")

</div>

> [@Dancergraham](#):
>
> import sys print sys.path

Oh I see thank you that worked.

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 31, 2019, 3:40pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/15 "2019-08-31T15:40:11Z")

</div>

Hello,

Sorry I was not very clear.

My suggestion was to print sys.path in order to see which directories you can put python files for import. Then put your .py file in one of those directories and the import should work.

You can also get the list via the python file editor in Rhino

Does that help?

Graham

---

<div class="post-metadata">

**Author:** ![keerthana](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/keerthana/32/566379_2.png) [@keerthana](https://discourse.mcneel.com/u/keerthana)\
**Post date:** [August 31, 2019, 3:43pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/16 "2019-08-31T15:43:20Z")

</div>

I see, But I maintain my files in an environment and how can i ask Rhino to look at my environment path ?

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 31, 2019, 3:54pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/17 "2019-08-31T15:54:38Z")

</div>

Ahh… is this an external CPython environment?

---

<div class="post-metadata">

**Author:** ![keerthana](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/keerthana/32/566379_2.png) [@keerthana](https://discourse.mcneel.com/u/keerthana)\
**Post date:** [August 31, 2019, 3:56pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/18 "2019-08-31T15:56:04Z")

</div>

No its just pure python but i have created an environment for it

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 31, 2019, 4:01pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/19 "2019-08-31T16:01:59Z")

</div>

I don’t think that will be possible Python scripts run in the integrated Ironpython interpreter session

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [August 31, 2019, 4:21pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/20 "2019-08-31T16:21:14Z")

</div>

That said, if it is pure python 2 and if all the components work in the integrated Ironpython interpreter then you may just be able to open the rhino python editor, list the module paths and add your directory to the list…

Or programmatically you could try

```auto
if mypath not in sys.path:
    sys.path.append(mypath)

```

Before your import statements

---

<div class="post-metadata">

**Author:** ![keerthana](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/keerthana/32/566379_2.png) [@keerthana](https://discourse.mcneel.com/u/keerthana)\
**Post date:** [August 31, 2019, 5:21pm UTC](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806/21 "2019-08-31T17:21:35Z")

</div>

My script works when i check it on edit python script and also my python path has been linked to my environment but when i copy paste the scripts on the button command it can’t find my script

[Next page](https://discourse.mcneel.com/t/python-script-to-rhino-button/38806.md?page=2)
