Multiple bugs with Python/C++

I am currently working on a project that combines C++ with Python. The idea is that we use sensors to measure distances, translate this through C++ to Python and the Blender Game Engine into a movement. We use Blender 2.57b I am having some strange bugs though:

  1. For some reason I can’t just import classes and use their functions in Python. I have looked through tons of tutorials/examples and lessons and other people are able to use these functions, but self or imported functions such as pointer are being recognized without clearly stating their whole path.

  2. I am having a strange error with this bit of code:

>> values = Types.Sensor() 
>> Types.DLL.GetSensorStatus(Types.Motor.MOTOR1, ctypes.pointer(values))  

ValueError: procedure probably called with too many arguments (8 bytes in excess) 

The called function in C++ looks like this:

bool GetSensorStatus(Motor aMotor, Sensor &SensorValue)

Anyone have any idea what can cause this?

  1. I am using the following code to rotate objects:
 locArmL ='loc.l')
locArmL.rotation_euler[0] = angle

However this code doesn’t seem to work in the game engine itself, but it does work in the 3d view. So when I exit the game after running this code a few times without results, I suddenly find the objects rotated in the 3d view.

I want the rotation to be a fixed amount every time and I want to call it through Python without using logic bricks. Anyone know how?

Bumping the thread, added another question.

ctypes can as the name suggests “only” call c functions, not c++, else it would most likely be named cpptypes :slight_smile:

You need to tell your compiler that the the statement needs to be compiled c-style as your compiler will compile cpp.

If you got your header file for instance with

bool GetSensorStatus(Motor aMotor, Sensor &SensorValue);

you´d change it into:

 extern "C" {
bool GetSensorStatus(Motor aMotor, Sensor &SensorValue);

So if your compiler is a C compiler it will simply compile the declaration as it is, if it is a CPP compiler, it will wrap it in a “extern “X”{}” statement, so the compiler knows, this stuff is supposed to be C.
It is an compiler intern thing with function naming conventions.

You can also just use a

extern "C"  bool GetSensorStatus(Motor aMotor, Sensor &SensorValue);

declaration before the definition in the cpp file if I am not mistaken.

You also need to make sure, that all the linking is done properly else this´ll throw you lots of linker errors :wink:

You can also pass an object from a C++ class to a C function this way, defining the class in the headerfile with #IDEF cpp, then use class #ELSE use struct and pointers. It also requires a C callback function.

If you want to know more, also deriving classes that way, that´s a good read to start with:

If you got no C whatsoever, you might want to use Boost.Python. really elegant: