Chapter 5. Scripting

Kig allows you 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 a similar functionality (the GNOME program Dr.Geo).

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

Suppose you were not aware of the Kig built-in type Mid Point, 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 ObjectsOtherPython Script 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 notice 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 we want to define is called calc, and in our case it accepts two arguments. These are the objects you have selected as arguments in the previous screen. You need as many 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 define. In our case, this is a Point object. The two arguments are also Point objects, and we can use the Point.coordinate() function to define the coordinates of the two given points.

The calculation necessary in our example is very simple, we simply add the two sets of coordinates, and divide the new set by two. We then construct a new point using the result. 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, the new object will appear in the Kig document. If you move one of the points, the newly created point will move along with it. Much 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 a Point. Many more classes and methods are available in the Kig Python code, and a more complete reference is provided on the Kig website.