Embedded Python Module Configuration - c++

I'm looking at adding Python support to my game engine project, with the intent of using it as a scripting language for much of the game logic and world building. I'd like to expose a lot of the C++ code to Python. I've already gone through the relevant documentation on the Python site, and I'm fairly confident that I understand the basic requirements for embedding Python and communicating back and forth between Python and the core engine components.
I'm going to embedding Python into the engine itself, rather than compiling the engine as a Python module, since one of my target platforms is the iPhone.
One question I haven't been able to answer myself is how to introduce structure to the C++ modules. For example, I'd like to be able to do something along the lines of:
from engine.scene import skyobject
import engine.core.platforminfo
Would you just create a single "engine" module, and then add some sort of sub module for scene, core, etc? Or should there be separate modules for each of the exposed components, i.e. defining the skyobject module with the name "engine.scene.skyobject"?
Any insight on this is appreciated.

Related

Webots Programming with python

I am a Student in Newcastle University, currently undergoing my Master's Degree. My final project is a simulation using webots. I have to simulate a mobile robot, using python 2.7 coding. The problem is that I cannot find any documentation on python robot programming and the webots website has no python documentation. Everything in the website is based on C examples except from the names of some commands.
Where can I find python documentation for webots in the internet? Or If anyone has documentation can they provide me with it? It will be very helpful for my project.
Thank you.
The complete Python API of the Webots controller module is described in the sections of this chapter:
https://www.cyberbotics.com/doc/reference/nodes-and-api-functions
For example: https://www.cyberbotics.com/doc/reference/accelerometer?tab=python#wb_accelerometer_enable
In Webots, the Python API is exactly the same as the C++ oriented-object API, and the link with the C API is one-to-one too. So "translating" a Webots controller written in C / C++ to Python is often straight forward.
General instructions about programming a Webots controller (written for C) are true for Python too: https://www.cyberbotics.com/doc/guide/controller-programming
Webots contains several Python examples, including:
$WEBOTS_HOME/resources/templates/controllers/template.py (The template used when creating a new Python controller, reading the comments there should help you.)
$WEBOTS_HOME/projects/languages/python/... (a demo to study showing how to control a robot, communicate between robots, do Supervisor tasks, get the Computer keyboard, etc.)
Note also that all the benchmarks of robotbenchmark are written in Python and are using the Webots API (it could be a very good starting point to perform some of these benchmarks).

Sublime for python's Ctrl+Space function?

I have been using sublime-text to write my python codes, but have just realized there is a very useful Ctrl+Space command we can use in the default python IDLE, which makes me re-consider where to write the code again.
Is there any way sublime can do this? Or is there any other text editor that does it?
Check out the Anaconda plugin for ST3 (not related to the Anaconda Python distribution at all). It does a whole bunch of stuff, including intelligent autocomplete based on an object's type, as well as autocompleting methods of imported modules, along with other functions like linting, showing documentation, finding usage of an object, etc. It only works with Python, but really, what other languages are worth programming in? :)
Setup is pretty straightforward, and allows for you to have different configurations (and different Python interpreters, even virtualenvs) on a per-project basis. I've been using this plugin for quite a while, and I absolutely love it. It's much faster and more accurate than SublimeCodeIntel, and combines functions of several other plugins all in one place.

C/C++ Python interpreter

What I'm trying to do is write an app in C/C++ which will allow users to enter a Python script, which the app will then interpret & run. The aforementioned script will have a separate API that I'll implement in C, then expose to the user.
Is there a way to do this? I've searched on Google, but I've only found ways to create 'extensions' and not actual interpreters (like Lua allows).
Documentation about Embedding Python in Another Application says:
The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.
Look especially into Extending Embedded Python:
Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application.
Just read the docs I referenced and you should be able to implement Python interpreter within your app written in C.
You can do a call from C++ to Python and vice versa using Boost Python
If you're interested I recently implemented embedded Python scripting for C++ from first principles. I created a C++ Python wrapper library called ECS:Python (Embedded C++ Scripting with Python) that allows you to expose object from a C++ application to an embedded Python interpreter for interactive scripting. It's light-weight and very easy to use (and free!).
http://sourceforge.net/projects/ecspython

Prototyping Qt/C++ in Python

