Get Object With Python?

I know that you can get the object that the python controller is attached to with “.getOwner”, but how to you get another object in the scene? Or another scene for that matter?
Any help would be greatly appreciated.:slight_smile:

like this:

scn = GameLogic.getCurrentScene()
obl = scn.getObjectList()

object = obl['OByour_object']

You have to append ‘OB’ infront of the object name or else it won’t find it, but stay alert because I think is getting depreciated in 2.49 or 2.50 (the ‘OB’ bit that is)

object is the same class as the owner, so you can still do all the getPostion() setPosition() stuff you normally do to it.

Edit:
Just saw your question about the other scene, I haven’t actually tested this before but in theory it should work. Create a script in the scene that has the objects:

GameLogic.scene2 = GameLogic.getCurrentScene()

Then in the scene you wish to access it from do:

 obl2 = GameLogic.scene2.getObjectList()

Thank you!

GameLogic.scene2 = GameLogic.getCurrentScene()

Then in the scene you wish to access it from do:

 obl2 = GameLogic.scene2.getObjectList()

In theory it should, but in reality it doesn’t. :smiley: I tested it out, and then did a search on it because I couldn’t figure out why it wasn’t working. The reason is quite simple. Blender will only load the objects in the current scene. Switching to another scene causes the objects in the current scene to be removed. So, the objects in “GameLogic.scene2” become non-existent once you change to another scene.

Hmmm, even if you use an overlay/underlay scene?

I used this with an overlay scene (which is what I asked the question for) and it worked!
Haven’t tried switching scenes yet but may give it a try sometime.

Oh, ok. My bad, I thought you were using it to access objects in a scene from a new scene. Yes, overlay will work, as well as background and anything else which has both scenes active at the same time (the one from which you’re trying to access the objects and the one you’re accessing them from). The whole idea is Blender saves memory by not loading the objects it doesn’t need at the time. The objects in an overlayed scene are needed, but those in a scene which is not active aren’t. Sorry for the misunderstanding.

I was trying to do this and never thought of using global variables,
Thanks for the info Andrew!

Can we get actuators from other objects and activate them? If so how?

P.S. I’ve considered using search, but I don’t really what I’d put down in the search.

@mattroe: I’m pretty sure that’s not possible at the moment.

Well, actually it’s possible:
First you have to apply a script like this to every object you want to access later:


import GameLogic as g

scene = g.getCurrentScene()
objList = scene.getObjectList()

try:
    GameLogic.DictObjects
    init=1
except:
    init=0
if init:
    cont = g.getCurrentController()
    own = cont.getOwner()
    name = own.getName()
    if not GameLogic.DictObjects.has_key(name):
        GameLogic.DictObjects[name]=cont
    own.init=0

This script gets the the current object controllers and add them to a list( DictObjects[] )
Set a property “init” to the object so the script executes only once.

So now you have a list of object names and their controllers.
Here is how to use them(from any other script of your game):

First call an object from the list:

cube= GameLogic.DictObjects['OBCube"]

Then get its controllers:


contrSensorKeyPress=cube.getSensor("RKey")
contrActuatorRotate=cube.getActuator("Rotate")

You can also set different object properties:


cubeObj=obj.getOwner()
cubeObj.setMass(100)
cubeObj.init=0

ect…

Hope this helps anyone.

Sweet!!! I’ve been trying to do that for a long time! Thank you! Ya learn somethin’ new every day:)

Thank you so much haidme, that’s EXACTLY what I was looking for( I have a bunch of objects and I don’t want to have to connect each one’s actuator to the main script, cause more are gonna get duplicated again later,too tedious), there’s no way I would have figured that out for myself in a reasonable amount of time, lol. Thanks man! Also, thanks for asking the question mattroe!