Set Object Location From Python

I want to be able to set the location of an object that is not the owner of the script from a python script.
So if object a is executing a script, i want the script to move object b

My problem is i can’t get the name of the object i want to move right. I want to be able to use a variable as the name of the object.

for example



What do i need to replace OBSUN with to make it work.

My explanation is bad so please ask questions.

let’s assume that you want to move an object named “Cube”, and you want to move it 5 units from the origin along the x-axis.

import bpy[“Cube”].location = (5,0,0)

Does this make sense?

so you could set a variable, x, and use that instead.

x =[“Cube”]
x.location = (5,0,0)

this works in Blender 2.55 - I hope this is the version you’re using :slight_smile:

In 2.5x game engine it would be something like this (unchecked):

import bge

scene = bge.logic.getCurrentScene()
objList = scene.objects

obj_name = 'Cube'
cube = objList[obj_name]

cube.worldPosition = [1, 2, 3]

It’s been a while, but in 2.4x game engine something like this might work (unchecked):

import GameLogic

scene = GameLogic.getCurrentScene()
objList = scene.objects

obj_name = 'OBCube'
cube = objList[obj_name]

cube.worldPosition = [1, 2, 3]

In 2.4x or 2.5x the process is the same…

Get a list of objects in the scene objList

Store the name of the object you want in a variable obj_name

Retrieve the object you want from the list of objects objList[object_name]

Set the object’s world position to the value of your choice. I’m not sure if my example will work with passing a list, but it might. In 2.5x you can also set the world position to a mathutils.Vector which enables easier manipulation of the position.

Hope this helps. :slight_smile:

This is Blender but not the BGE.

Use FunkyWyrm’s solution instead. It is for the BGE.

I’m new to scripting I didn’t realize there were 2 API’s - I agree, follow the BGE example!

Double the api is double the fun. :wink:

It was confusing to me for a while. The way I see it now (and I may be mistaken) is that Blender and BGE are two different programs that communicate very well together, although the communication is only one way, from Blender to the game engine.

I believe Blender’s api is optimised for power and control because of the diverse nature of the program while BGE’s api is optimised for speed due to the realtime nature of games.

You are right.
When the BGE starts it converts all the Blender data to BGE data. The Blender data is still present and (if used in Blender) accessable and editable, but it has no effect on the converted BGE data. That’s why some users complain that the changes they did’t are active after the BGE ended. If you create a standalone with the Blenderplayer the Blender part is not available (at least until with 2.49). This might change with 2.5x.