The whole idea
See "Yo Frankie" tutorials for the concept of linking Groups into a 'main' file. (http://www.yofrankie.org/download/)
Basically you keep characters and props in seperate files and you Group them. Then you link and add the groups into another file for use.
Normally (in BGE 2.49b) those groups cannot be animated in a simple way. They do bring all their own (personal to the sub blend file) animations and so on with them, but you can't impose another set of actions on the Group.
Until now. (Sound of drums....)
A car driving along a road. You model the car in car.blend, you give it all the stuff a car needs. You then parent the parts of the car to some master/base object and you then add all those parts to 'CarGroup'. Good to go.
Now, I argue that the actual forward motion of the car is nothing to do with the car.blend file. (Well, for a game perhaps not.)
I say that the mainscene.blend file (or whatever you call it) is where the car should be 'driven' around. Look at it from a 'presentation' pov. If you are not writing a game, but some more passive kind of animation. How would you make that car follow a path and appear to be driving along?
You open mainscene.blend and you get that CarGroup linked and added. Then you naturally grab its Group and possibly add some IPO's for motion and rotation. (I do not know BGE well enough to speak of path constraints, or what have you, so insert your technique here.)
If you animate this via ALT-P it seems to work. (Not for logic bricks though.) But when you Play your game, the car just sits there looking snazzy, but very still...
Huh? WTF? Halp!
You need a way to control that Group! So, the idea of this GroupControl technique is to do that -- and for multiple instances of CarGroup too (so you can have many cars with different logic bricks and animations.)
The 'technique' involves a little bit of setup, some care and a bit of special naming of objects. Read on, it's not that hard.
The file layout
You are free to change this or adapt it to your own system. I am not experienced enough with BGE to know the best-practice, so this layout is my own trial and error; I may come to regret it in the future, who knows? :D Caveat Emptor.
Directory names in quotes. This is all relative to wherever you are.
"root" (or any name you like) | "lib" |-- "agents" | GroupControl.blend (APPEND this to your file) | |-- "scripts" | __init__.py (empty file - required) | CustomGameInit.py | GroupControl.py | |-- "groups" | cars.blend (or whatever) | hosepipes.blend (etc.) | |-- "scenes" | mainscene.blend (Your main/top file) | aaa_init.blend (LINK this to your file)
- LINK the object aaa_init from this file into your mainscene.blend file.
- It has a script block that is set to run first (star).
- It has two jobs:
- Find the "scripts" directory and set Blender's path to point at it. This is so that import X commands will work in Python.
- Import the CustomGameInit module. This sets-up some stuff we need for the future.
- APPEND the object "GroupControl" from this file into your mainscene.blend file.
Sadly, if you LINK it, it does not run in a predictable fashion -- sometimes this happens before aaa_init, which is a mess.
- The script of this object will search the scene for all the Group objects that you have 'tagged' and it makes a note of them for later. (See mainscene.blend for tagging details.)
|(Optional) Add a property to it named "GroupTag" and set the value to some unique string that will identify the groups in your scence. This is how it knows which Groups to select.|
- This is up to you. It contains your models etc. They can have logic bricks and the whole 9 yards. You could have many cars in this file, but each one must be in a Group.
- Now, the important steps:
- Have a parent of all the parts of each car within that car's Group. An empty is usually best. We will call this parent the "base".
- Make sure the base is at [0,0,0] ... OR ... set the Group Offset(Object F7, Objects and Links, next to Groups field) to the same as the x,y,z of the empty itself.
- Select the base and:
- Add an action (a Python Module controller.) Make sure it only fires once (don't set the activate True/False buttons.)
- Add this in the Module field: GroupControl.find_my_group. The job of that function is to go and find the Groups that have been selected already (by the GroupControl object) and then parent the base to each of those Groups. It sounds weird (and it is) but it works.
Do 1 to 3 for each car Group in the scene.
- Again, it's up to you. This is the main file from which you link-in the various car Groups from cars.blend. (Or the fish from trout.blend, or the trousers from ...) It will have a bunch of Group objects scattered about.
- Now is the time to 'tag' those groups that you want to control. Select them one by one and do either:
- Object Name: Include the 'tag' you specified in the GroupControl object (GroupTag property) in the name. So, for example, if your GroupTag is "BLAH" then name the group "RedFerrariBLAH" or "spaceCarBLAHone". As long as 'BLAH' is in there, it will be selected.
- Object Property: Give it a property with the name of the tag. As long as there is a property called 'BLAH' it does not matter what it's value is. (It can be of any type like string, or Float etc.)
That's it. I hope. Play your mainscene file and your Groups should now be moving as you want them to.
You can find a sample zip file here. It contains all the directories, blender and python files needed to run the demo. In lib/scene, start "top.blend" Media:GroupControl.v1.zip
I have not tested this very much. I have not pushed it either. I don't know what will happen if you make groups of groups and have multiple blend files nesting each-other. Things could break; then again, it might just work.
If you find bugs or ways to improve this, then just contact me:
- "dotherwise" on blenderartists.org
- "d o n n d o t i n g l e a t g m a i l d o t c o m"
Copyright (C) 2009 Donn C. Ingle Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.
Versions of other software used
- Blender Version: 2.49b
- Python Version: 2.6
- Platform: Kubuntu Jaunty (2009) (should be cross-platform)
"Monster" on the blenderartists.org forum. His help and LinkLib script helped me to get a handle on this whole thing.
Other Free Software
I have afflicted the planet with some other software too.
Here you will find:
- Fonty Python
- Blender Water Texture plugin
HTH, Dotherwise 17:47, 7 November 2009 (UTC)