Wednesday, August 5, 2009

Getting started with Python

This week we are going to start our Python scripting. We are kicking of with a three day workshop. Today is focusing on the basics of Python scripting. I will go through all the steps below to set up your computer, since many of you will need to do this at home or on your laptop.

The first thing we will need to do is to get Python installed! (This is already installed in the labs.)
  • To install Python 2.5 (not Python 2.6 or 3.0):
  1. Download msi file
  2. Double click the file that you downloaded.
Then we need to install an IDE, which is an Integrated Development Environment. We will be using Eclipse. (This is already installed in the labs.)
  • To install Eclipse Galileo:
  1. Download zip file
  2. Unzip the file anywhere on your hard disk - e.g. in C: drive
  3. To start Eclipse, double click the eclipse.exe file
The we will need to install a couple of plugins so that Eclipse can be used for writing Python scripts. Installing plugins in Eclipse is very easy -
  • To install PyDev
  1. Start Eclipse
  2. In the menu, go to Help > Install new software
  3. Click the Add button
  4. In the Add Site dialog box, enter:
    Name: PyDev
    Location: http://pydev.sourceforge.net/updates/
  5. Then click install...
  • To install PyDev Extensions
  1. Start eclipse
  2. In the menu, go to Help > Install new software
  3. Click the Add button
  4. In the Add Site dialog box, enter:
    Name: PyDev
    Location: http://www.fabioz.com/pydev/updates
  5. Then click install...
Now we will now need to configure a new Eclipse workspace and project. First you need to tell Eclipse where Python is installed. Then you need to create a new project. And finally, you need to tell Python where the new project is by adding it to the PYTHONPATH.
  • To tell Eclipse where Python is:
  1. Start eclipse
  2. In the menu, go to Window > Preferences
  3. In the preferences dialog box, navigate to 'Pydev > Interpreter - Python'
  4. Click the 'New' button
  5. In the select Interpreter dialog box:
    Interpreter Name: Python25
    Interpreter Executable: click 'Browse' and navigate to find Python.exe
    C:\Python25\python.exe
  6. Click 'OK' and 'OK' again
  • To create a new project
  1. Start eclipse
  2. In the menu, go to File > New > Pydev Project
  3. In the dialog box:
    Project name: type any name
    Use default: tick to use your default workspace
    Choose Project: Python
    Grammar Version: 2.5
    Interpreter: Python25
    Create default 'src'...": untick this - it is not needed
  4. Click Next and Finish
  • To tell the Python where your project is:
  1. In the Pydev Package Explorer, find your project folder
  2. Right click the folder and select 'Properties'
  3. Click on 'PyDev - PYTHONPATH'
  4. Click the 'Add source folder' button
  5. Click 'OK'
Now the project should all be set up and ready to start coding. First test that everything is working by writing a little 'Hello World' script. Then, in order to communicate with Windows, you will need to install pywin32. And finally, you will need to set up py2rhino and py2ecotect.
  • To test that everything is working:
  1. In the Pydev Package Explorer, find your project folder
  2. right click the folder and select 'New > Pydev Module'
  3. In the dialog box:
    Source Folder: leave it as it is
    Package: leave it as it is, or add a name if you want a sub folder
    Name: enter the name of your script, ege. 'test'
  4. Click 'Finish'
  5. In the script file, type ' print "Hello World" '
  6. Click the run button (or go to Run > Run)
  7. Check that you see 'Hello World' in the output console
  • To install pywin32 (can only be installed on windows):
  1. Download the pywin32 file fpr Python25:
    http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/pywin32-214.win32-py2.5.exe/download
  2. Run the exe by double clicking the file
  • To install py2rhino and py2ecotect:
  1. Go to the download page for design-automation:
    http://code.google.com/p/design-automation/downloads/list
  2. Download the latest version of py2rhino orpy2ecotect
  3. In Windows explorer, navigate to the Python sitpackages folder
    C:\Python25\Lib\site-packages
  4. Delete any older versions by deleting the py2rhino or py2ecotect folders
  5. Then unzip the py2rhino or py2ecotect into the site-packages folder
  6. Now go back to eclipse and refresh eclipse as follows:
    In the menu, go to Window > Preferences
    In the preferences dialog box, go to PyDev > Interpreter - Python
    Click the 'Apply' button
  • Now test that you can draw something in Rhino
  1. Start Rhino
  2. Create a script file in eclipse
  3. Type:

    import py2rhino as p2r
    p2r.obj.Line.create( (0,0,0), (5,5,0) )
    print "done"

  4. Click the run button (or go to Run > Run)
  5. Check that you see a line in Rhino and the 'done' is printed to the output console

No comments:

Post a Comment