读取python中用于混合的文件中的原始数据

2024-06-08 12:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含几何数据的文件。几何数据是一系列向量,这些向量是每个基本体的位置、法线、纹理坐标和索引。这些文件以二进制格式存储,如下所示

32bit unsigned int nbVertices
32bit unsigned int nbIndices
nbVertices * 3 * 32bit float positions
nbVertices * 3 * 32bit float normals
...
nbIndices * unsigned int indices

我想将其读入python数组,以便在Blender中使用此几何体。 我可以一个元素一个元素地读取,一个浮点一个浮点地读取,并将其存储在一个数组中,但这需要在每一行调整数组的大小。我对Python的知识非常有限,但是在C++中(使用Qt帮助),我会使用类似的

QDataStream str(&f);
unsigned int nbVertices;
unsigned int nbIndices;
str >> nbVertices;
str >> nbIndices;

QByteArray positionsData = str.read(nbVertices*3*sizeof(float));
QByteArray normalsData = str.read(nbVertices*3*sizeof(float));
...
using Real3 = std::array<float,3>;
Real3 *positions = reinterpret_cast<Real3*>(positionsData.data());

我如何在Python中执行类似的操作


Tags: 文件数据元素数组float向量int浮点