Python 控制台¶
The Python Console offers a quick way to test code snippets and explore Blender's API.
It executes whatever you type on its >>>
prompt and has command history and auto-complete.
界面¶
主视图¶
键位绑定
LMB -- 沿着输入行移动光标。
Left / Right -- 光标移动一个字符距离。
Ctrl-Left / Ctrl-Right -- 光标移动一个单词距离。
Shift-Left / Shift-Right -- 选择左/右侧的字符。
Shift-Ctrl-Left / Shift-Ctrl-Right -- 选择左/右侧的单词。
Ctrl-A Selects all text and text history.
回退键 / Delete -- 擦除字符。
Ctrl-回退键 / Ctrl-Delete -- 擦除单词。
Return -- 执行命令。
Shift-Return -- 添加到命令历史记录而不执行。
用法¶
别名¶
一些变量和模块可以方便使用:
C
: 快速访问bpy.context
。D
: 快速访问bpy.data
。bpy
: 顶层级Blender Python API模块。
初探控制台环境¶
To see the list of global functions and variables,
type dir()
and press Return to execute it.
自动补全¶
The Console can preview the available members of a module or variable.
As an example, type bpy.
and press Tab:
The submodules are listed in green. Attributes and methods will be listed
in the same way, with methods being indicated by a trailing (
.
示例¶
bpy.context¶
This module gives you access to the current scene, the currently selected objects, the current object mode, and so on.
Note
要使以下命令显示正确的输出,请确保在3D视图中有选中的物体。
获取当前3D视图的模式 (物体、 编辑、 雕刻等):
bpy.context.mode
获取活动物体:
bpy.context.object
bpy.context.active_object
将活动物体 X 坐标更改为 1:
bpy.context.object.location.x = 1
将活动物体沿 X 轴移动 0.5:
bpy.context.object.location.x += 0.5
Change all three location coordinates in one go:
bpy.context.object.location = (1, 2, 3)
Change only the X and Y coordinates:
bpy.context.object.location.xy = (1, 2)
获取选中的物体:
bpy.context.selected_objects
获取除了活动项以外的选中物体:
[obj for obj in bpy.context.selected_objects if obj != bpy.context.object]
bpy.data¶
Gives you access to all the data in the blend-file, regardless of whether it's currently active or selected.
bpy.ops¶
"Operators" are actions that are normally triggered from a button or menu item but can also be called programmatically. See the bpy.ops API documentation for a list of all operators.