AttributeError: 'module' object has no attribute 'getKeyboard'

Hi all,

I’m following a tutorial from youtube on python scripting for the blender game engine. I’ve checked the code and it is identical to that in the tutorial, but I am getting the error “AttributeError: ‘module’ object has no attribute ‘getKeyboard’” in the console when I start the physics (“p”). The cube does not respond to the up arrow, either. Posting the blend file and the python script. I suspect this is a problem with the bge python not being aware of some path name or something, but I am not sure. The script is in a sub-folder called “Python” under the project folder where the blend file resides.

Blender 2.69
Ubuntu 14.04

Here is the content of the py file:
++++++++++++++++
import bge

Make sure to set up an Always sensor and a control

def main():
cont = bge.logic.getCurrentController()
cubeobject = cont.owner

# Get the scene for the mouse and other things
scene = bge.logic.getCurrentScene()

# Get a keyboard
keyboard = bge.logic.getKeyboard()

First check for a keyboard event and a specific key.

# Now check keyboard input (Input Status)
if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.UPPARROWKEY]:
    cubeobject.localPosition.y += 1.0

main()
++++++++++++++++++

Here is the console output:
++++++++++++++++++
Blender Game Engine Started
Python script error - object ‘Cube’, controller ‘Python’:
Traceback (most recent call last):
File “keyboard_input”, line 23, in <module>
File “keyboard_input”, line 13, in main
AttributeError: ‘module’ object has no attribute ‘getKeyboard’

++++++++++++++++++

The blend file is attached.

Any ideas?

Attachments

keyboard_input_learning.blend (486 KB)

When debugging Python scripting, the Python interpreter gives you hints to your problem. Before anyone else posts the immediate answer, take a look at the error console:

Python script error - object ‘Cube’, controller ‘Python’:
Traceback (most recent call last):
File “keyboard_input”, line 23, in <module>
File “keyboard_input”, line 13, in main
AttributeError: ‘module’ object has no attribute ‘getKeyboard’
What do you think it means - what is it telling you? It’s not there for decoration :slight_smile:

First of all, I’m 51 years old so please don’t talk to me as if I’m a child (you should never assume). I’m looking for help, not condescension.

Second, if I were familiar enough with python and the bge to follow a traceback in that scripting language, I wouldn’t be posting here. This is my very first attempt at scripting anything in the BGE, so I have no familiarity with any of the libraries, etc. It’s sort of the point of my asking such a basic question in this forum, ok?

Third, to answer your question, I have no idea. I am simply following a tutorial and this type of error does not occur in the tutorial. If I were more familiar with the BGE’s python scripting, I would have something to go on.

Please don’t turn this into a petty argument over whether or not I’m using my brain. Any ego worth it’s own estimation does not need to advertise, if you get my drift. Make room for someone to make a constructive response.

Please use code tags when submitting code.

What agoose77 means is that most cases it is worth to read the error message again and again. They are pretty clear, but sometimes they do not match the image we have in our mind.

In your case:
AttributeError: ‘module’ object has no attribute ‘getKeyboard’

simply means: there is no “getKeyboard”.

I can tell you this is correct and I wonder why that is mentioned in the tutorial.

bge is the (super)-module of the BGE API
bge.logic is a (sub)-module representing the “logic” part of the API

If you want to see all attributes of bge.logic use this code:


print( dir( bge.logic ))

To see the API documentation you can search for Blender API and/or BGE API.

In both cases you will see there is no “getKeyboard” attribute. But there is a similar thing the “keyboard” attribute. Maybe this is what you need, maybe not.

Beside of that, I think the above coding style is showing this code is written by a beginner.

Monster,

Thanks a bunch for that command to print the logic. Apparently I did brain fart and put ‘getKeyboard’ instead of just keyboard. However, still no worky. now I get “TypeError: ‘SCA_PythonKeyboard’ object is not callable”

Typically, you’ll get the “object not callable” error when you try to call an object that is not a function (ie: keyboard() instead of just keyboard).

I don’t know which tutorial you’re following, but bge python can be quite difficult for someone who doesn’t have a good grounding in python.

You might find this python series useful: https://www.youtube.com/watch?v=iGQv7bR6zCQ&list=PLDFB7FFF90EE6F0C1

Ok, so let me get this straight…

Blender has a set of functions, libraries, etc that are specific to blender albeit written in Python. These functions, libraries, etc evolve. When a person attempts to use these functions, libraries etc they may or may not get a nasty surprise. Perfectly straightforward statements which work in version 2.63a, for instance, may throw errors in version 2.69, for instance?

I want very much to continue loving to work in Blender. The question is, will Blender continue to want to work with us?

Sorry for the rant, guys, but I’m beginning to see very serious limitations to Blender which mostly involve under-documented changes, these changes causing users to have to spend the better part of their day figuring out instead of being a tool meant to be used. That’s a fact, pardners.

This might be helpful:

It’s the tutorial I’m trying to follow.

