Python book of magic/02.Python Console

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Contents | Previous | Next

Eventually, the following question is asked. How is Python used? One way of using Python is inside one of Blender's window types, called the Python Console, as shown below:

Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:10:52)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>> 2*3+4
>>> 6/3

The text can be extensive. The first two lines are the version of Python and the version of the C compiler that was used to compile the Python source. The next lines, on the other hand, are much more interesting. Command history can browse through the commands that have already been entered, but for now, it's empty because it hasn't received any commands yet. Remove or deleting text is achieved by using backspace or delete. Each command is executed by hitting enter. Auto-complete can be ignored for now, and so can the rest of the Python libraries that have been loaded simply.

Basic Types[edit | edit source]

Integer Type[edit | edit source]

On the Python Console, there is a line that says “>>>”. This is short for, “after this, type the command”. Type 1+2 into the console window, then press enter to execute the command. The following will be seen:

>>> 1+2

Surprisingly, the first program was just made. The line of code consists of some data and a command. The data is "1" and "2", and the command is "+". "+" is also called an "operator", a mathematical operator in this case. Python includes addition, subtraction, multiplication, and division mathematical operators.

Example of multiplication:

>>> 2*3

Example of division:

>> 18/6

Example of subtraction:

>>> 18-6

String Type[edit | edit source]

Another Python command that can be used is the print() command. Below is an example of using the print() command:

File:Screen shot 2011-10-27 at 9.22.46 PM.png

This Python command is called a "function". This is considered a function because it includes a name, print, and opening and closing parentheses. That is the basic syntax for any Python function. However, it's not necessary to worry about what a function is exactly, for we will deal with it later.

The "this is pure Python magic" is called a string. What exactly is a string? Type the following into the Python Console, and then press enter to execute the command:

File:Screen shot 2011-10-27 at 9.30.02 PM.png

As shown above, the output is 12. Why is this? Because the command was not 1 + 2, but “1” + “2”. Since quotes surround each number, they are considered strings. Therefore, when using the addition operator, it combines the 2 strings into one string. That being said, "1" + "2" becomes "12". Now, type “1” * “2” into the Python Console, and press enter to execute the command.

File:Screen shot 2011-10-27 at 9.37.34 PM.png

The output results in what is called a Python error. Python errors are bad. Everyone wants to type perfect code, and nobody wants to experience all the pain of correcting mistakes. However, the world isn't perfect, and Python helps by reporting what is wrong.

The error may seem a bit cryptic, but it's not hard to decipher. The first line reports an error that was found, the second line reports where the error is, and the third line reports what the error is. In this case, the error is TypeError. “TypeError” means there is an error with the type. When reading the rest of the line, it becomes obvious that it expected data type “int” (short for integer), but it got data type “str”, which is short for string. A string is a 'string' of characters, one character after another. The “int” data type, short for integer, is one of the many types of numerical data. Types matter a lot for all programming languages. However, Python tends to be more forgiving with mixing different types in the same command. To help provide an example, type the following into the Python Console, and press enter to execute the command.

File:Screen shot 2011-10-27 at 10.01.15 PM.png

Now it works like a charm. Not only did the command contain the data type “str”, and also an “int” as expected. Another way of saying this is that the command provided the correct Python syntax. Type the following into the Python Console, and then press enter to execute the command:

File:Screen shot 2011-10-27 at 10.15.00 PM.png

The command included a string, and then “[0]”. The command, in English, means output the first letter in the string, which in this case, is "y". Though it would be easier to understand the number if it was one, it is 0 because it represents 'first'. Note that a character doesn't have to be a letter. It can be "!", "@", "4", or of course any mix of that.

2.2 Text editor[edit | edit source]

The Python Console really fun and useful, but scripts and add-ons rarely are one line long. That being said, something is required that will allow the typing and execution of multiple lines as one script. Luckily, the only necessary thing is a regular text file with a *.py extension. This is called a Python module. To edit a Python module, a text editor is necessary. The preferred choice is an editor that can highlight Python syntax, which means that it colours differently different Python commands to make them easier to notice. Blender already comes with a text editor that performs very well for Python coding. So change the window type to "Text Editor" by left-clicking the little icon in the bottom-left corner and choose “Text Editor”. The following should be seen:

File:Screen shot 2011-10-28 at 1.16.57 PM.png

To the right of "+ New", click the second button in the row to label the line numbers. Now click "+ Next" to create a new text file. To save a script created within the text editor, click "Text", and then "Save As" to save your file. remember to put a .py extension after the name of the file when saving.

That's it! The first Python module was created, and the text editor is ready for inserting multiple commands. However, before doing any coding, there is one more thing that needs to be done, and that is enabling the OS (Operating System) console for use. This needs to be done because the Python Console works separately from the text editor. Therefore, each time a module, script or program is executed, (they mean the same thing ) it will report incomplete errors, which aren't suitable. If the OS console or terminal is enabled, the resulting errors (if any) will be in full detail. How to enable it depends on the OS that's being used. For Windows users, press Ctrl+R, type cmd.exe, and press enter. For mac OS and Linux users, Blender will have to be run from the terminal. Here is a nifty little article on doing so.

Contents | Previous | Next