Thursday, May 04, 2006

Vue Python for Beginners

An updated version of this article is now available on my website at http://www.impworks.co.uk/vue/vuepythonforbeginners.php.

Tonight, by special request, a short introduction to using Vue and Python for beginners.

What is Python

"Python is the industry standard, cross-platform, object-oriented application scripting language. It is both sufficiently easy to use and powerful to let you develop complex scripts and expand the capabilities of Vue 5 Infinite."

Or so says the Vue manual. An explanation that is as clear as mud.

Python is a computer programming language that lets people write things called scripts. It’s not the only programming language there are lot of others such as C, C++, Perl and Visual Basic. Scripts are computer programmes, little pieces of software that your computer can run.

E-on software included a version of Python in Vue 4 Pro and now in Vue 5 Infinite, which is why we are interested in it. Their version of Python includes extra features that let a programmer make Vue do things. A script may do something simple like adding stones to make a circle or complex, that can change the textures of lots of objects at once.

So where can I find Python Scripts?

There are some that come with Vue in the Python/Scripts folder on the Application CD. Others can be found on the Internet. A few places to try include:

Python scripts normally have a .py file extension (eg random_walk.py)

So how do I use a Python Script?

First of all it needs to be on your computer. Save it somewhere on your computer that you can find it again. It also needs to have been written for the version of Vue your using. Some scripts will require that you have the most recent release of Vue so they can work properly. It may say what version of Vue it requires or it has been tested with. The script may come with separate instructions or some people put them at the top of the Python script. If they are at the top of the script open it in a text editor (such as Word Pad on a PC) to read them.

For example at the top of random_walk.py there are some instructions.

#******************************************************
# Add a random walk animation to selected objects
# - random_walk.py
# - Give Selected Objects a simple random
#   animation
# - By Mark Caldwell
# - Version 0.1
# - 30th April 2006
#
# How to use in 4 easy steps
#
# 1. Download this file onto your computer
#
# 2. Edit the configuration variables below
#
# 3. Select Objects to be Replace in Vue Infinite
#
# 4. Then run script and wait for it to work
#    To run it go to Python -> Run Python Script
#    Then locate the file on your computer
#
#******************************************************

We’ll come back to those in a moment in detail, especially step 2.

If the script has instructions that need to be followed before it is run (like step 3 in the random_walk.py script) do them now.

To run a script you run Vue and go to the Python Menu. Next select "Run Python script…" . Then find the script on your computer, select it and click open. It should now run. Some scripts run quickly, some not so quickly depending on how complex they are. If you’ve run the script before, recently, it will appear in the list of five scripts on the Python menu saving you time finding it to run it.

The Instructions say to Edit the Script. Help!

Don’t worry; this isn’t as scary as it sounds. To be on the safe side you may want to copy the script first so you can go back to the copy if something goes wrong. I’ll take my random_walk.py script as an example again. This time we are looking a bit further down the script. You should find some of it that looks like this:


#----------------------------------------------
# Configuration: Set these to alter end result
#----------------------------------------------

# Frames to generate
start=0     # First frame of animation
stop=101    # Last frame of animation if step divides into
            stop add 1 to get a frame on last step
step=10     # Number of frames between key frames

# Starting Velocity of objects
velsxmin=-1  # Starting Minimum Velocity in the x direction
velsxmax=1   # Starting Maximum Velocity in the x direction
velsymin=-1  # Starting Maximum Velocity in the y direction
velsymax=1   # Starting Maximum Velocity in the y direction
velszmin=-1  # Starting Maximum Velocity in the z direction
velszmax=1   # Starting Maximum Velocity in the z direction

# Random Walk accelerations applied at a key frame
accxmin=-20  # Minimum Acceleration the x direction
accxmax=20   # Maximum Acceleration the x direction
accymin=-20  # Minimum Acceleration the y direction
accymax=20   # Maximum Acceleration the y direction
acczmin=-20  # Minimum Acceleration the z direction
acczmax=20   # Maximum Acceleration the z direction
By changing these lines you can change what the script does. Each line has a variable that you can change that will change how the script works when it’s run eg start=0 . This is setting the first frame of the animation. If I wanted to only have it animate from the tenth frame I could change it to stop=10 . If I want it to start from the fiftieth stop=50.

Once you’ve made the changes you want to save the script and try running it.

Hopefully it will run but if it doesn’t have a look at the Python Console in Vue by going to Python Menu then Python Console. There may be an error message there that tells you where the problem is (you may have put a letter in instead of a number for example). If you don’t understand an error message just go back to the copy and try again.

What Next?

Hopefully this has given you a taste of how to use Python Scripts in Vue. If you’d like to learn to write your own there are lots of web sites about writing Python in general. Python’s own web site is a good starting point ( http://www.python.org/ ). If you prefer learning from a book I'm a big fan of "Learning Python 2nd Edition" (published by O'Reilly Media Inc ISBN 0-596-00281-5). For special features of Vue Python see the documentation by clicking on the Python menu then Python Documentation in V5I. Python isn’t just for making Vue do new cool stuff you can write scripts that do all sorts of other things for you.

2 comments (leave your comment here):

Anonymous said...

Thanks for the helping lines, but I still don't get it: what is the script? Is it the whole page named random walk.py? If so, how do I copy it? Copy and paste? And how do I edit it? With a text editor?

impworks said...

How you get it will depend on your web browser.

In Mozilla goto the page and click File -> Save As and then save it as random_walk.py to where you want to store it on your computer.

In Internet Explorer goto the page and click File -> Save As and then save it as type Text to random_walk.py to where you want to store it on your computer. You may need to change it from random_walk_py.txt to random_walk.py either before or after you save it.

Once you've done this you should be able to edit it in a text editor (like Wordpad on Windows) or through an IDE (like IDLE that comes with Python). Depending on how your machine is set up you may need to select how you edit it.