This is the first of a series of tutorials focusing on using the Python Scripting Language to extend the Blender Game Engine. They are all based on Blender 2.41 and Python 2.4.Also download the Python 2.2 manuals as we will be referring to them from time to time(should come with Python 2.4).I can recommend doing the intro tutorial before attempting these pages. I have assumed a basic Blender GE knowledge This is very much based on the tricks I have learned over the years from experimenting with other peoples scripts and blend files. Thanks to all.I tend to use python to manipulate properties more subtley than you can with logic bricks alone.I also use the copy property and the IPO property actuators a lot so make sure you are familiar with these. |
|||||||||||
![]() |
|||||||||||
|
First up we will use a script to vary the volume of a sound depending on a property, in this case I wanted a rail rumbling sound to increase and decrease in volume as the train went faster or slower.The above image shows the finished set up. 1)First I added an Empty(Always add objects from the top view(NumPad7) to get matching Global and Local orientation) to "carry" the sound and the script. 2) Then I parented this empty to my coal cars so that the sound will go where the coal cars go.Find yourself a suitable looping engine sound(I pinched this one from MS Train Simulator) and load it into Blender via the audio window. If you want 3d sound go to the sound block buttons(Found via F10) and set the new sound to 3d and you might need to vary the scale depending on your world.See Below. a) and you have to use a sound with a suitable format b) |
|||||||||||
![]() |
|||||||||||
3)Now the Empty needs to know how fast it is going.In this case we will copy the m(momentum/speed) property from the locomotive(SteamEmpty) and copy it into the mom float property added to the empty.This is the first line of logic bricks in the image below.You could use this to copy engine revs or wind noise etc for your own stuff or even how close you are to an object. 4) Now the script. Go to the Text Editing window and add a new script and call it a relevent name.This will help you keep track of things.The first 2 lines of the rumble script let python find the object you have connected the script to.(See 2nd line of logic bricks)."Own" is basically the object we are dealing with(Empty.006).I use an always sensor so the script is always checking the "speed" of the loco and you must connect the python controller to the sound actuator . 5) In line 12 of the script we get the sound actuator so we can play with it and it is defined here as sound. 6)Now we define and copy the mom value from Empty.006(own) into the script using line 14 and define the "rum" value with line 15.The mom variable we copied actually goes from zero to about 1200 so we have to divide by this amount before we set the gain(volume) because the actuator is expecting a value between zero and 1. |
|||||||||||
![]() |
|||||||||||
7)In line 17 we set the gain according to the rum value and finally we have to add the actuator(sound)back in, remember we named the act1 actuator "sound" .The next variable "1" is to turn the actuator on. I hope that this all made sense and everyone can get something from it. I was hoping to explain the ideas behind the scripts rather than just give a "Do this then do that and press this button to make etc" recipe. You need to know how to read the python documentation for syntax,what is possible or not etc.Also the blender python API docs are essential. And I know this is a bit harsh but no blend to download, as you need to know how to debug.If I have had made an error Sorry :) I didn't mean it. If you have probs look in the console window(size your windows so you can see all.This will be critical later when things get really complicated). A friend describes what I do with python as liquid algebra but I did 1st year university engineering algebra and this is heaps easier. Trust Me.Name your variables sensibly so you can keep track.And if you are really stuck post at blender Artist(elysiun) and I will try to help.
Dr S |
|||||||||||
| Back to Tutorials Page | |||||||||||