[ LiB ] |
Graphics
What do 3DS Max, Lscript, Lightwave, Alice, Maya, Blender, Animation Master, TrueSpace, RenderMan, and Poser all have in common? Well, besides being graphic programs and 3D applications, they are all Python scripting interfaces. Python is ideal for the struggling artist; it's able to link up to industry gear and is perfect for creating quick custom tools or automating repetitive tasks.
Alice
Alice is a tool for developing three-dimensional graphics, built around the concept of "3D for everyone." Most 3D engines require the programmer to know extensive trigonometry, vector algebra, and other painful math. Alice is designed to provide non-programmers with access to 3D programming and interactive worlds. One of the things that makes Alice powerful is that it has a very straightforward, easy-to-learn GUI (shown in Figure 5.4) for placing, sizing, tweaking, and animating three-dimensional objects and spaces.
Figure 5.4. The Alice GUI
Alice is open source and made available by its current developers and copyright holders, the Stage Three Research Group at Carnegie Mellon University, and can be found online at http://www.alice.org.
The worlds and content created with Alice are freely distributable, as long as the stipulations in the license are followed. The Alice project initially began at the University of Virginia, and over the years has received support in the form of grants from DARPA, Intel, Microsoft, NSF, Pixar, Chevron, NASA, the Office of Naval Research, Advanced Network and Service Inc., ONR, and the Python community itself.
Currently Alice supports two-dimensional graphic imports (via drag and drop or through its built-in billboard) and .ase files, which are ASCII Scene Export files used for exporting 3D wire-frames on several 3D modelers (including 3D Studio Max). Alice is also capable of importing music and sounds by using MP3 files. The engine comes equipped with hundreds of models and sounds pre-built and packaged for the newbie.
Alice actually has draggable programming constructs (for example, if/else statements and loops) that can be used to set the behavior of the models. Underneath the GUI is a complete language that supports methods, arrays, lists, functions, recursion, and so on.
Alice has recently gone through a complete re-development, and work is ongoing to allow Alice to export and import more formats and run on more platforms. Originally, Alice was completely Pythonthe core, the code, the whole enchilada. With the recent major rewrite (which has been ongoing since 1999), much of the software has been rewritten in Java. However, the engine is still scriptable via Jython.
Jython is an implementation of Python. However, Jython is written completely in Java, and is integrated into Sun Microsoft's Java 2 J2EE platform. This means Jython has all the dynamic object-oriented features of the Python language, and also runs on any Java platform.
In order to implement Python/Jython scripting in Alice, you need to first enable it. You can turn on Jython scripting under the Preferences menu. Select Edit, Preferences, Enable Jython Scripting, as shown in Figure 5.5.
Figure 5.5. Enabling Jython scripting in Alice's GUI
Once scripting is enabled, every object within the Object Tree (the top left-hand window, which includes any instance of three-dimensional objects, including the world itself) is script editable with a right-click of the mouse, or through one-line scripts via a "go" executable line (see Figure 5.6). You can also access scripts when editing methods (Alice has a built-in method editor) with two draggable tiles called Script and Script-Defined Response.
Figure 5.6. Editing a penguin object script from the object tree
The Script tile allows you to type in code that will be run when that script method is run in the Alice engine. The Script-Defined Response is used to fire pre-composed Alice animations.
Objects in Alice can be called, using their names, through scripts, and their properties and variables are accessed just like member variables:
Penguin.isShowing = false
All of this is pretty powerfulnot only can you script objects via Python/Jython, but with Jython you also have access to the entire Java API. The scripts can also call built-in Alice animations and Alice's "RightNow" methods, like those outlined in Table 5.4.
Method | What it does |
---|---|
DoInOrder() | Runs a series of animations |
IfElseInOrder() | Runs animation list if the condition is met for if/else statements |
isShowing() | Sets subject to be visible or not visible |
ForEachInOrder() | Iterates through a list |
MoveAnimation() | Moves subject |
moveRightNow() | Moves subject immediately if given direction and amount |
PositionAnimation() | Sets subject position in world |
ResizeAnimation() | Resizes subject |
resizeRightNow() | Resizes subject immediately |
rotateRightNow | Rotates on given axis immediately |
setOrientationRightNow | Sets subject's orientation via 3D matrix immediately |
SoundAction() | Plays given sound at specified volume |
TurnAnimation() | Rotates subject |
turnRightNow() | Rotates subject immediately given amount |
WaitAction() | Waits for given duration |
WhileLoopInOrder() | Runs through animation list while condition is true |
These methods (and many otherscheck out the Alice2 documentation) can be called on models within Alice, but also on Alice's camera (the "watcher" point of view) and other objects like lights.
Let's say you wanted to define an animation function in Jython. You can define the animation just like you define any other function:
def MyAnimation(MyObject):
return MyAnimation
In this case, the function MyAnimation will take in MyObject as an argument and send back MyAnimation as the animation series you want the model to execute (assuming that the object will be an Alice model). Now let's set the animation to do something:
def MyAnimation(MyObject):
turn = TurnAnimation(MyObject, right, amount=1.0)
move1 = MoveAnimation(Forward, amount =1.0, duration =1.0)
move2 = MoveAnimation (Backward, amount=1.0, duration=1.0)
MyAnimation = DoInOrder(
MyObject.IsShowing = true,
move1,
turn,
move2,
)
return MyAnimation
You define move1 and move2 to move forward and backwards using Alice's MoveAnimation method. Then you set turn to give the model a spin using TurnAnimation. Finally, you make sure the object is visible with MyObjectIsShowing and run your series of animations.
AutoManga
Although now nearly defunct, AutoManga is a solution for digital cell animation. Japanese Manga-style animation is the idea behind AutoManga, and the engine is implemented with Python scripts that call C/C++ extensions for SDL routines. The engine was developed by Terry Hancock, has had a number of other contributors over the years, and originally was to be connected to the Python Universe Builder to handle interactive fiction and use XML for sequencing resource files.
Much of AutoManga was completed, including lighting effects and the ability to pull a few different formats for background images and animation cells, but the project unfortunately hasn't seen much action in the past year or two. Still, it is a good starting point for frame and cell based Python animation; the developer notes and files are located on Sourceforge, at http://automanga.sourceforge.net/.
Blender
Blender is a 3D graphics suite with a tumultuous history. Originally, Blender was a rewrite of the Netherlands animation house NeoGeo's 3D toolset. One of the co-founders of NeoGeo, Ton Roosendaal, also founded a spin-off company called Not another Number (NaN). This company's model was to further develop and market Blender technology. Initially this company faired very well, raising millions of dollars and gaining thousands of customers, but it was hit with hard economic times. In 2001, the company announced bankruptcy and the investors closed down NaN.
Blender, however, proved to have a strong will to live. Roosendaal started a non-profit foundation and began the "Free Blender" campaign with the idea of opening up Blender to the community as an open-source project. He worked with NaN's investors to agree to a plan wherein the Blender Foundation would be able to purchase the intellectual rights and source code of the Blender engine. Then, to the surprise of everyone, Roosendaal and several ex- NaN employees, with the help and support of Blender's loyal users, managed to raise 100,000 EUR in seven weeks to make the purchase. Blender was free, and continues to be free to this day, supported by developers and used by artists around the world, under the GNU GPL License.
Blender can be used for 3D modeling, animation, game-engine scripting (in some versions), and rendering. Most useful is Blender's built-in text editor (see Figure 5.7) for Python scripts, which can be used to customize tools, set up animations and effects, and even build sophisticated AI control over lighting and game objects.
Figure 5.7. Blender's text editor readily opens a Blender Python script
Blender offers a number of Python modules (shown in Table 5.5) to use in scripting. Some of them are still being ported into the newest version of Blender as of this writing.
Module | Description | Porting Complete |
---|---|---|
Blender | The main Blender module | yes |
BGL | The Blender OpenGL module | yes |
Camera | The Camera module | yes |
Draw | Display module | yes |
Image | The Image module | yes |
IPO | The IPO animation key module | no |
Lamp | The Lamp module | yes |
Material | The Material module | no |
Mesh | The Mesh module | no |
Nmesh | Low level mesh access | no |
Object | The Object module | no |
Scene | The Scene module | no |
Text | The Text module | yes |
Window | The Window module | yes |
To switch to the scripting mode in Blender, press the Shift and F11 keys simultaneously or go to the current Window Type button and choose Text Editor. Click the Browse Datablock button and choose Add New Blender to open a blank .py file. Blender will automatically name the file TX:text; you can change the name by clicking on it and typing in the new name (see Figure 5.8).
Figure 5.8. Highlighted text controls in Blender
To test out Blender, start by renaming a text file to MyFile.py, and then import the main Blender module. From that point on you have access to the Blender methods such as Object:
import Blender
MyObject=Blender.Object.Get("Some_Object")
When running scripts on objects in Blender, you would normally have two windows open. One would be a workspace with the object within, and the second would contain the Python script that you would run on the object.
Let's say you needed to run some complex math on a Mesh or Nmesh in Blender. First you import Mesh or Nmesh:
import Blender
from Blender Import Nmesh
Then grab the mesh object, its name, and its raw data using Object and Nmesh methods:
MyObject=Blender.Object.Get("Some_Mesh_Object")
MyMeshName=MyObject[0].data.name
MyMesh=Nmesh.GetRaw(MyMeshName)
Finally, run your complex math on each vertex, replace the values in your objects, and have Blender redraw the object:
for each_vertex In MyMesh.verts:
# complex math here
# complex math here
# complex math here
Nmesh.PutRaw(MyMesh, MyMeshName)
Blender.Redraw
Blender a is an excellent demonstration of the power of open source and open community development. Blender's user base is extremely supportive and creative, and is busily at work at making Blender the best appliance since toasters. You'll find information on Blender at
The Blender community site. http://www.blender.or.g
The Blender foundation site. http://www.blender.org/bf.
The Blender release page. http://www.blender3d.org.
Nebula
Nebula is an open-source, 3D, real-time, multi-platform game engine that supports Direct X and OpenGL. The project is brought to us by the game studio Radon Labs, in Berlin. Nebula is actually implemented with C++, but what makes it super-fun is that it is also scriptable with Python, Lua, and Tcl/Tk. I'll talk a bit more about Nebula later on in this book (specifically in the Lua sections).
Panda3D
Panda3D is a rendering engine for SGL. The core of the engine is in C++, but Panda3D also provides a Python scripting interface and utility code. I'll talk a bit more about Panda3D in the section on commercial games later in this chapter.
Poser
The Poser Pro Pack and Poser 5 come equipped with Python scripting as an available resource for artists; this is mainly used to automate advanced functions in the interface. Python scripts can be accessed from Poser's Window menu, which opens up a Python Scripts dialog box, as shown in Figure 5.9.
Figure 5.9. Accessing Poser's Python Scripts dialog box
The dialog box can be used as a placeholder for commonly used scripts. Clicking on a script with the Alt key on a PC or the Control key on a Mac will bring up a text version of the script that you can edit.
When creating custom scripts, much of the work on Poser is done through the Scene, which is part of the Poser import module:
# First Import Poser module
import poser
# Create a scene
MyScene = poser.Scene()
# Then you would do things to the poser scene
# And at the end re-draw the scene
Myscene.DrawAll()
Pretty nifty, huh? Poser actually has a very deep API for interacting with Python; it goes way beyond scenes and comes equipped with pre-defined scripts for you to use. There is also a fairly large knowledge base and plenty of sample scripts within the community.
Information on Poser can be found at Curious Labs's site, at http://www.curiouslabs.com/products/poser4#productinfo.
[ LiB ] |
No comments:
Post a Comment