RUG/Pennsylvania/State College/Software/Python scripts

From RepRap
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. You can change the speed and course of the finger at the beginning of the script, which can be modified with any text editor. On Unix systems, the command line will be (once the terminal is set to the right folder):

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

Gradient grid

Download: GcodeGrid.zip

This script generates a gcode file to print a 3D grid of variable spacing. It uses Python 3.x, and expects two arguments: the path of the folder where the gcode must be created, and a name for the gcode file (including the extension .gcode). This script uses two files, which must be put in the same folder. On Unix systems, the command will be (once the terminal is set to the right folder):

python3 grid.py path/to/the/target/folder filename.gcode

The script generates the exact same grid as the gradient-grid python script for Blender, but creates a gcode file instead. Slic3r does a bad job with bridging when slicing this object, which is why we created this script.

Python scripts and Blender

Blender (http://www.blender.org) is an open-source modeling 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

Turn any editing window to a Python console using Shift + F4. Then, use the following commands:

Set the variable 'filename' to the file path you want to use:

filename = "/path/to/the/python/script"

Run the script:

exec(compile(open(filename).read(), filename, 'exec'))

Useful scripts

Gradient

Download: Gradient.zip

This file contains three different python scripts to generate gradients in Blender.

  • Gradient-grid generates a 3D grid with variable spacing, intended for one-extruder prints.
  • Gradient-dot generates a gradient made of small cubes, intended for two-extruders prints.
  • Gradient-bar generates a gradient made of small bars, intended for two-extruders prints.

Constants can be defined in the beginning of each script to change the size of the resulting object, or the number of bars/cubes.