Update location problem

Hello! I want to modify the location of an object by script, but it doesn’t work in the scene.
If I print the location before and after changing it, I see that the modification has worked great, but this change is not updated, so the next time I check the location before and after the next change, the location has lost the change and it maintains the original value.
Here the small code, and the console info:

you need use property for reading location position - if you assign position - insert pos = own.worldPosition in property pos[0], pos[1], pos[2] its XYZ, and check position in property if property position not equal 0.0 for you objects replace position from property data

Instead of:

bpy.data.objects['Cubo']

&

self.object.scene.objects['Suzanne']

Use:

self.object.scene.objects['Cubo']

&

self.object.scene.objects['Suzanne']

Thanks! I’m a noob in UPBGE. I use the common Python that is used in the standard Blender. I didn’t know that the game engine used a different kind of script. Too much to learn :slight_smile:

Thanks! I’ll check that new way to write the script.

Anyway, I have to say that bpy.data.objects[’ ‘] worked for me.
If I use self.object.scene.objects[’ '] instead, I have an error:
AttributeError: ‘KX_GameObject’ object has no attribute ‘select_set’

Surely I’m using different libraries. I’m not sure if I’m using the old game engine script, or if the script is not updated… Where is the last updated manual for UPBGE?

That’s because select_set is an BPY attribute… if you wish to get an BPY object using a KX_GameObject’s name, you can do:

bpy.data.objects[self.object.name].select_set

Hi, thanks! Yes, but that’s what I was already using:
bpy.data.objects[‘Cubo’].select_set(True)
or
mona = bpy.data.objects[‘Suzanne’]
And that works already great.
My problem is moving an object to a specific location. How would you move a cube to (2,4,1), for example? Just using Python (I’m not using nodes or blocks). Do you know how? Thanks.

Here’s the api:
https://upbge.org/docs/latest/api/bge.types.KX_GameObject.html#bge.types.KX_GameObject.worldPosition

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()

Suzanne= scene.objects['Suzanne']

Suzanne.worldPosition=[2,4,1]
1 Like

Fantastic!! :slight_smile: You’re a lifesaver! It worked great.
I had tried the worldPosition before (in the documentation, I had seen that it is a vector3). The problem is that it doesn’t explain very well. I was searching for “Suzanne” using:

Suzanne = bpy.data.objects['Suzanne']

and this method doesn’t recognize the worldPosition function.
But using:

scene = bge.logic.getCurrentScene()
Suzanne= scene.objects['Suzanne']

works awesome!
But I don’t understand well, because if I want to remove Suzanne, I can use:

Suzanne = bpy.data.objects['Suzanne']
bpy.data.objects.remove(Suzanne)

and it also works great, so it looks like there are some different methods to select an object, but depending of what you’re going to do, you have to use one or another thing. And that’s not explained in the UPBGE docs.
Thanks a lot again :slight_smile:

Suzanne.endObject()

Welcome to an alpha engine wich uses 2 types of codes,1 of the reason that i don’t like 0.3. bpy is blender internal script, bge is game engine script.

1 Like

Oh, man! Thanks a lot! I’m learning a huge lot today.
So, it seems that I don’t need “import bpy” at all, just import bge, right?
Thanks for the .endObject() function. :slight_smile:

if you use bpy script then you need to import it as well, if you use bge then importing bge is enough.

Thanks! You’re great.