Changeset View
Changeset View
Standalone View
Standalone View
extern/mantaflow/helper/pwrapper/registry.cpp
| Show First 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | public: | ||||
| void addPythonCode(const std::string &file, const std::string &code); | void addPythonCode(const std::string &file, const std::string &code); | ||||
| PyObject *createPyObject(const std::string &classname, | PyObject *createPyObject(const std::string &classname, | ||||
| const std::string &name, | const std::string &name, | ||||
| Manta::PbArgs &args, | Manta::PbArgs &args, | ||||
| Manta::PbClass *parent); | Manta::PbClass *parent); | ||||
| void construct(const std::string &scriptname, const vector<string> &args); | void construct(const std::string &scriptname, const vector<string> &args); | ||||
| void cleanup(); | void cleanup(); | ||||
| void renameObjects(); | void renameObjects(); | ||||
| void runPreInit(); | void runPreInit(PyObject *name_space); | ||||
| PyObject *initModule(); | PyObject *initModule(); | ||||
| ClassData *lookup(const std::string &name); | ClassData *lookup(const std::string &name); | ||||
| bool canConvert(ClassData *from, ClassData *to); | bool canConvert(ClassData *from, ClassData *to); | ||||
| private: | private: | ||||
| ClassData *getOrConstructClass(const string &name); | ClassData *getOrConstructClass(const string &name); | ||||
| void registerBaseclasses(); | void registerBaseclasses(); | ||||
| void registerDummyTypes(); | void registerDummyTypes(); | ||||
| ▲ Show 20 Lines • Show All 373 Lines • ▼ Show 20 Lines | #endif | ||||
| std::map<std::string, int>::iterator it; | std::map<std::string, int>::iterator it; | ||||
| for (it = mEnumValues.begin(); it != mEnumValues.end(); it++) { | for (it = mEnumValues.begin(); it != mEnumValues.end(); it++) { | ||||
| PyModule_AddObject(module, it->first.c_str(), Manta::toPy(it->second)); | PyModule_AddObject(module, it->first.c_str(), Manta::toPy(it->second)); | ||||
| // Alternative would be: | // Alternative would be: | ||||
| // e.g. PyModule_AddIntConstant(module, "FlagFluid", 1); | // e.g. PyModule_AddIntConstant(module, "FlagFluid", 1); | ||||
| } | } | ||||
| } | } | ||||
| void WrapperRegistry::runPreInit() | void WrapperRegistry::runPreInit(PyObject *name_space) | ||||
| { | { | ||||
| // add python directories to path | // add python directories to path | ||||
| PyObject *sys_path = PySys_GetObject((char *)"path"); | PyObject *sys_path = PySys_GetObject((char *)"path"); | ||||
| for (size_t i = 0; i < mPaths.size(); i++) { | for (size_t i = 0; i < mPaths.size(); i++) { | ||||
| PyObject *path = Manta::toPy(mPaths[i]); | PyObject *path = Manta::toPy(mPaths[i]); | ||||
| if (sys_path == nullptr || path == nullptr || PyList_Append(sys_path, path) < 0) { | if (sys_path == nullptr || path == nullptr || PyList_Append(sys_path, path) < 0) { | ||||
| errMsg("unable to set python path"); | errMsg("unable to set python path"); | ||||
| } | } | ||||
| Py_DECREF(path); | Py_DECREF(path); | ||||
| } | } | ||||
| if (!mCode.empty()) { | if (!mCode.empty()) { | ||||
| mCode = "from manta import *\n" + mCode; | mCode = "from manta import *\n" + mCode; | ||||
| PyRun_SimpleString(mCode.c_str()); | PyObject *return_value = PyRun_String(mCode.c_str(), Py_file_input, name_space, name_space); | ||||
| if (return_value == nullptr) { | |||||
| if (PyErr_Occurred()) { | |||||
| PyErr_Print(); | |||||
| } | |||||
| } | |||||
| else { | |||||
| Py_DECREF(return_value); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| PyObject *WrapperRegistry::createPyObject(const string &classname, | PyObject *WrapperRegistry::createPyObject(const string &classname, | ||||
| const string &name, | const string &name, | ||||
| Manta::PbArgs &args, | Manta::PbArgs &args, | ||||
| Manta::PbClass *parent) | Manta::PbClass *parent) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | #endif | ||||
| addConstants(module); | addConstants(module); | ||||
| return module; | return module; | ||||
| } | } | ||||
| //****************************************************** | //****************************************************** | ||||
| // Register members and exposed functions | // Register members and exposed functions | ||||
| void setup(const std::string &filename, const std::vector<std::string> &args) | void setup(const std::string &filename, const std::vector<std::string> &args, PyObject *name_space) | ||||
| { | { | ||||
| WrapperRegistry::instance().construct(filename, args); | WrapperRegistry::instance().construct(filename, args); | ||||
| Py_Initialize(); | Py_Initialize(); | ||||
| WrapperRegistry::instance().runPreInit(); | WrapperRegistry::instance().runPreInit(name_space); | ||||
| } | } | ||||
| void finalize() | void finalize() | ||||
| { | { | ||||
| Py_Finalize(); | Py_Finalize(); | ||||
| WrapperRegistry::instance().cleanup(); | WrapperRegistry::instance().cleanup(); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines | |||||