forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneLoaderPY3.cpp
More file actions
149 lines (122 loc) · 5.53 KB
/
SceneLoaderPY3.cpp
File metadata and controls
149 lines (122 loc) · 5.53 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
/******************************************************************************
* SofaPython3 plugin *
* (c) 2021 CNRS, University of Lille, INRIA *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Contact information: [email protected] *
******************************************************************************/
#include <sstream>
#include <fstream>
#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/graph/DAGNode.h>
using sofa::simulation::graph::DAGNode;
#include <SofaPython3/PythonEnvironment.h>
#include <SofaPython3/SceneLoaderPY3.h>
#include <SofaPython3/PythonFactory.h>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
namespace py = pybind11;
#include <sofa/helper/system/SetDirectory.h>
using sofa::helper::system::SetDirectory;
using namespace sofa::core::objectmodel;
MSG_REGISTER_CLASS(sofapython3::SceneLoaderPY3, "SofaPython3::SceneLoader")
PYBIND11_DECLARE_HOLDER_TYPE(Base, sofa::core::sptr<Base>, true)
template class py::class_<sofa::core::objectmodel::Base,
sofa::core::sptr<sofa::core::objectmodel::Base>>;
namespace sofapython3
{
bool SceneLoaderPY3::canLoadFileExtension(const char *extension)
{
std::string ext = extension;
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
return (ext=="py" || ext=="py3" || ext=="py3scn" || ext=="pyscn");
}
bool SceneLoaderPY3::canWriteFileExtension(const char *extension)
{
return canLoadFileExtension(extension);
}
/// get the file type description
std::string SceneLoaderPY3::getFileTypeDesc()
{
return "Python3 Scenes";
}
/// get the list of file extensions
void SceneLoaderPY3::getExtensionList(ExtensionList* list)
{
list->clear();
list->push_back("py3scn");
list->push_back("py3");
list->push_back("pyscn");
list->push_back("py");
}
bool SceneLoaderPY3::syntaxForAddingRequiredPlugin(const std::string& pluginName,
const std::vector<std::string>& listComponents, std::ostream& ss, sofa::simulation::Node* nodeWhereAdded)
{
ss << nodeWhereAdded->getName() << ".addObject('RequiredPlugin', name='" << pluginName << "')";
if (!listComponents.empty())
{
ss << " # Needed to use components [" << sofa::helper::join(listComponents, ',');
}
ss << "]" << msgendl;
return true;
}
sofa::simulation::Node::SPtr SceneLoaderPY3::doLoad(const std::string& filename, const std::vector<std::string>& sceneArgs)
{
if (sofa::simulation::Simulation* simulation = sofa::simulation::getSimulation())
{
sofa::simulation::Node::SPtr root = simulation->createNewNode("root");
loadSceneWithArguments(filename.c_str(), sceneArgs, root);
return root;
}
return nullptr;
}
void SceneLoaderPY3::loadSceneWithArguments(const char *filename,
const std::vector<std::string>& arguments,
Node::SPtr root_out)
{
PythonEnvironment::setArguments(SetDirectory::GetFileName(filename).c_str(), arguments);
PythonEnvironment::gil lock;
try{
py::module::import("Sofa.Core");
py::object globals = py::module::import("__main__").attr("__dict__");
py::module module;
SetDirectory localDir(filename);
std::string basename = SetDirectory::GetFileNameWithoutExtension(SetDirectory::GetFileName(filename).c_str());
module = PythonEnvironment::importFromFile(basename, SetDirectory::GetFileName(filename), &globals);
if(!py::hasattr(module, "createScene"))
{
msg_error("SofaPython3") << "Missing createScene function";
return ;
}
py::object createScene = module.attr("createScene");
createScene( PythonFactory::toPython(root_out.get()) );
root_out->setInstanciationSourceFileName(filename);
root_out->setInstanciationSourceFilePos(0);
}catch(py::error_already_set& e)
{
std::stringstream ss;
ss << "Unable to completely load the scene from file '"<< filename << "'." << msgendl
<< "Python exception: " << msgendl
<< " " << e.what();
if( py::isinstance(e.type(), py::eval("type(DeprecationWarning)")) && !py::isinstance(e.type(), py::eval("type(AttributeError)")))
{
msg_deprecated() << ss.str();
}else
{
msg_error() << ss.str();
}
}
}
} // namespace sofapython3