Fun with a bit of Frustration learning Python

“Hello World”, I have been using blender since 2.5 n i never thought this day would come, for the past month i have finally taken the leap into Python. I am a bit new to the forum, (hopefully my etiquette is proper) especially when it comes to asking for help. I enjoy learning Python almost as much as i have enjoyed learning Blender, but am finding my frustration is knowing how to implement the blender API. Its a lot to take in for someone who is a bit older n has shy ed away from python but i am a gluten for punishment.

I’m trying to start simple, with a ‘cube’ (controller) with a ray sensor to detect the ground plane’s (‘property’) n in doing so will move n change color of the cube. I’m sure you can see (don’t laugh) but it’s not working. i think I’m close but again i need a bit of guidance.

###########################

from bge import logic

scene = logic.getCurrentScene()
cont = logic.getCurrentController()

own = cont.owner
ray = cont.sensors[“Ray”]
own.color = [0, 1, 0, 0]

ground = bpy.data.objects[“Plane”]
props = ground.name(“property”)

if ray.positive and props:
own.color = [1, 0, 0, 0]
own.setLinearVelocity((2, 0, 0), True)

###########################

Also, i plan on making a proper introduction in the future…For now thanx in advance for any response or help.

I’m curious how this hasn’t received any responses yet. Anywho, first off, this should probably go in the BGE forum as most of this forum is full of people looking to script python addons and such not pertaining to the BGE. Also, you should start using code blocks for your code posts to keep formatting.


import antigravity
if antigravity.active:
    antigravity.engageAwesome()

That being said…

You’re missing a few things. Bpy isn’t available outside of the blender interface, really. It’s not available in BGE.

For the code, everything looks good until you get to your bpy references. Instead of ground = bpy.data.objects[“Plane”], what you’re actually looking for is scene.objects[“Plane”] … IF you have a Plane object in your scene. This is the purpose of getCurrentScene in your script.

For the rest, what you’re wanting to do should probably be approached completely different. For that reference http://www.blender.org/documentation/blender_python_api_2_69_10/bge.types.html and http://www.blender.org/documentation/blender_python_api_2_69_10/bge.logic.html to get you started. It’s a good idea to just keep those pages open at all times. Good luck!

I’m not sure what this is doing:


ground = bpy.data.objects["Plane"]
props = ground.name("property")

‘ground.name’ is a string and you cannot use it like a function. I suspect that your problem is that if you look in your console you will see that this line is constantly throwing an error like, “TypeError: ‘str’ object is not callable”. That is preventing your ‘if’ statement from ever being reached so it never executes.

this

ground = bpy.data.objects[“Plane”]
is wrong

under bge, you should use ground = logic.getCurrentScene().objects[“Plane”]

and this I’, not sure :
own.color = [0, 1, 0, 0]
maybe you have to use float values.

And this ;

props = ground.name("property")

Maybe it’s

props = ground["property"]

Thank You, everyone for responding. You guys helped derail my frustration. I took all your advice n ended up with this. i understand where my mistakes were made, n think i have a handle on it well enough to continue.

from bge import logic

scene = logic.getCurrentScene()
cont = logic.getCurrentController()

own = cont.owner
ray = cont.sensors["Ray"]
own.color = [0, 1, 0, 0]

props = scene.objects["Plane"].get("property")

if ray.positive and props:
    own.color = [1, 0, 0, 0]
    own.setLinearVelocity((2, 0, 0), True) 

@jtsmith1287 thanks for pointing out the code blocks. Along with learning Python, I’m still learning how to properly post on this forum too.

thanks again.

No problem! Happy coding. :slight_smile:

Happy coding :slight_smile: