利用者:Kilon/Python book of magic/02.Python Console

提供: wiki
< 利用者:Kilon‎ | Python book of magic
2012年9月2日 (日) 00:30時点におけるwiki>Newfyworldによる版 (2 Python Console)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Contents | Previous | Next


2 Python Console

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 cosole image1.png

The text can be extensive. The first line is 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 is able to 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 to simply.

2.1 Basic Types

2.1.1 Integer Type

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:

Python console image2.png

Surprisingly, the first program was just made. The line of code consists of 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:

Python console image3.png

Example of division:

Python console image4.png

Example of subtraction:

PythonSubtractionExample.png


2.1.2 String Type

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

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 toe execute the command:

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.

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

The output result 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”, 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.

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 it expected. Another way of saying this is that the command provided correct Python Syntax. Type the following into the Python Console, and then press enter to execute the command:

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

The Python Console really fun and useful, but scripts and addons 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 thing that's necessary is a regular text file with *.py extension. This is called a Python module. In order 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 colors differently different Python commands to make them easier to notice. Blender already comes with a text editor that it works very good 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:

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.

Thats 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 text editor. Therefore, each time a module / script / program is executed, (they mean the same thing ) it will report incomplete errors, which aren't not suitable. If the OS console / 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 ran from the terminal. Here is a nifty little article on doing so.


Contents | Previous | Next