Simple python script help

hi all,
I’ve recently learnt python (it was a module in my university course), I’m writing a simple script to make the title on the title screen of my game rotate a randomly by a small amount. I’ve got a first attempt but its not working, could someone take a quick look at it please?

The title is a mesh object in the scene (converted from a text object), it’s called ‘Title’.

Its the first python I’m coding specifically for a blender game, so its probably just something simple I’m missing:

import bpy
from math import *
import random

Xrot = 90
Yrot = 0
Zrot = 0
Xpos = -3
Ypos = 0
Zpos = 2

while 1==1:
Rand = random.randint(1,3)
RmodX = Rand * 0.5
RmodY = Rand * 0.2
RmodZ = Rand * 0.6

    Xrot = Xrot + RmodX
    if Xrot >= 95:
            RmodX = -RmodX

    if Xrot <= 85:
            RmodX = -RmodX

    Yrot = Yrot + RmodY
 if Yrot >= 5:
            RmodY = -RmodY

    if Yrot <= -5:
            RmodY = -RmodY

Zrot = Zrot + RmodZ
if Zrot >= 5:
    RmodZ = -RmodZ

   if Zrot <= -5:
           RmodZ = -RmodZ

bpy.ops.object.Title(loction = (Xpos,Ypos,Zpos), rotation = (Xrot,Yrot,Zrot))


Does this also freeze your game in general? I’m not sure how the BGE’s update threads run but it looks like your code has an infinite loop on your ‘while loop’ I don’t see a ‘break’ command or any other way for your code to get out of that loop. Also, your formatting was not preserved for your script so I cannot tell if your “bpy.ops.object.Title” call is in the loop or not.

thanks for taking a look at it

when I hit the run script button in the text editor bit, it gets down to the bpy.ops.object.title and says its an error is in the console. Cant find the console for some reason though?

Yeah the bpy.ops.object.Title is in the while loop.
I’ve added a little bit to allow the while loop to end by adding this to the loop:

ext = int(raw_input("Press 0 to exit:"))

if ext == 0:

which is all in that loop.

however it doesnt stop the program running if I press 0 and I still get a message about the bpy.ops.object bit?

Ok, I took a really quick look at how the BGE works and I think your wires are a bit crossed.

I think that what you really want to do is move your logic into a script that is run every few frames and you want to update the objects location directly instead of using the operator.

Furthermore, it seems that there are different properties and object types that you work with in the BGE than the standard ones you access when working in “Blender Proper”.

Check out the quick test I threw together. It just makes a cube bounce back-and-forth on the screen even .5 seconds. You can see the script that drives this logic. You basically need to replace my simple code with your randomization code, get rid of your while-loop, and get rid of the bpy.ops call.

You change the object location by manipulating the position and rotations directly on the object itself. What causes the script to run is the fact that it is tied to a “delay sensor” that runs every 1/2 second and calls my script “Text”.


bge_test.blend (442 KB)

Forgot to mention: You can show the Console by selecting “Help->Toggle System Console” from the menu in the “Info” panel.

thankyou for all the help

much appreciated :slight_smile: