Graphics Reference
In-Depth Information
By using Blender's built-in interpreter and application programming interface (API) to run scripts, you can
access Blender assets and functionality. This can be useful for a wide variety of tasks, from automating simple
butrepetitive steps that wouldbetootedious todobyhandthroughthe standard Blender interface, tosetting up
your own macros and tools, to creating sophisticated mathematical visualizations automatically with the stroke
of a key. Furthermore, Blender's Python functionality is deeply integrated, making it possible to access Python
in other ways aside from the main script interpreter. In addition to simply running scripts in Blender, you can
access Python through the nodes system, use Python for animation drivers, use Python to create constraints,
and incorporate full Python functionality in the game engine with Python logic bricks. In the next few chapters,
you'll learn how to do all of these things and how this knowledge can open up a whole new world of Blender
functionality to you.
Why Python?
IntheyearssincePythonwasinitiallyreleasedin1991,ithasbecomewidelyusedinahostofdifferentapplica-
tion domains. There are several reasons for this. First, Python is relatively easy to learn. For people accustomed
tootherprogramminglanguages,therearesyntacticfeaturespeculiartoPythonthatmustbelearned,butbyand
large,thefunctionalitypeopleexpectfromotherlanguagesisstraightforwardlyimplementedinPython.Fornew
programmers,Pythonimplementshigh-levelabstractionsthatcansimplifymanytasksthatwouldrequiremore-
involved solutions in other languages, making Python comparatively easy to start programming with. Indeed,
one of the biggest habits for experienced programmers to break when they begin working with Python is the
tendency to want to code things in an unnecessarily complex way. Iterating through lists, for example, is done
more simply in Python than in other commonly used languages.
Python'seaseofuseextendsnotjusttolearningthelanguagebutalsotodevelopment.Pythoncodeisknown
for its readability. One of Python's most notable syntactic idiosyncrasies is that it forces you to write readable
code. In Python, the scope of control structures such as for loops and conditionals is determined by the left-
side indentation of the line. In other commonly used languages, this is usually handled by some kind of brack-
eting, andwhite space, tabs, andspaces are farless important. Python'sapproach helps toeliminate many kinds
of subtle bracketing errors that plague other languages. In Python, if the pattern of indentation in a script is not
well formed, the interpreter will throw up a syntax error and the script will not be executed. If the indentation is
well formed, on the other hand, then the scope of control structures is clearly visible at a glance. In fact, many
conscientious programmers use indentations as a way of visually identifying control scopes even in languages
that do not require them to. By requiring indentations, Python is able to ensure a more consistent coding style,
whichresultsinmore-readableandcompactcode.BlenderandPythonassumethatatabbedindentationisequi-
valent to four spaces.
Inadditiontothehighlevelofabstraction tosimplifytasksandtheadvantages inreadability enforcedbythe
syntax, Python's status as a full-fledged, object-oriented programming (OOP) language takes advantage of the
OOP paradigm and the benefits that come with it. OOP enables programmers to define data structures that will
beusedintheirprograms,called classes. Individualinstances ofaclassarecalled objects ofthatclass.Muchof
OOP involves sending and receiving queries, called methods , between objects. Each class has specific methods
defined for it. As a simple example, imagine a class called Person and another class called Car . A specif-
ic person is an object of class Person . It is reasonable to ask what color a person's eyes are, so the question
“Whatcolorarethisperson'seyes?”isanalogoustoamethoddefinedfor Person .Itdoesn'tmakesensetoask
that question about a car, so that method would not be something that is defined for the class Car . The advant-
age of OOP is that after you understand the basics of the programming language, interacting with libraries that
have been written by others requires only that you know what classes you are dealing with and what methods
Search WWH ::




Custom Search