0
$\begingroup$

I am attempting make change spatial properties of a cube by using a Python script which is triggered by keyboard logic. After pressing W the following script gets executed:

import bpy
import mathutils

Cube = bpy.data.objects['Cube']
Cube.location += mathutils.Vector((1, 1, 1))

print(Cube.location)

As seen on the console output in the screenshot, the cube's location gets updated correctly, but in the game view nothing happens. Whenever game mode is exited, the cube is suddenly at the location that the script has placed it.

Am I overlooking some setting or connection here? Using the keyboard modifier to simply change the cube's location in the logic bricks does work, but using a script which changes the location has no result in the game view.

blender with BGE running and console output

$\endgroup$
1
$\begingroup$

There is no bpy module in game (same problem as this one and this one).

You really are moving the cube. Everything you described proves exactly this.
The console output shows the location changing, and when you exit the game the cube is in the new location.

Assuming blender is laid out like you have it in that screen shot. Exit the BGE, then press the "Run Script" button in the text editor. That will run your code, you will see the cube move every time you click the button.

Now how to make it work.

You need to use the bge module.

To move a cube in game like you are doing now would be like this:

import bge, mathutils

# the object holding the logic brick running this script
obj = bge.logic.getCurrentController().owner

obj.worldPosition += mathutils.Vector((1, 1, 1))

Or if you want to get the object by name:

obj = bge.logic.getCurrentScene().objects['Cube']
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.