Python script to Rhino button

From another thread jeff_hammond was kind to share a python script [! _Line _BothSides] . 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

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

! _-RunPythonScript (

<paste entire script in here>

)

HTH, --Mitch

3 Likes

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

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

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

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:

MyScriptName()

HTH,

–Mitch

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

! _-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()

)

Thanks for the help

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.

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

if __name__ == '__main__: 

from the last lines. Just

main()

-Pascal

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

import sys
print sys.path

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 (714 Bytes)

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

Oh I see. I didn’t realize that script had a dependancy. That finger script Is in a folder on my desktop.finger.py (3.0 KB)

I refer you to my answer above to find a better location :wink:

Oh I see thank you that worked.

1 Like

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

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

Ahh… is this an external CPython environment?

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

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

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

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

Before your import statements

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