Using blender game engine with Pydev or Pycharm

I’ve been looking online like crazy trying to find out how to use the blender game engine with Pycharm or eclipse. I usually come across a code completion tutorial. If anyone know where a step by step tutorial on how to do it or a video let me know. Thank you.

I usually just start a new project in pycharm and then make my blender files in the same directory as the pycharm project. As for code completion for blender I don’t worry about it because if I can’t remember a specific command Im used to looking it up in the api anyways. Then in blender, as long as the blend file is in the same directory as the python files you can just use them like usual.

Hope that helps.

OK, so I’ll try to walk through what I’ve found effective.

Firstly, PyCharm is pretty much functionally the same as IntelliJ IDEA with a Python plug-in, so instructions for either should transfer fine. IntelliJ works pretty well currently, and since I’m using Java and Python, it’s nice to have a single IDE for both. If you only want Python, PyCharm works fine, too.

In any case, I would recommend structuring your game in such a way that you put script files in a dedicated folder. This will help to keep your game nice and tidy. If you do this, though, you’ll need to set the path of the Python interpreter in Blender to look for that folder. Otherwise, it won’t be able to find your script files unless you type it out starting from the blend file (which the interpreter knows the location of). So, if you wanted to run the “run()” function of a Player.py file in your scripts folder, and you didn’t change the interpreter, you’d have to type “assets.scripts.player.run” in the module path for the Python controller of your Player object. If you add to the path, then the Python interpreter will look in that additional location for the script files. You can set the path either in a script in Blender, or in an external script in module mode.

Anyway, you would specify a path to a function that runs first and alters the sys module’s path variable (the Python path) to also point to your game’s script folder. It could be a simple function like this that resides in a file:



import sys
from bge import logic

def path_init(cont):

    sys.path.append(logic.expandPath("//assets/scripts/"))


This would get called by using Module mode on the Python controller and qualifying the path to the script starting from the blend file’s location (so, “assets.scripts.whatevername.path_init”). If the file is named “init.py” (with two underscores on the front and back of “init”), then you don’t have to type the file name (so “assets.scripts.path_init”).

Anyway, once this runs, you can access your scripts as normal with the Python interpreter looking in the scripts directory for them (i.e. typing “player.run” rather than “assets.scripts.player.run”).


As for directly integrating and using PyCharm / IntelliJ, I’d recommend creating a new project that is in the scripts directory of your game (or wherever you want the scripts to reside). After creation, the IDE should be able to handle code completion of custom classes and files with no problems. For trying to get the BGE’s functions to auto-complete, there’s no way to do so from the BGE’s Python distribution. Since the IDE can handle parsing .py files for autocompletion, though, you can refer the IDE to a folder that is full of “stub” functions and classes that the BGE uses, which allows you to have pretty effectual auto-completion. Moguri made such a folder of stubs here. If that link ever goes down, I also have it up for download on my Google Code repo.

Once you have the folder, you can either put it in your project, or put it elsewhere and direct your project (and any other projects you create) to the same single folder. To do this, you’d go to the File menu, and then go to Project Structure (IntelliJ). In the Project heading, select the Python interpreter you’re going to use (if you installed one already). I’d recommend this for spotting syntax errors, and it’s probably necessary for code completion, too. Under Modules, click the “Add Content Root” button and navigate to the folder that contains your BGE stub function and class folder. After doing that, you should see in the middle pane under the folder you selected the contained folders, one of which that should be the stub folder. Select that folder and select the mark as “Source” button. Apply, and I think that’s all that you need to do.

Your code should complete, the BGE’s function and class names should complete (though the signatures aren’t all correct), and it should all run correctly in the BGE.

Thanks for responding to my comment. So first I tried looking at pydev but it didn’t work well maybe I’m getting he steps wrong. I’m going to try solorlune way but tell me if I’m proceeding correctly (Btw I was inspired to program a game based on your dev videos good job with those. Waiting for the next video).

Using Pycharm

Steps:

  • On desktop I created a folder named game in in there a folder named src
  • Open blender and put this …(module mode)

import sys
from bge import logic

def path_init(cont):

sys.path.append(logic.expandPath("//assets/scripts/"))
  • in it along with a module that points to src file which is also where the blend file is.
  • Make sure the file is named init.py
  • From there open pycharm and you can open a new file that will print to blender and it will automatically interact with the init file.

Is this correct. Thanks for the feedback.

And if you can tell me with eclipse would be most appreciated.

1 Like