1-Hello World

Running the code

  1. Download the file animate.py for this tutorial,
  2. Place the file in the extensions folder (you can override the existing file)
  3. Open Inkscape if it is not open already
  4. Run the script by going under extensions  click on animation under Animation tools
  5. The following will pop up
Hello World Message Box

Understanding the code

Here we are going to build on what we have done in the setting up process.
The .inx is going to stay the same.
We are just going to Modify the .py file to display a Message Box

When I write code, I always write a brief description of what I am trying to Achieve:
here:
CREATE A MESSAGE DISPLAYING "HELLO WORLD!" and I keep this description inside the file to remind me later on what I am was to achieve at the time,
and reuse it if need be.

So we go from this:

#!/usr/bin/env python

import inkex

class Animate(inkex.Effect):
  def effect(self):
    #Act 1: CREATE A MESSAGE DISPLAYING "HELLO WORLD!"
    return

a = Animate()
a.affect()

we add the following instruction inkex.debug("Hello World!!!")
and our program should now look like this:

#!/usr/bin/env python

import inkex

class Animate(inkex.Effect):
  def effect(self):
    #Act 1: CREATE A MESSAGE DISPLAYING "HELLO WORLD!"
    inkex.debug("Hello World!!!")
    return

a = Animate()
a.affect()

Do the modification and save the file.  Run the code again by click on animate in Animation tools under the extensions Tab

And Voila! you should see the Message box below:




Understanding the code

We have not changed the .inx file so I will not be giving more explanation
So let's have a look at the python file again, this time I have added comments (lines which start with #) to explain what each line does.
Comment do not affect your code so you can write whatever you think is necessary
But Mind your TAB Spaces!!!
The tab spaces before each lines are very important as it is part of the python syntax. There is one Tab space before def and two tab spaces before index.debug and return.
If you forget to put these spaces the program will fail.
Here you don't have to add new lines but it is something to bear on mind for the future.

#!/usr/bin/env python

import inkex # import the inkex Module so that we can use the debug function below

class Animate(inkex.Effect): #This is your program

  def effect(self): #This is a function this is were you define what your program in going to do
    #Act 1: ------------------DISPLAY A TEXT SAYING "HELLO WORLD" IN A MESSAGE BOX
    inkex.debug("Hello World!!!") #Create the message box with the Hello world message
    return #end of the program

a = Animate() # Create an intance of your program
a.affect() #run the program

As you can see it is smaller then the inx. but no worries it will be significantly bigger as we do more things!
This is the line which create the message box
inkex.debug("Hello World!!!") #Create the message box with the Hello world message

The best way to understand how something works is to play with it!!
If you change any of the text inkex.debug() will receive an error message.

But you can change anything in between the double quote: "Hello World!!!"
If you want your program to say something different you change this :
inkex.debug("Hello World!!!")  into this inkex.debug("Something different!")

When you have done the changes save you file, go back to Inkscape and run you extentions again.
(Luckily you don't need the restart Inkscape every time you make a change )

If this works and "something different" is displayed, Congratulation!!!, you have stepped into the really intricate unique and wonderful world of programming extensions for Inkscape.
If not check the syntax again and rerun your code.

This program is very simple but also very important for what is going to happen from now on.
I constantly use the debug function when I write extensions.

Now Let's create some friendly vector shapes and get them to talk to us!!!

But before we do so here a little challenge for you:

Challenge:

(EASY STUFF)

  1. Add an extra debug message which displays: "Inkscape really is Awesome"

Tips: you can to add this line of code before or after the Hello Word debug message.
Reminder: Mind your TAB spaces!!!

Next: Act 2: AND....WHO ARE YOU?
or
Back to the Index







No comments:

Post a Comment