Pass array of structs from Python to C

后端 未结 2 869
盖世英雄少女心
盖世英雄少女心 2021-02-08 17:44

[Update: Problem solved! See bottom of the post]

I need to allow python developers to pass an array of packed data (in this case vertices) into my API,

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 18:13

    Not tested but you should give this a try and let us know if its fast enough for your needs.

    On the python side, pack the vertices into a string instead of an object.

    str = "" # byte stream for encoding data
    str += struct.pack("5f i", vert1.x, vert1.y, vert1.z, vert1.u, vert1.v, vert1.color) # 5 floats and an int
    # same for other vertices
    
    device. ReadVertices( verts, 3) # send vertices to C library

    On the C library/python wrapper, modify your PyArgs_ParseTuple to use the format string "si". This will convert your python string into a C string (char*) which you can then typecast as a pointer to your vector struct. At this point the C string is a stream of bytes/words/floats and should be what you're looking for.

    Good luck!

提交回复
热议问题