Links.py - Python basics.

Using Python version 2.3 
'import site' failed; use -v for traceback 
No installed Python found. 
Only built-in modules are available. Some scripts may not run. 
Continuing happily.

The above is what you get in the console when you don’t have Python
installed and the first time you saw it you thought Blender was broken.
So I hope this will help you get your head around it all.

Python is a programming language; text, written according to a given
set of rules with goofy spacing and punctuation that computers can
understand. To use it your computer must have the basics of Python
installed. Think of it as the alphabet and grammar101.

Fortunately, Blender comes with the alphabet and at least the first
five chapters of the grammar book. It can’t do everything that a full
install of the Python language can do, but it can do most of what a
graphics program like blender has for it to do like playing around with
meshes, importing and exporting them in a format that other CG
programs can understand, or just comming to the rescue with
whizz-bang little tricks that make modelling, texturing and animating a
lot easier.

How does it do this? Inside Blender, compiled and included in the 5
or 6Mb that you downloaded, is a bunch of Python related code (known as the API)
that is hooked up to Blender’s C and C++ framework on the one end
and on the other end has three input consoles, where you feed it little
chunks of Python. If you feed it the right stuff, the right way, thru the
right console it can make Blender do tricks for you.

(the 3 consoles are the text window, the script window and script links but we’ll look at them later)

(I found this definition of API on the net @:
www.polyphonic-ringtones-ring-tones.co.uk/glossary.html)

Historically, “application programming interface”. Practically, an API is any interface that enables one program to use facilities provided
by another, whether by calling that program, or by being called by it.

http://img.photobucket.com/albums/v625/fligh/PythonPeek.jpg

As you can see above the API is like an interpreter. It takes the raw
Python code that you feed it, called Scripts, and parses it then feeds
it to the C programming part of Blender in a way that Blender
understands it. Or it might call for Blender to feed it information, (eg:
info about a mesh you want to export) read that info and reply by
spitting out a file that Maya or Ogre or some other 3D Graphics or
Game program can use. Or it may parse data from Blender, modify it
and feed it back to Blender again. Many possibilities.

So, the folks at www.python.org developed this language, and the
folks at Blender decided that it was good (there are reasons but they
will only sidetrack us) so they built, actually wrote the API so that
Blender could talk to Python and vice-versa. I say built because even
though they wrote it there is a structure, someone decides this is how
it all fits together, and that’s dependant on the structure of the code in
both Blender and Python.

You may stop reading for a moment while we bless their souls.