This is a pretty up to date documentation of the python API:
http://www.blender.org/documentation/blender_python_api_2_71_0/

And the Blender Foundation usually documents changes in the “What’s New” link on their website:
http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.70

Hopefully those links help! I’ve never really had a problem with the “evolution” but that’s just me.

Good luck!

Edit: If you stay up to date, you won’t have much trouble with the changes, they usually deprecate before removed or altered. (Also changes in API don’t that occur often if I recall)

Thank you, and I agree that ‘keeping up to date’ is a good policy, unfortunately I wear many many hats and cannot keep up to date with everything. I don’t see anything out there, commercial or open source, that compares with Blender’s potential. I would be happy to pay for blender, but they would need to spoon feed us poor ragged souls a little better, lol. Again, thank you for the links. I’ll book mark them and keep plugging. Thanks guys, I appreciate your help.

Let’s not set the tone of the rest of this conversation as that of an argument. I am trying to give you direction on how to solve your problem without consulting others, because in this instance, the Exceptions raised by Python are enough to indicate what the problem is. This is usually the case with nearly every problem one might encounter with Python. (The only exceptions are when Python, the BGE API internals or the OS is to blame - something we cannot see as users of the Python language).

The reason for my direction to others is that we see a number of new users raise very similar questions, and simply telling them what the solution is rarely changes their outlook, and if they encounter another problem of the same nature, they are unable to solve it for themselves.

Whilst you may not have experience with Blender to know the BGE API off by heart (and few people do), the reason I felt it acceptable to suggest reading the traceback was because it is not an API problem. If you had phrased your post indicating that you were aware that the issue was with the “getKeyboard” attribute, then you would already have demonstrated that you knew where the issue lay, but not the correct means to resolve it.

Monster has explained the Traceback structure, so I will leave that to his explanation, but hopefully you can understand given his response that my advice was intended to direct you to your own solution, even it was not the complete one.

I have not assumed that you are a child, and it seems that you have assumed me to have formed such a conclusion, equally without evidence. I had assumed that when read literally, there was enough information in the traceback to indicate where your error occurred, and what was implicated in that error (“getKeyboard”)


Concerning API changes, the BGE API has remained relatively unchanged over the course of its lifetime, typically only growing in size. The exception to this occurred during the API transition to a more Pythonic syntax where getOwner would be replaced with an attribute, and so forth. Tutorials from pre-2.49/2.5 would have been confusing for this reason, if you’re using the latest release of Blender.

Goran’s tutorial series is one of the most comprehensive on the subject of 2.5+ BGE Python, and I would recommend it.

Perhaps where you come from that wouldn’t count as a smart-ass tone, but believe me boy where I come from, that would have gotten you knocked on your ass – unless you were talking to a child (and then it might still have). I took umbrage, and I believe, rightfully so. You might consider leaving out unnecessary and provocative garnishments, like “…not there for decoration.”

On to something more productive…

After typing the command that monster recommended into a python shell in a CLI window, I get:

Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘bge’ is not defined

So I try it from the blender_console (the python console in the blender interface), and I get:

Traceback (most recent call last):
File “/usr/lib/python3.4/code.py”, line 90, in runcode
exec(code, self.locals)
File “<blender_console>”, line1, in <module>
NameError: name ‘bge’ is not defined

That section of code.py is " def runcode(self, code):"

Interesting… I would have thought that a blender installation and the attendant python libraries (is that the right term?) would make themselves available to python in the system.
Next question… is my Blender installation somehow broken? Possible. Is it my python installation? Possible. Is it something else? Film at 11.

Be aware they console is for Blender only. The BGE uses Python controllers either as script or as module. You can find some guides in my signature giving introductions to the architecture of the BGE.

Indeed you need to import the bge ino your local context before performing any operations on it.

Blender and the BGE are two separate applications. The BGE is embedded inside Blender and shares some data e.g. text blocks, scenes. The bge can run without Blender. It is called blenderplayer.

The BGE Python API is only useful in the context of the BGE. Outside of the engine, its API would hold no meaning, so it is not exposed as a Python module.

Even within Blender, you cannot import BGE modules unless you are running the code from within the engine, as it defines and removes those modules as you enter/leave the engine’s game loop.

Blender modules are also defined in this manner, only available from within the application. (NB, it is not recommended to use the Blender “bpy” module in the Game Engine (as it is only defined for the embedded player, and cannot operate on the BGE data), and there are some user scripts which make ill-use of this, so be careful when reading other source material).

You can build Blender as a standalone bpy module, and you can also get a standalone module for mathutils, but everything else is confined within the application.


Perhaps where you come from that wouldn’t count as a smart-ass tone, but believe me boy where I come from, that would have gotten you knocked on your ass – unless you were talking to a child (and then it might still have). I took umbrage, and I believe, rightfully so. You might consider leaving out unnecessary and provocative garnishments, like “…not there for decoration.”

