BGE Code Completion in Pydev/Eclipse

Hi all,

I wrote a small script to generate .pypredef files for the Blender Game Engine. This gives you code completion for the bge and mathutils modules within Eclipse.


Here are the API declarations for Blender 2.55:
http://phatcore.com/projects/files/blender-bge-api.zip

To get started with this:

  • Install Eclipse.
  • Install Pydev inside Eclipse. It’s probably best to use the update manager.
  • Add the directory containing the pypredef files to the Predefined Completions section of the Pydev configuration.

Attached is the file used to generate the declarations (in case the API changes). Documentation is contained in the file. Just make sure you know what the current working directory is when you run Blender, as the .pypredef files will be written there. The easiest way to ensure you know which directory you’re in is to start Blender from the command line.

Edit: You need to import your modules like this to get it to work:

from bge import logic

Attachments

generateBgeApi.blend (78.8 KB)

Hello

That is an awesome project, but I could not get it work.
Here is my setup - Eclipse does not see GameLogic :frowning:


@koto
This is using the 2.5 BGE api, so it uses bge.logic instead of GameLogic

@z0r
Top level modules like mathutils and bge work fine, but submodules like bge.logic do not appear to work correctly for me. I’ve also added aud and blf to the list of modules, and they work fine.

Cool Idea, It always annoyed me that this code completion wasn’t there (but not enough to dig deeper :wink: ).

I will try at home.

I think I will take your file and produce a 2.49 version of it :p.

@Moguri
Ah yes, thanks for pointing out that it needs to be bge.logic instead of GameLogic. I also found that I can’t refer to it as bge.logic, though - I have to import it like this:

from bge import logic
c = logic.getCurrentController()
(etc)

Then you should have code completion for the logic module. The same goes for the other modules, like bge.types.

Sorry for not mentioning that. I must have been half asleep when I posted :confused:

@koto
Yeah, it looks like you’re using 2.4x. Let’s hope @Monster is successful porting it :slight_smile:

I don’t think

from bge import logic

will work in the BGE. The BGE submodules are a little weird in how they are made…

I changed the above file to run with 2.49. It works quite well. Unfortunatly GameTypes produces problems. I think without it, it will be worth just the half :S.

I will continue…

But it does work - at least, it does in GNU/Linux, when running inside Blender (I’m not sure about blenderplayer). I have several scripts that import the modules that way.

@Monster
That’s great news! Did you have to change much? I guess you would have had to port it back to Python 2.6 or so?

Hmmm. I wonder if there is some way to get it working with


import bge
bge.logic.whatever

as it would better suit my coding style. Any thoughts? Still cool though. :slight_smile:

I prefer that way too. I tried for a while to get it to work, but without success. I have tried: importing each submodule in the bge.pypredef file, and re-organising the submodules into subdirectories (like Python packages). But neither seem to work.

That’s not to say it can’t be done, though!

The generator is modified to work with 2.49.
There is a problem to extract the type of the instance variables. So they are all set to “None”.

In the meantime I will go through the files and add the right return types and replace the None with the right type (according to the API documentation).

I added the generator output to the blendfile, so you do not need to run it again. But you can if you want.

The main work was to replace the Python 3.0 syntax with 2.x syntax. Which wasn’t that much work. The code completion documentation recommend Python 3.0. So I do not know if there are any issues.

If you have any advice let me know.


Attachments

BgeApiGenerator.blend (325 KB)

@Monster
Cool! That looks good. Yeah, I wouldn’t worry about the types - I’m not sure they would be useful anyway. If you get completion without them, that’s probably good enough.

Very cool.

You can also set up code completion without creating .pypredef files for any modules (Blender or otherwise) where you have the source code. This includes everything in the Scripts directory (including bpy). Go to your project and right-click. Go to
preferences -> pyDev - PYTHONPATH -> External Libraries -> Add Source Folder
Then point to your /scripts/modules folder and that will pick up most things you normally get through bpy.

I tried to add bge also but couldn’t find it and then discovered that trying to import bge fails in my version so Monster’s script to create .pypredef files doesn’t work for me either. I’ve raised a question about that in a different thread, but, if you can find the bge Python API files (anybody know where they are?) you can add the path in the same way for automatic code completion.

The Python BGE API is all defined in C/C++ code via the Python C-API. This means you wont find any .py files laying around for the BGE. This is why the Pypredef files are needed. Although, I still have yet to find a way to get them to work with:


import bge
bge.logic.foo

Also, the bge module is a Blender 2.5 module that’s only available while the BGE is running.


mine shows every property, functions etc, when using from like in the image,

did nothing to the blender-bge-api, just downloaded then put to external libraries in the Project->Properties->Pydev-PYTHONPATH->External Libraries…