Not only that, they also built it so that, as shown on the right of that
pic, the whole python codebase and all its modules and libraries can
be called from Blender. Well, not all but that’s the point of this
paragraph. As they found and fixed bugs in Python, as they added
more functionallity and extra modules, and as Blender evolved, so
the structure of the API had to be maintained, or else functionality
would become static and even redundant. Python *(which in essence
is an API itself) had this same problem and the “maintainence” was
much easier if they just rebuilt everything and released it as a new
version.

  • (From the same definition:-

At a higher level still, an API is a set of functionality delivered by a programming system, and as such the mix of APIs in a particular system tells you what that system can do

This meant that each time Python released a new version, Blender, to
keep up with the progress in functionallity, would need to recompile
its API to be compatible with Python versions. This, unfortunately,
means that the two will never be fully synchronized as Python has to
release a version before Blender can recompile it’s API and it’s
usually a good idea to do that in a future release and after lots of
testing. The result is that there are only ever rare windows in time
when it’s possible for Blender to use the latest version of Python.

Now, to get back to the API itself, to be able to grab ahold of a block
of (eg:mesh) data and modify it in Blender, the API needs to have
hooks, a set of protocols, that can hook onto the C code where and
how they’re supposed to. As Blender evolves somebody has to
reevaluate, modify, add to, add on, update and debug each of these,
while keeping an open mind about compatibility across platforms and
Blender versions. Unfortunately Python versions are not forward or
backward compatible with Blender versions. And you can see that,
although the geeks can compile Blender with the latest version of
Python, it doesn’t mean that the API will support everything available
in the new version. And, besides, some functionality may be lost as
Python changes it’s code.

Okay, back to the three places to get behind the wheel. First is the Text
Editor. You open it up, click on the menu in the header and hit “Open
New” then navigate to your Python Script and MMB
(middlemousebutton) it to load. Or you could just type in your script

import Blender 
from Blender import Curve, Object, Scene, Text3d, Window 

#To Create a Text3d object 
txt = Text3d.New("Text1") 
txt.setText("ABC123") 
cur = Scene.getCurrent() 
obj = Object.New("Text") 
obj.link(txt) 
cur.link(obj) 
obj.makeDisplayList() 
Window.RedrawAll() 

The above thanks to maniegrob and it should add a text object in the
3D window “ABC123”

Now you put your mousecursor over the Python code and hit Alt-P
(hotkey for ‘Pray’), and either the script will do what it’s supposed to
do or you’ll get a message at the mousecursor saying “ERROR:
Python script error, check console”, which means minimize Blender
and look in the DOS-like console for the full error message. In Mac
and Linux you might have to go to Applications and open a console
from there.

Second, the Scripts Window is much like the text window except it’s
specifically designed to load and run scripts. You go to the “Scripts”
menu in the header and navigate the submenus till you find the script
you need. It will open automatically when clicked and there’s so much
that goes on behind the scenes that is a mystery to me that all I’m
going to say about it for the moment is “sliced bread”.

To have scripts show up in your Scripts menu they need the following
(!BPY) code:

#!BPY

"""
Name: 'BreadSlicer0.5Beta'
Blender: 237
Group: 'Object'
Tooltip: 'Slices, packs, lables and ships sliced bread'
"""

Somewhere there’s a folder called “Scripts” and where it is depends
on your OS. But you can find it by doing a search for one of the
scripts you see in the menu. So, if someone publishes a script you find useful and it has the !BPY code you can pop it in that folder and hit the
Update Menus” button in the Scripts menu and it should go to the
Object’ group using the example above. If your script doesn’t have the code you can write it; just make sure that your Blender Version is valid and that the menu Group exists.

The third console is the “Script Links” found here

http://img.photobucket.com/albums/v625/fligh/ScrptLnk.jpg

This is mainly used for the GameEngine or for animations where the
script gets called repeatedly to update the scene frame by frame as it moves along.

So now, according to the error message at the top all scripts will (and
can) use the internal modules included in the API. And it
says “Some scripts may not run”. But there are some natty scripts out
there that do need a full Python install. You’ll know which version from
the first line in the Console message:

Using Python version 2.3
(this may change of course)

Head on down to www.python.org/download/ and install it either on
C:\ or C:\Program Files or the UNIX equivalent. If you still get an error
message with:

No installed Python found.

then go to this link:
https://blenderartists.org/forum/viewtopic.php?t=7723

and see how to set your PythonPath. I have never had any problems
with my pythonpath so I can’t help you much there but there are
people who can, just ask.

Now, once you have successfully installed and set your path all
scripts written for Blender should run. If you do get error messages
they will be very different from the benign message that first troubled
your soul. For example:

Traceback (most recent call last): 
File "lanc_povanim228_07b.p", line 1792, in bevent3 
File "lanc_povanim228_07b.p", line 995, in BasdepageBevent 
UnboundLocalError: local variable 'sys' referenced before 
assignment 

Now you can see how plain and clear that first message was! When
you get a message like the above and you want to ask about it on the
forum, those who can help you decipher it need to see it exactly as it
is. They also need to know what OS, which script and which versions
of Blender and Python you’re using.

The question “How do I learn Python” comes up often so here is a list of the essential links.

A list of introductory information and documentation about Python itself.

http://www.python.org/doc/faq/
FAQ’s about most of what’s above.

Tutorials by Jean Michel Soler (jms) that deal with Python in the Blender environment.

I’ll fix the following 3 when blender.org and blender3d.org are fully integrated

http://www.blender.org/modules/documentation/237PythonDoc/index.html
The 2.36 Python Appi documentation for Blender.

http://www.blender.org/modules/documentation/pydoc_gameengine/PyDoc-Gameengine-2.34/index.html
GameEngine Python documentation for 2.34

http://www.blender.org/modules/documentation/pydoc_gameengine/PyDoc-T2-Gameengine-2.34/index.html
Tuhoppu GameEngine docs for 2.34

For further reading, examples and exercises outside of Blender:

Everybody seems to mention it.

http://www.python.g2swaroop.net/byte/index.html
http://www.g2swaroop.net/files/byte/byte_of_python_pdf_115.pdf
A-Byte-of-Python in HTML or PDF that has had good feedback.

http://diveintopython.org/
Available in HTML, PDF, Docbook, Word etc…
Tutorial for experienced programmers.

The Vaults of Parnassus: loads of stuff made with Python.
http://www.vex.net/parnassus/

%<

last update july 19, 05

heh nice list…maybe add comments to the site about each and which you prefer more :slight_smile:

Don’t forget the cookbook: http://aspn.activestate.com/ASPN/Python/Cookbook/

Blender + Python

Updated 11-2-04

%<

some one wants a sticky again…

nice work, good to have someone doing what no one else wants to do.

thanks

Posted this elsewhere :expressionless:
I also like these columns by Mertz: http://gnosis.cx/publish/tech_index_cp.html

Updated again. I’d appreciate some crits because I’m not sure that all my termanology is correct.

%<