Doc:2.6/Tutorials/Extensions/Python/BSoD/Hello World

提供: wiki
移動先: 案内検索

Here is an example of simple "Hello World" script for blender 2.6x.

First a little introduction to some important features that could be confusing at start. When using keyboard shortcuts it matters a lot where your mouse cursor is positioned, more precisely, above what Window\Editor Type you place it.

  • With mouse hovering anywhere inside "3d View" press CtrlN and confirm to open up a fresh new file (or click File » New).
  • Let's change the Screen lay-out to scripting.
  • By hovering over the Text Editor Window we create a new text data block with CtrlN (or by clicking the New button).

New-text-data-block-button.jpg

As we write anything inside the Text Editor, again, it is important that the cursor is somewhere inside it, or else you will be either typing in a wrong editor or not typing at all but instead calling some random keyboard shortcuts (test it by typing and moving cursor from Text Editor to anywhere else. I won't mention this anymore from now on).

Finally, let's get to the code.

  • In the Text Editor retype:
 print "Hello World"
  • press AltP to run our script (or click the Run Script button).

Run-script-button.png

Congratulations! Nothing happened. Well maybe not what we can see yet. In the Info Editor you will see this message:

Info-run-script.png

and to see the actual string "Hello World" printed out we have to open up the Blender System Console

  • by clicking Window » Toggle System Console

Toggle-system-console.png

    • Don't close System Console by clicking on X because the whole Blender will close!
    • This is where you will see your error info as well.

You would get the same result in the System Console if you typed print("Hello World") directly into Python Console, but then we wouldn't have a script and would have to paste or type it inside Python Console every time.

Let's look up one more thing, how to save our script. If you go to Text » Save you will see that you cannot save

Grayed-out-save-text.png

and that the save shortcut AltS is not working. So what is happening here? New text data blocks we create are all text files saved internally into current blender file, so if we were to open a new file they would disappear. That's why we have to save it outside blender file first

  • by clicking Text » Save As to make it independent
    • Don't forget to put ".py" at the end so that blender knows that it is a python script when you want to open it. However if you don't put the extension you can still open the file by disabling filtering of files:

Enable-filtering-of-files.png

Notice that the Text » Save is no longer grayed out.