I want to write a C++ application with Qt, but build a prototype first using Python and then gradually replace the Python code with C++.
Is this the right approach, and what tools (bindings, binding generators, IDE) should I use?
Ideally, everything should be available in the Ubuntu repositories so I wouldn't have to worry about incompatible or old versions and have everything set up with a simple aptitude install.
Is there any comprehensive documentation about this process or do I have to learn every single component, and if yes, which ones?
Right now I have multiple choices to make:
Qt Creator, because of the nice auto completion and Qt integration.
Eclipse, as it offers support for both C++ and Python.
Eric (haven't used it yet)
Vim
PySide as it's working with CMake and Boost.Python, so theoretically it will make replacing python code easier.
PyQt as it's more widely used (more support) and is available as a Debian package.
Edit: As I will have to deploy the program to various computers, the C++-solution would require 1-5 files (the program and some library files if I'm linking it statically), using Python I'd have to build PyQt/PySide/SIP/whatever on every platform and explain how to install Python and everything else.
I want to write a C++ application with Qt, but build a prototype first using Python and then gradually replace the Python code with C++. Is this the right approach?
That depends on your goals. Having done both, I'd recommend you stay with Python wherever possible and reasonable. Although it takes a bit of discipline, it's very possible to write extremely large applications in Python. But, as you find hotspots and things that can be better handled in C++, you can certainly port relevant parts to C++.
Is there any comprehensive documentation about this process or do I have to learn every single component, and if yes, which ones?
Here's what I'd recommend for the various pieces:
EDITOR/IDE: Use any editor/IDE you're comfortable with, but I'd highly recommend one that supports refactoring. If you're comfortable with Eclipse, use it. If you want to mainly go the C++ route and you're not too familiar with any editors, you might be better off with QtCreator. Eric is an extremely good Python IDE with support for refactoring, unless you're going to be doing lots of C++, take a look at it. Even better, its source code is an example of good PyQt usage and practices.
PROCESS:
The quick summary:
Write your application in Python using PyQt
When identified as hotspots, convert decoupled Python classes to C++
Create bindings for those classes using SIP
Import the newly defined libraries in Python in place of their Python counterparts
Enjoy the speed boost
General details:
Write the application in Python using PyQt. Be careful to keep a good separation of concerns so that when you need to port pieces to C++ they will be separate from their dependencies. When you finally need to port something to C++, write it in C++/Qt and then create bindings for it using SIP. SIP has a good reference manual on the process, and you have all of PyQt as an example.
DEPLOYMENT:
C++ - For many applications the dependencies are sufficiently simple that it's not too difficult to create an installer using a tool like NullSoft's Installer or InnoSetup.
Python/PyQt - PyQt applications are a bit more difficult to install because of the dependency on Python and its dependence on the presence of the Qt libraries. One person documented his efforts on this post at ARSTechnica. py2exe works pretty well on Windows and should work fine. IME, freeze.py, which comes with the Python source, sometimes has problems determining which shared libraries are truly necessary and will sometimes end up creating a binary whose dependencies aren't present. Py2app can be made to work on Mac OS X.
But worse, however, is the PyQt/Qt licensing. If you are developing a commercial application, you need to have a commercial PyQt (and Qt) license and make sure to prevent the users from easily modifying the source or otherwise writing code against the PyQt/Qt API because of licensing restrictions. Because of that, the PyQt author created a tool called VendorId (although it has a Python license). Within VendorId is a tool called SIB that can be used to create an executable which depends only on the Python interpreter. But, if you're going to go this far, you might want to install a custom Python along with your application.
DISCLAIMER: I haven't used PySide at all, so I'm not sure how it compares to PyQt. Also, note the following warning on their website:
PySide is a work in progress and is not yet suited for application development requiring production-level stability.
But, on a good note, they intend, at least for the initial release to "maintain API compatibility with PyQt." So, aside from the C++ bindings, you could easily switch between the two later.
If you are just learning Qt and want to leverage the speed of prototyping that Python gives you, then I would recommend you make a sample project using PyQt. As you said, there is a debian package, so you are just a simple apt-get away from making your first application.
I personally use gVim as my Python/Qt editor, but you can really use any Python-friendly editor without much trouble. I liked WingIDE and they have auto-complete for Qt but once you sip from the vim kool-aid it's hard to switch.
I would say that PySide is 95%+ compatible with PyQt and the LPGL license is nice, but if you are just trying to prototype your first Qt app, then I don't think there is a real reason to use PySide. Although, I do like the PySide docs better, you can also just use them and replace all the library references with PyQt.
Depending on the complexity of the application you are building, it might be better off to just start from scratch with a C++ version than to try to do a bunch SIP refactoring black magic. Once you have a solid grasp of the Qt framework, you should be able to switch between the C++ and Python bindings pretty effortlessly.
I would draw UI mockups before starting to code prototypes. Here are some benefits:
Quicker than coding prototypes as there is no programming involved
Quickly fill widgets, such as tables and trees, with data
Add descriptions and notes to your screens
Easily integrate mockups into specification documents without having to capture screens
Validate UI design concepts before implementing
There are a lot of tools that can help you do that, but if you are going to use Qt, MockupUI may be a good choice as it renders Qt widgets with native styles for Windows 7,8 or 10 which makes your mockup look more realistic.

How does Gedit expose its api to python for plugins?

I'm starting a medium (academic) project in C++ for which I need users to be able to write small scripts, which interact directly with the main program. My first thought as an aproach to this was to make something like Gedit does with it's plugins (in fact I thought about it because it is something very similar to what I need to do.)
I do have some experience writting plugins for geddit, but zero experience in writting a plugin framework.
Would it be really difficult to me to write one similar to gedit's? (i mean, the way it exposes its API to python, and then loads the python plugin and calls its methods). Can anyone point me in the right directions or teach me a little if you have experience with it?
Fortunately, gedit's plugin framework can be used. You could use Ethos, which is the same plugin framework gedit uses, only without gedit.