CTypes: Numpy Array Always has Different Values

  c++, ctypes, numpy, python

I’m trying to get an array from CTypes but it always has diffirent values. Here is my simple code:

C++:

struct OutArray {
    int     array_len;
    float*  array;
};

extern "C" // required when using C++ compiler
__declspec(dllexport) void getArray(OutArray *ret) {

    std::vector<float>  array_test;

    for (int i = 0; i < 100; i++) {
            array_test.push_back(0.3);
            array_test.push_back(0.1);
            array_test.push_back(0.2);
        }

    std::cout << array_test[0] << " C++ First Value "  << std::endl;

    // Return Array and its Length
    ret->array = array_test.data();
    ret->array_len = array_test.size();

    std::cout << array_test.data() << " C++ Array Object " << std::endl;

}

Python:

class OutArray(Structure):
    _fields_ = [("array_len", ctypes.c_int),
                ("array", ctypes.POINTER(ctypes.c_float))]

lib.getArray.argtypes = []


array_test = OutArray()

return_val = lib.getArray(ctypes.byref(array_test))

print('Py Return Value: ', return_val)
print(array_test.array_len)
new_array = np.ctypeslib.as_array(array_test.array, shape=(array_test.array_len,))

print('Py New Array: ', new_array)
print('Py New Array Type: ', new_array.dtype)
print('Py First Value * 2: ', n

And always when I run my code from python(in Blender 3d editor) I get different results. Sometimes they are correct but sometimes not. 50 to 50.

Log1:

0.3 C++ First Value
0000027C55F34B40 C++ Array Object
Py Return Value:  1
300
Py New Array:  [1.3229004e+03 8.9122582e-43 1.2467500e+03 8.9122582e-43 1.0000000e-01
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01
 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01
 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01
...........
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01
 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01
 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01
 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01 3.0000001e-01
 1.0000000e-01 2.0000000e-01 3.0000001e-01 1.0000000e-01 2.0000000e-01]
Py New Array Type:  float32
Py First Value * 2:  2645.80078125

Log2:

0.3 C++ First Value
0000022613667DA0 C++ Array Object
Py Return Value:  1
300
Py New Array:  [0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2
 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2 0.3 0.1 0.2]
Py New Array Type:  float32
Py First Value * 2:  0.0

You could see how I run the code 2 time and I get different results.
Also, I noticed even if the array of Log2 is correct but Py First Value * 2 = 0. It should be 0.6!

I assume that could be a problem with Sync or Blender.
Thank you!

Source: Windows Questions C++

LEAVE A COMMENT