Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMake/SofaPython3Tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ function(SP3_add_python_module)

find_package(pybind11 CONFIG QUIET REQUIRED)

# Ensure FindPython created Python::Python (needed by python_add_library)
if(NOT TARGET Python::Python)
find_package(Python REQUIRED COMPONENTS Interpreter Development Development.Module Development.Embed)
endif()


# We are doing manually what's usually done with pybind11_add_module(${A_TARGET} SHARED "${A_SOURCES}")
# since we got some problems on MacOS using recent versions of pybind11 where the SHARED argument wasn't taken
# into account
Expand Down
20 changes: 17 additions & 3 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_BaseData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ std::unique_ptr<DataContainerContext> writeableArray(BaseData* self)
return nullptr;
}

void __setattr__(py::object self, const std::string& s, py::object value)
py::object getValue(py::object self)
{
return PythonFactory::valueToPython_ro(py::cast<BaseData*>(self));
}

void setValue(py::object self, py::object value)
{
SOFA_UNUSED(s);
BaseData* selfdata = py::cast<BaseData*>(self);

if(py::isinstance<DataContainer>(value))
Expand All @@ -132,14 +136,20 @@ void __setattr__(py::object self, const std::string& s, py::object value)
BindingBase::SetAttr(py::cast(selfdata->getOwner()),selfdata->getName(),value);
}

void __setattr__(py::object self, const std::string& s, py::object value)
{
SOFA_UNUSED(s);
setValue(self, value);
}

py::object __getattr__(py::object self, const std::string& s)
{
/// If this is data.value we returns the content value of the data field converted into
/// a python object that is easy to manipulate. The conversion is done with the toPython
/// function.
if(s == "value")
{
return PythonFactory::valueToPython_ro(py::cast<BaseData*>(self));
return getValue(self);
}

if(s == "linkpath")
Expand All @@ -150,6 +160,8 @@ py::object __getattr__(py::object self, const std::string& s)
throw py::attribute_error("There is no attribute '"+s+"'");
}



void setParent(BaseData* self, BaseData* parent)
{
self->setParent(parent);
Expand Down Expand Up @@ -211,6 +223,8 @@ void moduleAddBaseData(py::module& m)
data.def("getName", [](BaseData& b){ return b.getName(); }, sofapython3::doc::baseData::getName);
data.def("setName", [](BaseData& b, const std::string& s){ b.setName(s); }, sofapython3::doc::baseData::setName);
data.def("getCounter", [](BaseData& self) { return self.getCounter(); }, sofapython3::doc::baseData::getCounter);
data.def("setValue", setValue);
data.def("getValue", getValue);
data.def("getHelp", &BaseData::getHelp, sofapython3::doc::baseData::getHelp);
data.def("unset", [](BaseData& b){ b.unset(); }, sofapython3::doc::baseData::unset);
data.def("getOwner", &getOwner, sofapython3::doc::baseData::getOwner);
Expand Down
44 changes: 44 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,47 @@ void setFieldsFromPythonValues(Base* self, const py::kwargs& dict)
}
}

class NumpyReprFixerRAII
{
public:
NumpyReprFixerRAII()
{
using namespace pybind11::literals;

m_numpy = py::module_::import("numpy");
const std::string version = py::cast<std::string>(m_numpy.attr("__version__"));
m_majorVersion = std::stoi(version.substr(0,1));
if ( m_majorVersion > 1)
{
m_setPO = m_numpy.attr("set_printoptions");
m_initialState = m_numpy.attr("get_printoptions")();
m_setPO("legacy"_a = "1.25");
}
}

~NumpyReprFixerRAII()
{
if ( m_majorVersion > 1)
{
m_setPO(**m_initialState);
}
}

private:
py::module_ m_numpy;
int m_majorVersion;
py::object m_setPO;
py::dict m_initialState;

};


/// Implement the addObject function.
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
{
//Instantiating this object will make sure the numpy representation is fixed during the call of this function, and comes back to its previous state after
[[maybe_unused]] const NumpyReprFixerRAII numpyReprFixer;

std::string name {};
if (kwargs.contains("name"))
{
Expand Down Expand Up @@ -291,6 +329,8 @@ py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs
if(d)
d->setPersistent(true);
}


return PythonFactory::toPython(object.get());
}

