Python script with random access to an array of objects

(sudo) #1

Hi
I need a python script to organize a set of objects in an array and then select one element of the array at random and move the object selected with random force and direction… is it possible? any suggestion, link, tutorial, example, script?
Thanks!

0 Likes

(Social) #2

Yes, but I would recommend that you specify a scope/range of possible random values for applied force, because moving something too fast or too slow will probably produce unwanted results.

Here, I made a quick example for you:
http://socialstorage.googlepages.com/Help_sudo.blend

Edit* - Press space as many times as you want to pick & move a random cube object.

0 Likes

(sudo) #3

Thank You SO MUCH Social!!! This is just what I needed!
Ok thanks, now I’ll try some values in order to built a scope of suitable values for force… If I have some other problems I will ask you! Thanks again!

0 Likes

(sudo) #4

I have a problem, I tried to activate the script using a timer instead of the space sensor…

This is my script


cont = GameLogic.getCurrentController()
own = cont.getOwner()

if (own.timer>0):
    
    # Get a list of all objects in the scene
    allObjects = GameLogic.getCurrentScene().getObjectList()
    
    # Make a list of objects you actually want to affect
    wantedObjects = []
    for i in allObjects:
        if i.getName()[0:2] == "Key":
            wantedObjects.append(i)
    
    # Pick a random object from wantedObjects
    randobj = wantedObjects[int(GameLogic.getRandomFloat() * len(wantedObjects))]
    
    # Pick a random force factor for all directions
    randx = 0 
    randy = 0
    randz = int(GameLogic.getRandomFloat() * 10)
    
    # Feed values to applyImpulse, which makes things move
    force = [randx, randy, randz]
    randobj.applyImpulse(randobj.getPosition(), force)
    
    own.timer=-3.0

All the logic is on another object (not the "Key"s), which has a property timer initialized at -3, and a always sensor activating the script…

If I debug the value of the timer I can see it increases not so quickly as it should, and when it reaches zero it keeps increasing without resetting…

Moreover just after pressing P it seems all the "Key"s object slightly move in different directions and then stop…

But I don’t see where I’m wrong…

0 Likes

(Social) #5

The only thing that I can see in the script itself is the if i.getName()[0:2] == “Key” line. Shouldn’t you have [0:3] instead of [0:2]? I don’t see how your wantedObjects list can even be populated, since “Ke” != “Key”.

That said, I don’t see anything wrong with how your timer trigger method is implemented. Actually, if you do the same thing with my example it will run just fine. So make a comparison between my .blend and your project, note the things you did different -> one of those is most likely the cause of the problem.

0 Likes

(sudo) #6

Mmmm
I don’t know why, this works great
http://sudostorage.googlepages.com/Help_sudo_timered.blend
but this no
http://sudostorage.googlepages.com/jumpingkeys.blend

0 Likes

(Octopus4) #7

Thanks Sushi (Social) , for explaning … :slight_smile:
but can anyone help , because i’m python noob
what is meant by

if i.getName()[0:7] == "OBCube. " :

so to be specific , what is that [0:7] ?
sorry for the dumb question … i’m good modeler and texturer but noob in animation and script because i’m new to them
i mean 7 represents what ?

edit : - the objects that need to be picked from are 5 , why mention all objects cam and big cube

0 Likes

(Social) #8

From the “Game” menu enable “Show Physics Visualization”, and you’ll see what I’m talking about.

The physics bounds for your keys are completely out of whack, as are your object center points. That is what causes all your keys to “shoot out” of the keyboard really fast in the beginning, because the physics engine is detecting a major collision, and is then trying to push things out as fast as possible (which is also what makes your framerate so horrible).

How can you fix it? I don’t know. I tried resetting the center points and rescaling the keys to fit with the bounds, but that didn’t work out. I really have no idea how you got the bounds so messed up in the first place. I hope someone can help you with that.

Or you can use the demo that works, and set up the static keyboard mesh around that.

So there is nothing wrong with the script aspect…except for the fact that you’re using the wrong one in that file. You have like 5 versions of the script, but you’re still using that original one in your python controller. Use the correct version of the script for your purposes -> and get rid of all the other trash.

3DGURU>

Start a python interpreter in console, or use IDLE. Then run a test like this:
>> print “3DGURU”[0:2]
Then try this:
>> print “3DGURU”[0:4]

You get it? Most of the python things you wonder about are easy to figure out with just a little testing.

I don’t mention them. I just retrieve a list of all the objects in the scene, and then I tell python to pick out the ones I want. It’s easier than this:


myobj = GameLogic.getCurrentScene().getObjectList()["OBMyObject"]
myobj1 = GameLogic.getCurrentScene().getObjectList()["OBMyObject1"]
myobj2 = GameLogic.getCurrentScene().getObjectList()["OBMyObject2"]
myobj3 = GameLogic.getCurrentScene().getObjectList()["OBMyObject3"]
myobj4 = GameLogic.getCurrentScene().getObjectList()["OBMyObject4"]

wantedObjects = [myobj, myobj1, myobj2, myobj3, myobj4]

It’s much better to let python do all the work, especially when you have more than a few objects.

0 Likes

(Octopus4) #9

Yay , i understand now , thanks Sushi .

0 Likes

(Social) #10

Who is “Sushi”?

0 Likes

(Octopus4) #11

sushi = soshial = social :smiley:

0 Likes

(Social) #12
PYTHON SCRIPT ERROR:
0 Likes

(Octopus4) #13

[LEFT]

[LEFT]PYTHON SCRIPT ERROR:[/LEFT]

heh … :smiley: …[/LEFT]

0 Likes

(sudo) #14

Well, I don’t know, actually I took the model from here
http://www.katorlegaz.com/3d_models/index.php

Anyone can tell me how can I solve the problems of the physics bound and of the center points which pointed out Social? And any advise to obtain a decent framerate?
And anyway, I think this has nothing to do with the fact that the timer does not trigger the script… So why it doesn’t work?

0 Likes

(Social) #15
  • Because you’re using the wrong script. The script in your text viewer is not the script you are actually using in your python controller brick. The correct version of the script is “timer-random-obje.003”, the script in the controller brick (the script running) is “timer-random-object”. Make sure that the name of the script you want to use is the same name in the python controller brick.
  • But even if you used the right script, the bounds are soo fucked up, that the keys are shot completely out of view on the very 1st frame (and even more so since your mesh is small) -> so you won’t see the keys even when the script works. You have to fix/redo your bounds first, if you can that is.
0 Likes