From BlenderWiki
[edit] Hello World
Traditionally, when learning a new programming language, the first program you write is called Hello World. It simply prints out "Hello, World!". Simple, but it demonstrates you have the ability to create and run programs. Python is meant to be an easy-to-use language, so it is not surprising that our Hello World is simple. Here is the whole thing:
print 'Hello, World!'
The print command writes a text string - the characters in quotes - to the console or terminal window where we started Blender. Quite useful for sending messages and keeping track of what is happening!
- start Blender from a console or terminal window
- change one window to a Text Editor
- choose File-New to get a blank text page
- enter the text below exactly as written:
print 'Hello, World!'
- run the script by one of the following methods:
- press Alt Por
- choose File-Run Python Script from the drop-down menu or
- select Execute Script using the Right Mouse menu in the Text window
- our message appears in the console window
Remember that Syntax Error mentioned above?
- just for fun, insert a space before the word 'print' and run the script
- the message 'Python script error, check console' means go look at the terminal or console window where you started Blender. Useful information is there!
[edit] Comments
Comments are simply notes in the code. They do NOT get executed. They are useful for describing what the code does. This is not only helpful to others looking at your script, but also reminds you what you were doing when you look at your code a week later. Good comments describe why something is done. The code itself describes what is being done.
Comments come in a couple different styles:
# this is a comment. comments start with the '#' character # the print command prints a string of # characters to the console print 'Hello, World!' # another silly comment # the string of characters is enclosed in quotes # either single quote marks 'like this' # or double quote marks "like this" ''' This is a block comment. The comment starts with three quotes - either single or double - on a line Use three matching quotes to close the comment '''
This may seem trivial, but we now know how to
- create a script
- run a script
- print useful messages with the print command
[edit] Import and dir()
Since that was so easy, let's try something else. Enter the code below and run it.
import sys
print dir(sys)
- Discussion
Our new commands here are the 'import' command and dir(). Python is divided up into modules that contain useful commands and data for specific purposes like talking to the operating system or doing math. Because Python tries not to eat up lots of resources, you must explicitly name modules you want to use using the import command.
The dir() command is used to list the methods and data in a module or that a particular type supports. Unlike the command line interpreter, the Blender interpreter does not print the results of the last command executed, therefore we need to use 'print dir(sys)' to see the contents of the sys module.
Modules are organized as hierarchies. To access a module item, prefix it with the module name followed by a dot like this: print sys.path. Try adding that to your simple script and run it.
All of the Blender BPy API is contained in a module called 'Blender'
You will become used to seeing
import Blender
You can read more about modules and the dir() command here: http://www.python.org/doc/current/tut/node8.html







![[]](/skins/blender/open.png)
