Difference between revisions of "Activities/Turtle Art/Plugins"

From Sugar Labs
Jump to navigation Jump to search
Line 77: Line 77:
 
* save as Physics project: save the current bodylist and jointlist as a Physics project in the Journal
 
* save as Physics project: save the current bodylist and jointlist as a Physics project in the Journal
  
 +
===== Where to get the Physics plugin =====
 
:[[File:Physics-plugin.tar.gz]] Physics plugin
 
:[[File:Physics-plugin.tar.gz]] Physics plugin
  
 +
===== Example =====
 
A simple gear and motor generated in Turtle Art:
 
A simple gear and motor generated in Turtle Art:
  

Revision as of 09:01, 17 November 2011

Plugins

As of Version 106, there is plugin support for Turtle Art. The basic idea is to let developers add new palettes and blocks to support additional functionality without having to make changes to any of the core Turtle Art packages. If a plugin is present, it is loaded when Turtle Art is launched and any palettes or blocks defined by the plugin are made available to the user.

The plugin mechanism is currently used to provide support for sensors, the camera, RFID, and the Media, Extras, and Portfolio palettes. A plugin has been developed for WeDo and additional plugins are being developed for Arduino, NXT, and GoGo.

How to install a plugin

Plugins are typically distributed as a *.tar.gz archive

Archivemanager.jpg

In Gnome, click on the link in your browser and open with Archive Manager, extract the files in the plugins directory of the archive to

/home/olpc/Activities/TurtleArt.activity/plugins

you may need to make the Activities directory writeable first, in Terminal type

chmod 777 /home/olpc/Activities

If your distribution does not include Gnome, the following commands in Terminal will download the Physics archive to the current directory

cd ~/Activities/TurtleArt.activity
wget http://wiki.sugarlabs.org/images/c/c1/Physics-plugin.tar.gz
gunzip Physics-plugin.tar.gz
tar xvf Physics-plugin.tar

Note that you should not need root access to install plugins into your home directory.

Available plugins

These plugins would typically already be installed

  • turtleart-extras (the extra palettes that distinguish Turtle Blocks from Turtle Art)
  • turtleart-camera
  • turtleart-sensors
  • rfid (This plugin allows interfacing with a RFID reader)

Project Butia

This plugin adds an extra pallette for the Butia robot (link to just plugin plse)

Currency

This plugin adds a pallette of currency (notes and coins), these items can be acted on by arithmetic operators

Oz currency.png

File:Oz-coins.tar.gz Australian coins plugin
File:Oz-bills.tar.gz Australian bills plugin
File:Oz-coins-and-bills.tar.gz Australian coins and bills plugin
File:Colombia-currency.tar.gz Colombian peso plugin
File:Mexican-currency.tar.gz Mexican peso plugin
File:Paraguay-currency.tar.gz Paraguay Gurani plugin
File:Rwanda-currency.tar.gz Rwanda francs plugin
File:UY-currency.tar.gz Uruguayan peso plugin
File:US-bills.tar.gz US bills plugin
File:US-currency.tar.gz US dollar plugin

Physics

This plugin allows you to construct a Physics model which can be saved to the Journal and run with the Physics Activity Activities/Physics

Physics pallette.jpg

Use these blocks to create objects that are added to the bodylist and jointlist of a Box2d database used by the Physics Activity. Objects are positioned by the turtle x,y and colored based on the current pen attributes. From left to right:

  • start polygon: use the current turtle x,y position to specify the first vertex of a polygon
  • add point: use the current turtle x,y position to add a vertex to a polygon
  • end polygon: use the current turtle x,y position to specify the last vertex of a polygon
  • end filled polygon: use the current turtle x,y position to specify the last vertex of a filled polygon
Note: The polygon must be 'normal', e.g., no crossed lines; no holes. Since Box2d does not support concave polygons, all polygons are converted to triangles (triangulation). (In Turtle Blocks, these triangles are shown by slight variations in color.)
  • triangle: add a triangle object at the current turtle x,y position
  • circle: add a circle object at the current turtle x,y position
  • rectangle: add a rectangle object at the current turtle x,y position
  • reset: reset bodylist and jointlist (clear all objects and reset default parameters)
  • motor: add a motor at the current turtle x,y position (attached to the object at this position)
  • pin: add a pin at the current turtle x,y position (attached to the object at this position)
  • joint: add a joint between the object at the current turtle x,y position and the object at x, y
  • density: set the object density (0 is light; 1 is heavy)
  • friction: set the object friction (0 is slippery; 1 is sticky)
  • bounciness: set the object restitution (0 is rigid; 1 is bouncy)
  • save as Physics project: save the current bodylist and jointlist as a Physics project in the Journal
Where to get the Physics plugin
File:Physics-plugin.tar.gz Physics plugin
Example

A simple gear and motor generated in Turtle Art:

TAGearTA.png TAGear.png

File:TAgear.ta File:Gear.physics

How to write a plugin

To add a plugin, simply drop a file into the plugins directory. The file must end with _plugin.py and the base class in the file must be descendent from the Plugin class defined in plugin.py

Note that the name of your new class much match the name of the file, but with the first letter capitalized, e.g.:

camera_plugin.py contains:

class Camera_plugin(Plugin):

Turtle Art called the __init__ method when starting up and traps import errors as its means to determine whether or not a plugin has the resources it needs to run. (You may want to remove this exception handler when debugging your plugin. It is in the _init_plugins method in tawindow.py.) It then calls the setup method when creating the palettes. It calls the start method whenever a stack of blocks is run and the stop method when execution is over. Also, there are methods for goto_background, return_to_foreground, and quit. (These methods are typically ignored.)

Adding a new palette is simply a matter of:

   palette = make_palette('mypalette',  # the name of your palette
                          colors=["#00FF00", "#00A000"],
                          help_string=_('Palette of my custom commands'))

For example, if we want to add a new turtle command, 'uturn', we'd use the add_block method in the Palette class.

   palette.add_block('uturn',  # the name of your block
                     style='basic-style',  # the block style
                     label=_('u turn'),  # the label for the block
                     prim_name='uturn',  # code reference (see below)
                     help_string=_('turns the turtle 180 degrees'))
   # Next, you need to define what your block will do:
   # def_prim takes 3 arguments: the primitive name, the number of
   # of arguments, 0 in this case, and the function to call, in this
   # case, the canvas function to set the heading.
   self.tw.lc.def_prim('uturn', 0, lambda self: self.tw.canvas.seth(self.tw.canvas.heading + 180))


That's it. When you next run Turtle Art, you will have a 'uturn' block on the 'mypalette' palette.

You will have to create icons for the palette-selector buttons. These are kept in the icons subdirectory. You need two icons: mypaletteoff.svg and mypaletteon.svg, where 'mypalette' is the same string as the entry you used in instantiating the Palette class. Note that the icons should be the same size (55x55) as the others. (This is the default icon size for Sugar toolbars.)