Expand Down Expand Up @@ -360,6 +400,9 @@ py::object createObject(Node* self, const std::string& type, const py::kwargs& k

py::object addChildKwargs(Node* self, const std::string& name, const py::kwargs& kwargs)
{
//Instantiating this object will make sure the numpy representation is fixed during the call of this function, and comes back to its previous state after
[[maybe_unused]] const NumpyReprFixerRAII numpyReprFixer;

if (sofapython3::isProtectedKeyword(name))
throw py::value_error("addChild: Cannot call addChild with name " + name + ": Protected keyword");
BaseObjectDescription desc (name.c_str());
Expand All @@ -378,6 +421,7 @@ py::object addChildKwargs(Node* self, const std::string& name, const py::kwargs&
d->setPersistent(true);
}


return py::cast(node);
}

Expand Down
15 changes: 6 additions & 9 deletions examples/basic-addGUI.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Required import for python
import Sofa


# Choose in your script to activate or not the GUI
USE_GUI = True

def main():
# Required import for python
import Sofa
import SofaRuntime
import Sofa.Gui
# Make sure to load all SOFA libraries

#Create the root node
root = Sofa.Core.Node("root")
Expand All @@ -20,12 +16,13 @@ def main():
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
import SofaQt
import Sofa.Gui
SofaRuntime.importPlugin("SofaImGui")

# Find out the supported GUIs
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
# Launch the GUI (qt or qglviewer)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
# Initialization of the scene will be done here
Expand Down
59 changes: 59 additions & 0 deletions examples/basic-useQtGUI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Choose in your script to activate or not the GUI
USE_GUI = True

def main():
# Required import for python
import Sofa
import SofaRuntime

# Make sure to load all SOFA libraries

#Create the root node
root = Sofa.Core.Node("root")
# Call the below 'createScene' function to create the scene graph
createScene(root)
Sofa.Simulation.initRoot(root)

if not USE_GUI:
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
import Sofa.Gui
import SofaQt

# Find out the supported GUIs
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
# Launch the GUI (qt or qglviewer)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
# Initialization of the scene will be done here
Sofa.Gui.GUIManager.MainLoop(root)
Sofa.Gui.GUIManager.closeGUI()
print("GUI was closed")

print("Simulation is done.")


# Function called when the scene graph is being created
def createScene(root):

root.addObject('RequiredPlugin', name='Sofa.Component.StateContainer')

# Scene must now include a AnimationLoop
root.addObject('DefaultAnimationLoop')

# Add new nodes and objects in the scene
node1 = root.addChild("Node1")
node2 = root.addChild("Node2")

node1.addObject("MechanicalObject", template="Rigid3d", position="0 0 0 0 0 0 1", showObject="1")

node2.addObject("MechanicalObject", template="Rigid3d", position="1 1 1 0 0 0 1", showObject="1")

return root


# Function used only if this script is called from a python environment
if __name__ == '__main__':
main()
12 changes: 10 additions & 2 deletions examples/emptyController.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def onKeypressedEvent(self, event):

if ord(key) == 20: # right
print("You pressed the Right key")

if key == 'M': # M key of the keyboard
print("You pressed the M key")


def onKeyreleasedEvent(self, event):
key = event['key']
Expand All @@ -54,6 +58,10 @@ def onKeyreleasedEvent(self, event):
if ord(key) == 20: # right
print("You released the Right key")

if key == 'M': # M key of the keyboard
print("You released the M key")


def onMouseEvent(self, event):
if (event['State']== 0): # mouse moving
print("Mouse is moving (x,y) = "+str(event['mouseX'])+" , "+str(event['mouseY']))
Expand Down Expand Up @@ -96,13 +104,13 @@ def createScene(root):
def main():
import SofaRuntime
import Sofa.Gui
import SofaQt
SofaRuntime.importPlugin("SofaImGui")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand Down
4 changes: 2 additions & 2 deletions examples/emptyDataEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def createScene(root):
def main():
import Sofa.Gui
import SofaRuntime
import SofaQt
SofaRuntime.importPlugin("SofaImGui")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand Down
4 changes: 2 additions & 2 deletions examples/emptyForceField.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def createScene(root):
def main():
import SofaRuntime
import Sofa.Gui
import SofaQt
SofaRuntime.importPlugin("SofaImGui")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand Down
4 changes: 2 additions & 2 deletions examples/example-forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def createScene(root):
def main():
import SofaRuntime
import Sofa.Gui
import SofaQt
SofaRuntime.importPlugin("SofaImGui")

root=Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand Down
8 changes: 4 additions & 4 deletions examples/liver-scriptcontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ def main():
Sofa.Simulation.initRoot(root)

if not USE_GUI:
import SofaQt

for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
SofaRuntime.importPlugin("SofaImGui")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand All @@ -43,12 +42,13 @@ def createScene(root):
'Sofa.Component.ODESolver.Backward',
'Sofa.Component.SolidMechanics.FEM.Elastic',
'Sofa.Component.StateContainer',
'Sofa.Component.MechanicalLoad',
'Sofa.Component.Topology.Container.Dynamic',
'Sofa.Component.Visual',
'Sofa.GL.Component.Rendering3D'
])

root.addObject('DefaultAnimationLoop')
root.addObject('DefaultAnimationLoop', computeBoundingBox=False)

root.addObject('VisualStyle', displayFlags="showCollisionModels hideVisualModels showForceFields")
root.addObject('CollisionPipeline', name="CollisionPipeline")
Expand Down
5 changes: 2 additions & 3 deletions examples/liver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def main():
for iteration in range(10):
Sofa.Simulation.animate(root, root.dt.value)
else:
import SofaQt

Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
SofaRuntime.importPlugin("SofaImGui")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
Sofa.Gui.GUIManager.MainLoop(root)
Expand Down
6 changes: 3 additions & 3 deletions examples/loadXMLfromPython.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ def createScene(root):
def main():
import SofaRuntime
import Sofa.Gui
import SofaQt

root = Sofa.Core.Node("root")
createScene(root)
Sofa.Simulation.initRoot(root)

# Find out the supported GUIs
print ("Supported GUIs are: " + Sofa.Gui.GUIManager.ListSupportedGUI(","))
# Launch the GUI (qt or qglviewer)
Sofa.Gui.GUIManager.Init("myscene", "qglviewer")
# Launch the GUI (imgui is now by default, to use Qt please refer to the example "basic-useQtGui.py")
SofaRuntime.importPlugin("SofaImGui")
Sofa.Gui.GUIManager.Init("myscene", "imgui")
Sofa.Gui.GUIManager.createGUI(root, __file__)
Sofa.Gui.GUIManager.SetDimension(1080, 1080)
# Initialization of the scene will be done here
Expand Down