Python Console(Pythonコンソール)

The Python Console is a quick way to execute commands, with access to the entire Python API, command history and auto-complete. The command prompt is typical for Python 3.x, the interpreter is loaded and is ready to accept commands at the prompt >>>.

The Python Console is a good way to explore the possibilities of Blender built-in Python. The Python Console can be used to test small bits of Python code which can then be pasted into larger scripts.

../_images/editors_python-console_default.png

Pythonコンソール。

Interface(インターフェイス)

ヘッダーメニュー

View(ビュー) メニュー

Zoom In(ズームイン) / Zoom Out(ズームアウト)

Increases/Decreases the font size of the console text.

Move to Previous Word(前の単語に移動) Ctrl-Left

前の単語の先頭にカーソルを移動します。カーソルが単語の途中にある場合、カーソルは現在の単語の先頭に移動します。

Move to Next Word(次の単語に移動) Ctrl-Right

次の単語の末尾にカーソルを移動します。カーソルが単語の途中にある場合、カーソルは現在の単語の末尾に移動します。

Move to Line Begin(行頭に移動) Home

現在の行の先頭にカーソルを移動します。

Move to Line End(行末に移動) End

現在の行の末尾にカーソルを移動します。

Console(コンソール) メニュー

Clear All(すべてクリア)

Refreshes the console giving the view a fresh start. Note that command history is not cleared.

Clear Line(行をクリア) Shift-Return

プロンプト行からすべてを削除します。

Delete Previous Word(前の単語を削除) Ctrl-Backspace

カーソルから前の(ピリオドで区切られた)単語の先頭までのすべてを削除します。カーソルが単語の途中にある場合は、現在の単語の先頭までのすべてを削除します。

Delete Next Word(次の単語を削除) Ctrl-Delete

カーソルから次の単語の終わりまでのすべてを削除します。カーソルが単語の途中にある場合は、現在の単語の最後までのすべてを削除します。

Copy as Script(スクリプトとしてコピー) Shift-Ctrl-C

Copies the full history buffer to the clipboard, this can be pasted into a text file to be used as a Python script.

Copy(コピー) Ctrl-C

Copy the selection.

Paste(貼り付け) Ctrl-V

Paste into the command line.

Indent(インデント) Tab

カーソルの位置にタブ文字を挿入します。

Unindent(インデントを解除) Shift-Tab

選択範囲のインデントを解除します。

Backward in History(履歴の後方) Up

Changes the current command to previous command as they appear in the command history.

Forward in History(履歴の前方) Down

Changes the current command to next command as they appear in the command history.

Autocomplete(オートコンプリート) Tab

See Auto Completion for more information.

メインビュー

キーバインディング

  • Left / Right -- Cursor motion.

  • Ctrl-Left / Ctrl-Right -- Cursor motion, by word.

  • Backspace / Delete -- 文字を消去。

  • Ctrl-Backspace / Ctrl-Delete -- 単語を消去。

  • Return -- コマンドを実行。

  • Shift-Return -- 実行せずにコマンド履歴に追加。

使用方法

エイリアス

便宜上、いくつかの変数とモジュールを使用できます:

  • C: bpy.context へのクイックアクセス。

  • D: bpy.data へのクイックアクセス。

  • bpy: Blender Python APIモジュールのトップレベル。

コンソール環境の初見

To check what is loaded into the interpreter environment, type dir() at the prompt and execute it.

../_images/editors_python-console_dir.png

オートコンプリート

Now, type bpy. and then press Tab and you will see the Console auto-complete feature in action.

../_images/editors_python-console_completion.png

You will notice that a list of submodules inside of bpy appear. These modules encapsulate all that we can do with Blender Python API and are very powerful tools.

Lets list all the contents of bpy.app module.

Notice the green output above the prompt where you enabled auto-completion. What you see is the result of auto completion listing. In the above listing all are module attributed names, but if you see any name end with (, then that is a function.

We will make use of this a lot to help our learning the API faster. Now that you got a hang of this, lets proceed to investigate some of modules in bpy.

Before Tinkering with the Modules

If you look at the 3D Viewport in the default Blender scene, you will notice three objects: Cube, Light and Camera.

  • All objects exist in a context and there can be various modes under which they are operated upon.

  • At any instance, only one object is active and there can be more than one selected object.

  • All objects are data in the blend-file.

  • There are operators/functions that create and modify these objects.

For all the scenarios listed above (not all were listed, mind you...) the bpy module provides functionality to access and modify data.

bpy.context

注釈

以下のコマンドで適切な出力を表示するには、3Dビューポートでオブジェクトを選択していることを確認してください。

../_images/editors_python-console_bpy-context.png
bpy.context.mode

Will print the current 3D Viewport mode (Object, Edit, Sculpt, etc.).

bpy.context.object or bpy.context.active_object

Will give you access to the active object in the 3D Viewport.

Change the X location to a value of 1:

bpy.context.object.location.x = 1

Move the object from previous X location by 0.5 unit:

bpy.context.object.location.x += 0.5

Change the X, Y, Z location:

bpy.context.object.location = (1, 2, 3)

Change only the X, Y components:

bpy.context.object.location.xy = (1, 2)

The data type of object's location:

type(bpy.context.object.location)

Now that is a lot of data that you have access to:

dir(bpy.context.object.location)
bpy.context.selected_objects

Will give access to a list of all selected objects.

Type this and then press Tab:

bpy.context.selected_objects

To print out the name of first object in the list:

bpy.context.selected_objects[0]

The complex one... But this prints a list of objects not including the active object:

[obj for obj in bpy.context.selected_objects if obj != bpy.context.object]

bpy.data

bpy.data has functions and attributes that give you access to all the data in the blend-file.

You can access following data in the current blend-file: objects, meshes, materials, textures, scenes, screens, sounds, scripts, etc.

That is a lot of data.

../_images/editors_python-console_bpy-data.png

bpy.ops

The tool system is built around the concept of operators. Operators are typically executed from buttons or menus but can be called directly from Python too.

See the bpy.ops API documentation for a list of all operators.