RUG/Pennsylvania/State College/Software/Python scripts

From RepRap
Revision as of 17:30, 22 January 2014 by Aem28 (talk | contribs)
Jump to: navigation, search

Introduction

Python is an open-source programming language. Its syntax is simple, however it includes many powerful tools, making it easy to create and edit files through a script. Python in the RepRap context can help generating or editing gcode, or generating 3D objects.

Two branches of Python are being used: Python 2.x and Python 3.x (more details: http://wiki.python.org/moin/Python2orPython3). Their syntax is slightly different, and a script written in one version won't work with the other. The version used should be specified at the beginning of the python file holding the script. Both versions can be downloaded at http://www.python.org/download/.

Most Unix systems (MacOs X, Ubuntu, Fedora...) come with Python 2.7 packages installed, so you won't need to install them again. However, you'll need to install Python 3.x to run Python 3.x scripts. Both versions of Python can co-exist on a computer without creating problems.

Python scripts and command line

Once Python is installed, you can write and run scripts from the Python software. However, it's generally easier to use the command line interpreter to run scripts, especially when one wants to edit another file.

On Windows

To run a Python script on Windows, open the command interpreter (look for cmd.exe). Then, use the following commands:

Change the current directory to the directory where the script is. Note that cmd.exe will write the path for you if you drag and drop a folder in its window.

cd path/to/the/script/folder

Enter the name of the python script, with its extension. Some scripts also required arguments, which must be added after the name of the script, with a white-space between them.

example.py argument1 argument2

On Unix systems (Mac and Linux)

To run a Python script on Mac or Linux, open the terminal (located in the Utilities folder on MacOs X). Then, use the following commands:

Change the current directory to the directory where the script is. Note that the terminal will write the path for you if you drag and drop a folder in its window.

cd path/to/the/script/folder

Enter the name of the python script, with its extension. Some scripts also required arguments, which must be added after the name of the script, with a white-space between them. To tell the system that you want to run a 2.7 Python script, add 'python' before the name of the script. If you want to run a Python 3 script, add 'python3'.

python3 example.py argument1 argument2

Useful scripts

Intersections

Download: Intersections.zip

Given a gcode file, this script determines the number of crossings inside the part, and prints it in the command window. This script uses Python 2.x, and expects one argument: the path to the gcode file. This script uses three files which must be put in the same folder. On Unix systems, the command line will be (once the terminal is set to the right folder):

python intersections.py /path/to/the/gcode/file

Camera

Download: Camera.zip

Given a gcode file, this script will add a bunch of code every layer to take a photo with a stepper-motor (learn more about this system: Stepper motor camera). A backup file of the original gcode will be created. This script uses Python 3.x, and expects one argument: the path of the gcode file. This script uses two files, which must be put in the same folder. On Unix systems, the command line will be (once the terminal is set to the right folder):

python camera.py /path/to/the/gcode/file

Python scripts and Blender

Blender is an open-source modelling software. Python commands can be used in Blender to generate objects. This is useful for objects based on geometric patterns, such as grids. Blender comes with the capacity to use Python 3 scripts.

Use a script in Blender

Useful scripts