Bàn Giao Tiếp Python (Python Console)#

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.

../_images/editors_python-console_default.png

Bàn Giao Tiếp Python.#

Giao Diện (Interface)#

Trình Đơn Tiêu Đề (Header Menus)#

Trình Đơn Góc Nhìn (View Menu)#

Phóng To / Thu Nhỏ (Zoom In / Zoom Out)

Increases/decreases the font size.

Chuyển đến Từ Đứng Trước (Move to Previous Word) Ctrl-Trái (Left)

Di chuyển con trỏ tới điểm đầu từ trước đây. Nếu con trỏ nằm ở giữa một từ thì con trỏ sẽ được chuyển tới điểm đầu của từ đó.

Chuyển đến Từ Tiếp Theo (Move to Next Word) Ctrl-Phải (Right)

Di chuyển con trỏ tới điểm cuối của từ tiếp theo. Nếu con trỏ nằm ở giữa một từ thì con trỏ sẽ được chuyển tới điểm cuối của từ đó.

Chuyển về Đầu Dòng (Move to Line Begin) Home

Di chuyển con trỏ đến đầu dòng hiện tại.

Shift-Home: Selects all text between the cursor and the start of the current line.

Chuyển đến Cuối Dòng End

Di chuyển con trỏ đến cuối dòng hiện tại.

Shift-End: Selects all text between the cursor and the end of the current line.

Cửa Sổ Bàn Giao Tiếp Trình Đơn (Console Menu)#

Xóa Toàn Bộ (Clear All)

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

Xóa Dòng Shift-Return.

Xóa mọi thứ khỏi dòng nhắc lệnh.

Xóa Từ Đứng Trước (Delete Previous Word) Ctrl-Phím Lùi (BackSpace)

Xóa mọi thứ giữa con trỏ và phần đầu của từ trước đó (phân tách bằng dấu chấm). Nếu con trỏ ở giữa một từ thì xóa mọi thứ về đầu từ hiện tại.

Xóa Từ Tiếp Theo (Delete Next Word) Ctrl-Xóa (Delete)

Xóa mọi thứ giữa con trỏ và cuối từ tiếp theo. Nếu con trỏ ở giữa một từ thì xóa mọi thứ đến cuối từ hiện tại.

Sao Chép thành Tập Lệnh Shift-Ctrl-C (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.

Cắt (Cut) Ctrl-X

Copies the selected text into the clipboard and deletes it.

Sao Chép (Copy) Ctrl-C

Copies the selected text into the clipboard.

Dán (Paste) Ctrl-V

Pastes into the command line.

Thụt Dòng (Indent) Tab

Chèn thêm một ký tự thẻ tại vị trí con trỏ.

Bỏ Thụt Dòng (Unindent) Shift-Tab

Bỏ thụt dòng vùng lựa chọn.

Quay Ngược Lại trong dòng Lịch Sử (Backward in History) Lên (Up)

Changes the current command to the previous one from the command history.

Tiến về Trước trong dòng Lịch Sử (Forward in History) Xuống (Down)

Changes the current command to the next one from the command history.

Tự động Hoàn Chỉnh (Autocomplete) Tab

See Auto Completion.

Khung Nhìn Chính (Main View)#

Những Bố Trí Phím (Key Bindings)

  • LMB -- Moves the cursor along the input line.

  • Left / Right -- Moves the cursor by one character.

  • Ctrl-Left / Ctrl-Right -- Moves the cursor by one word.

  • Shift-Left / Shift-Right -- Selects characters to the left/right.

  • Shift-Ctrl-Left / Shift-Ctrl-Right -- Selects words to the left/right.

  • Ctrl-A Selects all text and text history.

  • Phím Lùi (BackSpace)/Xóa (Delete) - Xóa ký tự.

  • Ctrl-Phím Lùi (BackSpace)/Ctrl-Xóa (Delete) - Xóa từ.

  • Return - Thực hiện lệnh.

  • Shift-Return - Thêm vào lịch sử lệnh mà không cần thực thi.

Cách Sử Dụng (Usage)#

Bí Danh (Aliases)#

Một số biến số và mô-đun có sẵn để sử dụng thuận tiện:

  • C: Truy cập nhanh vào bpy.context.

  • D: Truy cập nhanh vào bpy.data.

  • bpy: Mô-đun ở cấp bậc cao nhất trong API Python của Blender.

Làm Quen với Môi Trường Bàn Giao Tiếp (First Look at the Console Environment)#

To see the list of global functions and variables, type dir() and press Enter to execute it.

../_images/editors_python-console_dir.png

Tự Động Hoàn Chỉnh (Auto Completion)#

The Console can preview the available members of a module or variable. As an example, type bpy. and press Tab:

../_images/editors_python-console_completion.png

The submodules are listed in green. Attributes and methods will be listed in the same way, with methods being indicated by a trailing (.

Một Số Ví Dụ (Examples)#

bpy.context#

This module gives you access to the current scene, the currently selected objects, the current object mode, and so on.

Ghi chú

Để các lệnh bên dưới hiển thị đầu ra chỉnh tru thì xin hãy đảm bảo rằng bạn đã chọn (các) đối tượng trong Cổng Nhìn 3D.

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

Get the current 3D Viewport mode (Object, Edit, Sculpt, etc.):

bpy.context.mode

Get the active object:

bpy.context.object
bpy.context.active_object

Change the active object's X coordinate to 1:

bpy.context.object.location.x = 1

Move the active object by 0.5 along the X axis:

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)

Get the selected objects:

bpy.context.selected_objects

Get the selected objects excluding the active one:

[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.

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

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.