Python,ctypes,多维数组 [英] Python, ctypes, multi-Dimensional Array

查看:529
本文介绍了Python,ctypes,多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python代码和C代码中都有结构.我填写这些字段

I have structure in Python code and in C code. I fill these fields

("bones_pos_vect",((c_float*4)*30)),
("bones_rot_quat",((c_float*4)*30))

在具有正确值的python代码中

,但是当我在C代码中请求它们时,我从所有数组单元中仅获得0.0.为什么我会失去价值?我的结构的所有其他字段都可以正常工作.

in python code with the right values, but when I request them in C code, I get only 0.0 from all array cells. Why do I lose the values? All other fields of my structures work fine.

class SceneObject(Structure):
    _fields_ = [("x_coord", c_float),
                ("y_coord", c_float),
                ("z_coord", c_float),
                ("x_angle", c_float),
                ("y_angle", c_float),
                ("z_angle", c_float),
                ("indexes_count", c_int),
                ("vertices_buffer", c_uint),
                ("indexes_buffer", c_uint),
                ("texture_buffer", c_uint),
                ("bones_pos_vect",((c_float*4)*30)),
                ("bones_rot_quat",((c_float*4)*30))]

typedef struct
{
    float x_coord;
    float y_coord;
    float z_coord;
    float x_angle;
    float y_angle;
    float z_angle;
    int indexes_count;
    unsigned int vertices_buffer;
    unsigned int indexes_buffer;
    unsigned int texture_buffer;
    float bones_pos_vect[30][4];
    float bones_rot_quat[30][4];    
} SceneObject;

推荐答案

下面是一个如何将多维数组与Python和ctypes结合使用的示例.

Here's an example of how you can use a multidimensional array with Python and ctypes.

我编写了以下C代码,并在MinGW中使用gcc将其编译为slib.dll:

I wrote the following C code, and used gcc in MinGW to compile this to slib.dll:

#include <stdio.h>

typedef struct TestStruct {
    int     a;
    float   array[30][4];
} TestStruct;

extern void print_struct(TestStruct *ts) {
    int i,j;
    for (j = 0; j < 30; ++j) {
        for (i = 0; i < 4; ++i) {
            printf("%g ", ts->array[j][i]);
        }
        printf("\n");
    }
}

请注意,该结构包含一个二维"数组.

Note that the struct contains a 'two-dimensional' array.

然后,我编写了以下Python脚本:

I then wrote the following Python script:

from ctypes import *

class TestStruct(Structure):
    _fields_ = [("a", c_int),
                ("array", (c_float * 4) * 30)]

slib = CDLL("slib.dll")
slib.print_struct.argtypes = [POINTER(TestStruct)]
slib.print_struct.restype = None

t = TestStruct()

for i in range(30):
    for j in range(4):
        t.array[i][j] = i + 0.1*j

slib.print_struct(byref(t))

当我运行Python脚本时,它调用了C函数,该函数打印出多维数组的内容:

When I ran the Python script, it called the C function, which printed out the contents of the multidimensional array:

C:\>slib.py
0.1 0.2 0.3 0.4
1.1 1.2 1.3 1.4
2.1 2.2 2.3 2.4
3.1 3.2 3.3 3.4
4.1 4.2 4.3 4.4
5.1 5.2 5.3 5.4
... rest of output omitted

我使用的是Python 2,而您问题上的标记表明您使用的是Python3.但是,我认为这不会有所作为.

I've used Python 2, whereas the tags on your question indicate that you're using Python 3. However, I don't believe this should make a difference.

这篇关于Python,ctypes,多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