Where I come from, that counts as a friendly approach to a problem. Many users do not even realise the purpose of the error messages out put to the console, hence the closing statement.

I am here to voluntarily to provide assistance to those who ask for it, amongst other motivations. There is no hierarchy here, you ask or you inform. If I had wanted to insult you, I would have done so directly, though I have already stated that that would be a waste of everyone’s time.

I would appreciate being addressed in the same manner everyone else would, and as a result, sentences such as “but believe me boy” and “knocked on your ass” are, in my opinion out of place here.

Agoose, you were being condescending. It was definitely not a friendly approach to the problem. I got the distinct feeling that you were hiding the words “why am I answering something you should already know”.

In reality there are plenty of reasons why that error could happen. Maybe it was calling that function on the wrong object. Maybe the function name changed (sounds like this was the reason). Maybe there was a spelling or case error. Do you think he would’ve asked the question if it was obvious what the reason for the issue was?

Indeed, there are many reasons why an exception can be raised, but the nature of the exception indicates where to start looking. As I have said, It was not my intention to suggest that there was no problem worth solving, instead I indicated that there was more to the traceback than just indicating something went wrong.

It was possible to observe from the traceback message that the issue was with an attribute that was not defined for the object it was requested from. Given that this wasn’t included in the original post, it presented an opportunity to enable the OP to embrace the mindset that is required to solve errors in code. As applications grow in complexity, errors can grow in complexity, to the point where you might spend several hours injecting test data into the application to see where it is malformed. (Though, good coding practices mitigate this substantially).
One of the most useful individual lessons that I have learned concerning programming was that every problem can be solved by reducing it into smaller problems. This applies to error finding, feature design and even modularity. This is a lesson that many tutorials will omit, and can hinder your progress quite substantially, often because the mental framework that the programmer is using to design their application might seem abstract or unyielding to those who are learning from them.

Agoose, you were being condescending. It was definitely not a friendly approach to the problem. I got the distinct feeling that you were hiding the words “why am I answering something you should already know”.

Again, I wouldn’t be here if this was the case.

As a point of clarification:

What do you think it means - what is it telling you? It’s not there for decoration :slight_smile:
I really feel I digress in analysing what I have written, but I feel this is a useful time to discuss communication.

If you read this with a scornful tone of voice, then of course it has a negative message. However, this was not the tone it was written in, and not the tone of voice that I have followed up with. Whilst I usually refrain from digressing from the matter at hand, I feel it particularly important to clarify this, given that I write in such a manner for the majority of my posts.

If there are those who wish to discuss this further with me, feel free to drop me a PM. Otherwise, we are going off topic now,

I think a lot of people project their own insecurities into the context, and inject malice into perfectly innocent statements; A kind of instinct-driven self-defense mechanism for the ego.

It’s a shame, because some things really need to be emphasized, but they’re usually interpreted as personal attacks.

As I can see even a tiny bit of sarcasm can make a good point bad and a thread like this going off-topic.

I guess the topics issue is resolved by now. So no need to discuss anything further here.

Update:
First of all, the reason I initially suspected a more fundamental problem than a simple typo is that I was getting whiffs of it in other contexts. While the problem with the incorrect call did indeed exist, I was correct regarding the more fundamental issues. As it turns out the default python binary in ubuntu 14.04 (at least in my installation, which is not a home-rolled kernel) is python 2.7.6. I discovered this when I was unable to follow Goran’s very first tutorial.

The solution to that particular problem was to make sure that python 3 was installed, and then to rename the binaries in /usr/bin so that ‘python’ called python3 instead of the legacy python2.

This did NOT, however, solve the blender issue. I am still efforting that and will post an update here when I have the issue untangled.

EDIT: Sorry I meant to put this in so that you folks understand what I mean by ‘the blender issue’:

From the blender console viewport:

>>> print ( dir(bge.logic))
Traceback (most recent call last):
File “<blender_console>”, line 1, in <module>
NameError: name ‘bge’ is not defined

And from the terminal screen when I attempt to run the game:

Blender Game Engine Started
Python script error - object ‘Cube’, controller ‘Python’:
Traceback (most recent call last):
File “keyboard_INPUT”, line 23, in <module>
File “keyboard_INPUT”, line 13, in main
TypeError: ‘SCA_PythonKeyboard’ object is not callable
Blender Game Engine Finished

It seems pretty obvious that the blender libraries are not being fully referenced, if at all. That’s the tack I’m taking for now, at any ratej.

As written earlier bge is the BGE API, which is not available in Blender at all. This means it is not available in Blender’s Python console nor when executing as Blender Python script.

You an use it with Python controllers ONLY. The Python controller defines custom logic bricks for the BGE.

In opposite: with a Python controller you should avoid bpy as this is the Blender API. Blender will not be available when running the blenderplayer. The Blender API uses Blender data, which is not BGE data. The BGE loads and converts Blender data (either from memory or from file) into the BGE when loading a scene.