Python 控制台

Python的控制台是一个快速的方法来执行命令,以访问整个Python的API,命令历史记录和自动完成。

其探索的可能性,然后可以粘贴到大脚本的好办法。

简介

访问内置的Python控制台

通过按 Shift-F4 在任何Blender编辑器类型(3D视图,时间线等),你可以把它改成一个控制台编辑器。

../_images/editors_python-console_default.png

命令提示符是典型的 Python 3.x,解释器已加载,并准备接受提示符 >>> 后的命令。

先看看控制台环境

若要检查什么加载到翻译环境,请在提示符下键入``dir()`` 并执行。

../_images/editors_python-console_dir.png

自动完成工作

现在,键入 bpy. ,然后按 Ctrl-Spacebar ,你会看到控制台自动完成功能在行动中的。

../_images/editors_python-console_completion.png

你会注意到出现在 bpy 子模块的列表。这些模块封装所有我们能做搅拌机 Python API 和是非常强大的工具。

让我们列出的 bpy.app 模块的所有内容。

请注意上面的提示在您启用自动完成的绿色输出。你看到了什么是自动完成上市的结果。在上述清单都是模块属性名称,但是如果你看到任何名称结束与 (,那就是一个函数。

我们将利用这很多来帮助我们更快地学习 API。既然你得到了这个窍门,让我们着手调查一些 bpy 模块。

摆弄模块前

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

  • 所有对象的上下文中都存在,可以有各种模式下的他们动手术。
  • 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.
  • 有运营商/函数可以创建和修改这些对象。

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

示例

bpy.context

Note

For the commands below to show the proper output, make sure you have selected object(s) in the 3D View.

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

Try It Out!

bpy.context.mode
Will print the current 3D View mode (Object, Edit, Sculpt, etc.).
bpy.context.object 或 bpy.context.active_object
Will give access to the active object in the 3D View.

Change X location to a value of 1:

bpy.context.object.location.x = 1

Move object from previous X location by 0.5 unit:

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

Changes X, Y, Z location:

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

Change only the X, Y components:

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

Data type of objects 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
就可以访问到所有选定对象的列表。

Type this and then press Ctrl-Spacebar:

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.

Try It Out!

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

锻炼

After Enter twice it prints the names of all objects belonging to the Blender scene with name "Scene":

for obj in bpy.data.scenes['Scene'].objects: print(obj.name)

Unlink the active object from the Blender scene named 'Scene':

bpy.data.scenes['Scene'].objects.unlink(bpy.context.active_object)
bpy.data.materials['Material'].shadows

bpy.data.materials['Material'].shadows = False

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.

Lets create a set of five Cubes in the 3D View. First, delete the existing Cube object by selecting it and pressing X.

Try It Out!

The following commands are used to specify that the objects are created in layer 1. So first we define an array variable for later reference:

mylayers = [False] * 20
mylayers[0] = True

We create a reference to the operator that is used for creating a cube mesh primitive:

add_cube = bpy.ops.mesh.primitive_cube_add

Now in a for loop, we create the five objects like this (in the screenshot above, another method is used): Press Enter twice after entering the command at the shell prompt:

for index in range(5):
    add_cube(location=(index * 3, 0, 0), layers=mylayers)
../_images/editors_python-console_bpy-ops.png

使用方法

Aliases

Some variables and modules are available for convenience:

  • C: Quick access to bpy.context.
  • D: Quick access to bpy.data.
  • bpy: Top level Blender Python API module.

Key Bindings

  • Up / Down -- Cycle command history.
  • Left / Right -- Cursor motion.
  • Ctrl-Left / Ctrl-Right -- Cursor motion, by word.
  • Backspace / Delete -- Erase characters.
  • Tab -- Indent.
  • Shift-Tab -- Unindent.
  • Ctrl-Backspace / Ctrl-Delete -- Erase words.
  • Ctrl-Spacebar -- Auto complete.
  • Enter -- Execute command.
  • Shift-Return -- Add to command history without executing.
  • Ctrl-C -- Copy the selection.
  • Ctrl-V -- Paste into the command line.