-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmdx_bindings_1.cpp
More file actions
157 lines (146 loc) · 7.5 KB
/
Copy pathmdx_bindings_1.cpp
File metadata and controls
157 lines (146 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026 Fernando Sahmkow
//
// AUTOGENERATED by tools/codegen — do not edit by hand.
// Regenerate via: python -m tools.codegen.codegen mdx --backend pybind11
//
// Source headers and `@bind` annotations live under include/whiteout/.
//
// Include order matters here:
// 1. pybind11/pybind11.h sets up the base library
// 2. project headers define whiteout::u32 et al used inside MAKE_OPAQUE
// 3. PYBIND11_MAKE_OPAQUE opts out of stl.h's auto-conversion for our vectors
// 4. pybind11/stl.h, stl_bind.h honor the opaque declarations
#include <pybind11/pybind11.h>
#include <array>
#include <cstdint>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
#include <whiteout/vector_types.h>
#include <whiteout/models/mdx/types.h>
#include <whiteout/models/mdx/structures.h>
#include <whiteout/models/mdx/parser.h>
#include <whiteout/models/mdx/writer.h>
#include <whiteout/interfaces.h>
PYBIND11_MAKE_OPAQUE(std::vector<std::string>);
PYBIND11_MAKE_OPAQUE(std::vector<std::vector<whiteout::Vector2f>>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::u32>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::u8>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::u16>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::Quaternion>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::Vector2f>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::Vector3f>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::Vector4f>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::f32>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Attachment>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Bone>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Camera>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::CollisionShape>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::CornEmitter>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::EventObject>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Extent>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::FaceEffect>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Geoset>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::GeosetAnimation>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Helper>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Layer::SubTexture>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Layer>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Light>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Material>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::ParticleEmitter2>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::ParticleEmitter>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::RibbonEmitter>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Sequence>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Sound>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::SoundEmitter>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::Texture>);
PYBIND11_MAKE_OPAQUE(std::vector<whiteout::mdx::TextureAnimation>);
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/operators.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
namespace {
// Buffer-protocol vector wrapper for std::vector<Elem> where Elem is laid
// out as `Components` contiguous Scalars (Vector3f, Quaternion, ColorBGRA…).
//
// pybind11's bind_vector<> wires up py::buffer_protocol() automatically,
// but stl_bind.h's auto-buffer-info path static-asserts on element types
// lacking a format_descriptor — Vector3f/Quaternion don't have one.
// We provide the same Python surface (append, extend, clear, __getitem__,
// __setitem__, __iter__, __len__, __bool__) plus a 2D buffer view.
template <typename Elem, typename Scalar, py::ssize_t Components>
auto bindBufferVector(py::module_& m, const char* name) {
using Vec = std::vector<Elem>;
py::class_<Vec> cls(m, name, py::buffer_protocol());
cls.def(py::init<>());
cls.def("__len__", [](const Vec& v) { return v.size(); });
cls.def("__bool__", [](const Vec& v) { return !v.empty(); });
cls.def("__getitem__", [](const Vec& v, std::size_t i) -> Elem {
if (i >= v.size()) throw py::index_error();
return v[i];
});
cls.def("__setitem__", [](Vec& v, std::size_t i, const Elem& val) {
if (i >= v.size()) throw py::index_error();
v[i] = val;
});
cls.def("__iter__", [](Vec& v) {
return py::make_iterator(v.begin(), v.end());
}, py::keep_alive<0, 1>());
cls.def("append", [](Vec& v, const Elem& val) { v.push_back(val); });
cls.def("extend", [](Vec& v, const Vec& o) {
v.insert(v.end(), o.begin(), o.end());
});
cls.def("clear", &Vec::clear);
cls.def_buffer([](Vec& v) -> py::buffer_info {
if constexpr (Components == 1) {
return py::buffer_info(
v.data(),
static_cast<py::ssize_t>(sizeof(Scalar)),
py::format_descriptor<Scalar>::format(),
1,
{ static_cast<py::ssize_t>(v.size()) },
{ static_cast<py::ssize_t>(sizeof(Scalar)) });
} else {
return py::buffer_info(
v.data(),
static_cast<py::ssize_t>(sizeof(Scalar)),
py::format_descriptor<Scalar>::format(),
2,
{ static_cast<py::ssize_t>(v.size()), Components },
{ static_cast<py::ssize_t>(sizeof(Elem)),
static_cast<py::ssize_t>(sizeof(Scalar)) });
}
});
return cls;
}
} // namespace
// Part 1 of bind_mdx(), which calls the parts in order.
void bind_mdx_1(py::module_& m) {
py::bind_vector<std::vector<whiteout::f32>>(m, "VectorF32", py::buffer_protocol());
py::bind_vector<std::vector<whiteout::mdx::Attachment>>(m, "VectorMdxAttachment");
py::bind_vector<std::vector<whiteout::mdx::Bone>>(m, "VectorMdxBone");
py::bind_vector<std::vector<whiteout::mdx::Camera>>(m, "VectorMdxCamera");
py::bind_vector<std::vector<whiteout::mdx::CollisionShape>>(m, "VectorMdxCollisionShape");
py::bind_vector<std::vector<whiteout::mdx::CornEmitter>>(m, "VectorMdxCornEmitter");
py::bind_vector<std::vector<whiteout::mdx::EventObject>>(m, "VectorMdxEventObject");
py::bind_vector<std::vector<whiteout::mdx::Extent>>(m, "VectorMdxExtent");
py::bind_vector<std::vector<whiteout::mdx::FaceEffect>>(m, "VectorMdxFaceEffect");
py::bind_vector<std::vector<whiteout::mdx::Geoset>>(m, "VectorMdxGeoset");
py::bind_vector<std::vector<whiteout::mdx::GeosetAnimation>>(m, "VectorMdxGeosetAnimation");
py::bind_vector<std::vector<whiteout::mdx::Helper>>(m, "VectorMdxHelper");
py::bind_vector<std::vector<whiteout::mdx::Layer::SubTexture>>(m, "VectorMdxLayerSubTexture");
py::bind_vector<std::vector<whiteout::mdx::Layer>>(m, "VectorMdxLayer");
py::bind_vector<std::vector<whiteout::mdx::Light>>(m, "VectorMdxLight");
py::bind_vector<std::vector<whiteout::mdx::Material>>(m, "VectorMdxMaterial");
py::bind_vector<std::vector<whiteout::mdx::ParticleEmitter2>>(m, "VectorMdxParticleEmitter2");
py::bind_vector<std::vector<whiteout::mdx::ParticleEmitter>>(m, "VectorMdxParticleEmitter");
py::bind_vector<std::vector<whiteout::mdx::RibbonEmitter>>(m, "VectorMdxRibbonEmitter");
py::bind_vector<std::vector<whiteout::mdx::Sequence>>(m, "VectorMdxSequence");
py::bind_vector<std::vector<whiteout::mdx::Sound>>(m, "VectorMdxSound");
py::bind_vector<std::vector<whiteout::mdx::SoundEmitter>>(m, "VectorMdxSoundEmitter");
py::bind_vector<std::vector<whiteout::mdx::Texture>>(m, "VectorMdxTexture");
py::bind_vector<std::vector<whiteout::mdx::TextureAnimation>>(m, "VectorMdxTextureAnimation");
}