Python 控制台





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


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


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



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


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

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

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

我们将利用这很多来帮助我们更快地学习 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.




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


Try It Out!

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:


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


Type this and then press Ctrl-Spacebar:


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


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] 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!



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

for obj in['Scene'].objects: print(

Unlink the active object from the Blender scene named 'Scene':['Scene'].objects.unlink(bpy.context.active_object)['Material'].shadows['Material'].shadows = False


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)



Some variables and modules are available for convenience:

  • C: Quick access to bpy.context.
  • D: Quick access to
  • 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.