Chapter 5. Scripting

Kig allows the user to create custom types in the Python scripting language. This is a very advanced feature, and I know of only one other Interactive Geometry program that has similar functionality (the GNOME program Dr.Genius).

Python Scripting in Kig basically allows you to create your own way to define an object from certain parent objects. For example, if you are a math teacher, and you have some fancy way of calculating an interesting point of a conic, then instead of messing with complex constructions and macro's, you could just write down in Python code how the point is to be calculated and Kig will show it for you.

Suppose you were not aware of the Kig builtin MidPoint type, and you wanted to show the midpoint of two given points. You would then click on the Python Script button in the toolbar, or select Objects->Other->Script Object from the menubar. You are then presented with a wizard that allows you to proceed.


The Script Object Wizard

The first thing you have to do is select the arguments for the script object. In our example, this means the two points of which we want to show the midpoint. Select them in the Kig main window, and click Next to proceed.

Now you are presented with a text edit box where you can enter the code for you script object. Template code and some comments are already in place. It is important to make sure that your code is valid Python code. People familiar with Python will note that we are actually defining a Python function called calc. It is therefore necessary to adhere to the Python rules for defining functions. For example, every line of the function should start with a Tab. The first line not starting with a tab ends the definition of the function.

The python function that should be defined is called “calc”, and in our case accepts two arguments. These are the objects you have selected as arguments in the previous screen. There will be as much arguments as you have selected there. They are called arg1 and arg2, but you can change their names to something more meaningful if you want.

In the function, you can do all sorts of calculations that you deem necessary, using the two arguments if needed. You should return the object you want to appear. In our case, this is a Point object. The two arguments are also Point objects, and we can use the Point.coordinate() function to get hold of the coordinates of the two given points.

The calculation necessary in our example is very simple, we simply add the two coordinates together, and divide the result by two. We then construct a new point using that coordinate. The python code needed is:

   def calc( a, b ):
        m = ( a.coordinate() + b.coordinate() ) / 2;
	return Point( m )

Entering the code for the midpoint in the Script Object wizard.

If you now click the Finish button, then the new object will appear in the Kig document. If you move one of the points, then the newly created point will move along with them. A lot more powerful objects can be built in this way, you are encouraged to try it out.



	    The newly constructed Script Object.

All objects in Kig can be used in the Python code. As we have seen above, points are of the Point class, and you can use e.g. the Point.coordinate() method. You can also return all kinds of objects, not just Points. Many more classes and methods are available from the Kig Python code, and a more complete reference on them is provided here.