- Entering individual commands in the interpreter or the console is tedious and error prone. Therefore it is better to save the instructions to a file and run that file in the interpreter. As a quick start we present and discuss a template.
Template for exercises
First of all we show the template as a whole. The same template is listed in the appendix, so if you need a new fresh file also have a look there sample code (snippets).
A Python script usually contains multiple lines of code. To help organize your code, you can logically group code together. This is called a function. A function has a name by which it can be called in order to execute the containing code. A function with a good name can make code easier to understand. With comments within the function you can describe what a specific piece of code does.
Three functions are defined in the template, each starts with the keyword def followed by the name of the function.
After the name of the function a list of parameters in braces is possible, if left empty (like in our template), the definition of the function ends with a pair of empty braces. The colon at the end of the line starts a new code block - this is not only true for functions - all kinds of blocks can be started with a colon.
All following lines (but at least one line) are indented with four spaces. If you want to end the block (in this case the function) you do that by unindenting the code that should not be part of the block anymore.
With this cool indentation trick it is not necessary to use curly braces or other constructs to define code blocks.
Have a look to the lines 10 to 13:
This function consists of two lines. The first is a so called »docstring« followed by the command pass. Because of the rule: every block needs at least one line, we use the keyword pass and it does what it should do: nothing, but the block building rule is satisfied.
Calling a function
A function is called by writing its name followed by a pair of braces like in line 29-31. The last two lines thought are commented out and won't be executed. If you remove the number signs (#) at the beginning of the line, those functions will also be executed.
In the function named »function_2« we use the function print. The result is visible in a console.
The first print gives us the name of our script, the second returns fifty stars.
Two ways to use a script
Line number 27 contains a little bit of magic! In the if-statement we check the content of a variable named »__name__«. This variable is set automatically by the Python interpreter depending on how the file is run. Two values are possible:
- the name of the file
- the name »__main__«
If the value is »__main__«, all indented commands after the colon are executed - so the colon starts a new block here. If the value is different the commands in the block are not executed. With this trick you are able to run your file as a script that calls the functions directly and reuse the functions defined in your script from a different file by importing it.
Module specific functions
In the function »function_3«. we call a special Blender function. its name is »primitive_cone_add« and part of an object hierarchy. The path to the function is constructed with dots as delimiters. functions which are part of objects are called methods. The method »primitive_cone_add« needs one parameter to place the result of the method call in our 3D-World. Have a look in the 3D Window after execution.
You can use elements from other files by importing them. This helps you to organize your code in several files and makes code reusable. This also gives you the possibility to use e.g. the functions of Blender in your own modules by importing them: