25 #include <unordered_map>
26 #include "eng3d/utils.hpp"
27 #include "eng3d/log.hpp"
28 #include "eng3d/ui/components.hpp"
30 #ifdef E3D_FEATURE_PYTHON
32 #include "eng3d/pyvm.hpp"
33 #define PY_SSIZE_T_CLEAN
34 #define Py_LIMITED_API
37 std::unordered_map<int, UI::Widget*> python_widgets;
38 std::unordered_map<int, std::shared_ptr<Eng3D::Texture>> python_textures;
39 std::unordered_map<std::string, int> python_ui_callbacks;
44 if(PyImport_AppendInittab(
"eng3d", &[]() -> PyObject* {
45 static PyMethodDef eng3d_methods[] = {
46 {
"numargs", [](PyObject*
self, PyObject* args) -> PyObject* {
47 return PyLong_FromLong(10);
48 }, METH_VARARGS,
"Return the number of arguments received by the process." },
49 {
nullptr,
nullptr, 0,
nullptr}
51 static PyModuleDef eng3d_module = {
52 PyModuleDef_HEAD_INIT,
"eng3d",
nullptr, -1, eng3d_methods,
53 nullptr,
nullptr,
nullptr,
nullptr
55 auto* module_obj = PyModule_Create(&eng3d_module);
56 auto* module_dict = PyImport_GetModuleDict();
58 static PyMethodDef eng3d_core_methods[] = {
59 {
"numargs", [](PyObject*
self, PyObject* args) -> PyObject* {
60 return PyLong_FromLong(10);
61 }, METH_VARARGS,
"Return the number of arguments received by the process." },
62 {
nullptr,
nullptr, 0,
nullptr}
64 static PyModuleDef eng3d_core_module = {
65 PyModuleDef_HEAD_INIT,
"eng3d",
nullptr, -1, eng3d_core_methods,
66 nullptr,
nullptr,
nullptr,
nullptr
68 auto* core_module_obj = PyModule_Create(&eng3d_core_module);
69 PyDict_SetItemString(module_dict,
"eng3d.core", core_module_obj);
71 static PyMethodDef eng3d_ui_methods[] = {
72 {
"numargs", [](PyObject*
self, PyObject* args) -> PyObject* {
73 return PyLong_FromLong(10);
74 }, METH_VARARGS,
"Return the number of arguments received by the process." },
75 {
nullptr,
nullptr, 0,
nullptr}
77 static PyModuleDef eng3d_ui_module = {
78 PyModuleDef_HEAD_INIT,
"eng3d",
nullptr, -1, eng3d_ui_methods,
79 nullptr,
nullptr,
nullptr,
nullptr
81 auto* ui_module_obj = PyModule_Create(&eng3d_ui_module);
82 PyDict_SetItemString(module_dict,
"eng3d.ui", ui_module_obj);
93 auto* code = Py_CompileString(body.data(),
"test", Py_file_input);
94 auto* main_module = PyImport_AddModule(
"__main__");
95 auto* global_dict = PyModule_GetDict(main_module);
96 auto* local_dict = PyDict_New();
97 auto* obj = PyEval_EvalCode(code, global_dict, local_dict);
105 auto* name = PyUnicode_DecodeFSDefault(path.data());
107 auto* py_module = PyImport_Import(name);
109 if(py_module ==
nullptr) {
113 modules.emplace_back(py_module);
115 auto py_func = PyObject_GetAttrString(py_module,
"__init__");
117 if(py_func && PyCallable_Check(py_func)) {
118 auto py_value = PyObject_CallObject(py_func,
nullptr);
119 Py_XDECREF(py_value);
135 Py_XINCREF(this->obj);
140 Py_XINCREF(this->obj);
150 Py_XDECREF(this->obj);
void add_module(const std::string_view path)
void run_string(const std::string_view name, const std::string_view path)
PythonVM(Eng3D::State &_s)
std::string translate(const std::string_view str)
std::string translate_format(const std::string_view format, Args &&... args)
String formatter, with translation.
#define CXX_THROW(class,...)