Doc:2.6/Manual/Extensions/Python/Text editor

提供: wiki
< Doc:2.6‎ | Manual‎ | Extensions‎ | Python
2018年6月29日 (金) 03:45時点におけるYamyam (トーク | 投稿記録)による版 (1版 をインポートしました)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

The Text Editor

Blender has a Text Editor among its windows types, accessible via the Text Editor button (Manual-Part-XV-textButton.png) of the Window type menu, or via ⇧ ShiftF11.

The newly opened Text window is grey and empty, with a very simple toolbar (Text Toolbar).

Text Toolbar.

From left to right there are the standard Window type selection button and the window menus. Then there is the Text ID Block browse button followed by the New button for creating new Text files. Once you click it, you will find that the Toolbar has changed.. for good!

Text Toolbar with a file open

Now you find a textbox to change name of your text file, followed by + button to create new files. To remove the text block, click the X button.

The following three buttons toggle display of line numbers, word-wrap text and syntax highlighting respectively.

Typing on the keyboard produces text in the text buffer. As usual, pressing dragging and releasing LMB Template-LMB.png selects text.

The following keyboard commands apply:

  • CtrlC - Copies the marked text into the text clipboard.
  • CtrlX - Cuts out the marked text into the text clipboard.
  • CtrlV - Pastes the text from the clipboard at the cursor location in the Text window.
  • ⇧ ShiftCtrlAltS - Saves unsaved text as a text file, a File Browser window appears.
  • AltS - Saves an already open file.
  • AltO - Loads a text, a File Browser window appears.
  • AltP - Executes the text as a Python script.
  • CtrlZ - Undo.
  • Ctrl⇧ ShiftZ - Redo.
  • AltR - Reopen (reloads) the current buffer (all non-saved modifications are lost).
  • AltM - Converts the content of the text window into 3D text (max 100 chars).

To delete a text buffer just press the X button next to the buffer’s name, just as you do for materials, etc.

The most notable keystroke is AltP which makes the content of the buffer being parsed by the internal Python interpreter built into Blender. The next page will present an example of Python scripting. Before going on it is worth noticing that Blender comes with a fully functional Python interpreter built in, and with a lots of Blender-specific modules, as described in the API references.

The Text Editor has now also some dedicated Python scripts, which add some useful writing tools, like a class/function/variable browser, completion… You can access them through the Text → Text Plugins menu entry.

Other usages for the Text window

The text window is handy also when you want to share your .blend files with the community or with your friends. A Text window can be used to write in a README text explaining the contents of your blender file. Much more handy than having it on a separate application. Be sure to keep it visible when saving! If you are sharing the file with the community and you want to share it under some license you can write the license in a text window.

Demonstration

Exercise

Copy the text below in the Text Editor.

import bpy 
from math import radians, cos, sin
 
# An object can exist in 20 layers, 
# so the following code determines on which layers you want it to be

# Get the cursor's location
cursor = bpy.context.scene.cursor_location
 
# Radius of the circle
radius = 5
 
# Space the cubes around the circle. Default is 36 degrees apart
# Get a list of angles converted to radians
 
anglesInRadians = [radians(degree) for degree in range(0, 360, 36)]
 
# Loop through the angles, determine x,y using polar coordinates 
# and create object
for theta in anglesInRadians:
    x = cursor.x + radius * cos(theta)
    y = cursor.y + radius * sin(theta)
    z = cursor.z
    bpy.ops.mesh.primitive_cube_add(location=(x, y, z))

Execute the script with AltP.

You can see the result of running the above script in this video.