Importing Blender Python modules in the standalone player

I’ve seen other threads on here and stackexchange about import errors for modules such as bpy. However, the solutions given are usually to build Blender as a Python module, and that may work for testing scripts outside of Blender. But this seems complicated enough as it is and I don’t see how this would work for an exported game which you expect other people to play on their machines, as they would surely have to have built Blender as a Python module and that is way too much to expect from the user.

So my question is if there is a way of exporting a game (that uses modules such as bpy) as an executable (with Python libraries) such that it can be played on any machine with the right operating system without having to install anything else. If not, is there an alternative to using these modules which achieves the same effect? Surely this is possible or no-one would be able to play games made in Blender without having Blender (and knowing how to start the game).

The standalone player isn’t a complete blender installation, which is kind of the point of a standalone player. The gameengine’s API is called bge, so instead of bpy you import bge. You won’t be able to do everything bpy is capable of.

If you need other modules you need to make them binary compatible with the python distribution used in your blenderplayer. Which is trivial for pure-python modules, trickier for compiled extensions and the like…

I’m not sure what you mean by compiled extensions; Python is interpreted. Also, are you basically saying that what I’m asking is impractical, in which case we can only make very limited games because of how essential many of these modules are? This problem isn’t new and I would have expected fixing this to be a high priority; as someone who has finished developing a game I am disappointed that it is only playable within Blender - and getting even that to work was immensely difficult.

I haven’t used the blender game engine ever. I can’t speak to how useful it is to game developers.

Maybe this will help you: http://www.cgmasters.net/free-tutorials/python-scripting/

No, Python is not interpreted. It is compiled into a bytecode and then executed in a virtual machine. For the CPython implementation at least, which is included in blender. There is also JIT implementation though. What I do mean by compiled extensions is that you can extend Python with C and other languages to improve performance or to create bindings for other libraries. These binary extensions need to be compatible with blender and its Python distribution, which is a difficult topic. Blender could be compiled against “system python” distributions, but that seems to be the exception rather than the norm.

What you are referring to as “building blender as a python module” means that Blender is compiled to run inside a normal python process, usually your system’s Python. That also is a very complicated topic, since Windows doesn’t come with a Python distro included, and then there are lots of other options. In my opinion blender should choose one standard external Python distribution, for example anaconda, and build its official binaries against that, instead of including their own python build. The latter almost feels “lobotomized” to experienced Python programmers, because installing external modules is so difficult.

You probably need to explore the BGE API to see what is possible there, and work around what is not possible. Maybe some of these functions can be replaced by your own code if necessary? You can also ask in this forum if you need a new way to do something.

If that’s the case I think the only thing I can do is expect users to install Blender; I’m quite sure that reworking most games in such a way would be impossible or very time consuming. Hopefully someone else can figure out a way to use compiled extensions or else some developers will make it possible in the near future (I am doubtful considering the decreasing development focus on the game engine).

Can you give me an example what you think doesn’t work in BGE?

I can’t speak for the blender developers, but I think there still is an effort for the BGE, and that the Game Engine isn’t currently understood to support sophisticated games. If a lot of “scripting” seems necessary for your projects, other engines, more focussed on programming, may be a better option.

I can only think of bpy.data.groups[] at the moment; I read that there is no alternative for accessing by group in the game engine and this is a big issue if you have groups consisting of hundreds of objects like I do. I too think that another engine would have been better, but I didn’t know that before I started with the BGE and that seemed more convenient given that everything else was in Blender.

I can’t really see why you would need access to groups in BGE. Most of what you want to do may be possible by using custom properties. Maybe even transfer group names to a custom property of every object called “groups” using a blender-side script.

I think groups would allow faster access to objects and also I’m not aware of a way to check if an object has a particular property without the script giving an error if the property doesn’t exist and having to catch the exception in bad coding style. This solution also relies on me/the game developer understanding Python scripting outside the game engine as well as inside it (which seem to be similar in some respects and therefore really annoying when they end up being different). Ultimately, this could be done in this particular example of groups, but I don’t think it’s as good a method as before.

Respectfully, I think your expectations of the game engine saving you from programming are a bit too high. If you want to use Python in the game engine, well, you need to use it.

That’s not at all what I was asking; I was merely hoping that once I had completed and tested a game for Blender’s embedded player that I would not have to write more scripts and modify all of my pre-existing ones on the chance that it then might work in the standalone player. There are many more problems with the game engine which require very awkward work-arounds and the fact that I have to use different APIs (or different ways of using Python) is just the icing on the cake which adds insult to injury. Blender is one of the best software applications ever created and it is sad to see a lack features that would be incredibly useful in the game engine while so much great development goes into minor features in other areas.

That being said, let’s not get into an argument about this and perhaps someone else may have a simpler solution to my (and others’) problem.