Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4867b24
bindings snapshot + python scripts
Lucas-TJ May 12, 2026
3c1aaf2
clean
Lucas-TJ May 12, 2026
50f1e12
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ May 12, 2026
7e8bd21
clean again
Lucas-TJ May 12, 2026
2818bfc
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ May 21, 2026
7ef803d
update
Lucas-TJ May 21, 2026
9aa3385
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Jun 4, 2026
fab687f
update 09/06
Lucas-TJ Jun 9, 2026
4fc835d
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Jun 22, 2026
211fec8
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Jul 10, 2026
d726df8
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Jul 15, 2026
f02e415
Restore file
Lucas-TJ Jul 15, 2026
0199142
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Jul 15, 2026
f15bd5a
cleaning
Lucas-TJ Jul 15, 2026
ff35a26
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Sep 22, 2026
ca7fbdd
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Sep 25, 2026
38cd89b
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Oct 2, 2026
395f0ae
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Oct 29, 2026
79b3d23
Add binding for the class Snapshot
Lucas-TJ Nov 4, 2026
eb27d0e
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Nov 10, 2026
d2f5bf9
Merge branch 'sofa-framework:master' into snapshot_bindings
Lucas-TJ Nov 18, 2026
b628eaa
bind Snapshot class and update executeLoadSnapshotVisitor and execute…
Lucas-TJ Nov 18, 2026
fc5b824
Add functions to MemorySave and MemoryLoad (from SnapshotManager)
Lucas-TJ Nov 18, 2026
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
48 changes: 48 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/// Neede to have automatic conversion from pybind types to stl container.
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <iostream>

#include <sofa/simulation/Simulation.h>
#include <sofa/simulation/mechanicalvisitor/MechanicalComputeEnergyVisitor.h>
Expand Down Expand Up @@ -68,6 +69,17 @@ using sofapython3::PythonEnvironment;

using sofa::core::objectmodel::BaseObjectDescription;

#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>

#include <sofa/simulation/SaveSnapshotVisitor.h>
using sofa::simulation::SaveSnapshotVisitor;

#include <sofa/simulation/LoadSnapshotVisitor.h>
using sofa::simulation::LoadSnapshotVisitor;

#include <sofa/simulation/SnapshotManager.h>
using sofa::simulation::SnapshotManager;

#include <queue>
#include <sofa/core/objectmodel/Link.h>

Expand Down Expand Up @@ -660,6 +672,38 @@ void sendEvent(Node* self, py::object pyUserData, char* eventName)
self->propagateEvent(sofa::core::execparams::defaultInstance(), &event);
}

void executeSaveSnapshotVisitor(Node* self, Snapshot& snapshot)
{
auto visitor = SaveSnapshotVisitor(nullptr,snapshot);
self->execute(visitor);
}

void executeLoadSnapshotVisitor(Node* self, Snapshot& snapshot)
{
auto visitor = LoadSnapshotVisitor(nullptr,snapshot);
self->execute(visitor);
}

void executeMemorySave(Node* self, BaseComponent& object)
{
auto* manager = dynamic_cast<SnapshotManager*>(&object);

if (manager)
manager->memorySave();
else
throw py::type_error("Expected a SnapshotManager");
}

void executeMemoryLoad(BaseComponent& object)
{
auto* manager = dynamic_cast<SnapshotManager*>(&object);

if (manager)
manager->memoryLoad();
else
throw py::type_error("Expected a SnapshotManager");
}

py::object computeEnergy(Node* self)
{
sofa::simulation::mechanicalvisitor::MechanicalComputeEnergyVisitor energyVisitor(sofa::core::mechanicalparams::defaultInstance());
Expand Down Expand Up @@ -725,6 +769,10 @@ void moduleAddNode(py::module &m) {
p.def("getMechanicalMapping", &getMechanicalMapping, sofapython3::doc::sofa::core::Node::getMechanicalMapping);
p.def("sendEvent", &sendEvent, sofapython3::doc::sofa::core::Node::sendEvent);
p.def("computeEnergy", &computeEnergy, sofapython3::doc::sofa::core::Node::computeEnergy);
p.def("executeSaveSnapshotVisitor", &executeSaveSnapshotVisitor);
p.def("executeLoadSnapshotVisitor", &executeLoadSnapshotVisitor);
p.def("executeMemorySave", &executeMemorySave);
p.def("executeMemoryLoad", &executeMemoryLoad);

p.def("__enter__", [](py::object self)
{
Expand Down
103 changes: 103 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
* *
* 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: contact@sofa-framework.org *
******************************************************************************/

#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl_bind.h>
#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>
#include <SofaPython3/PythonFactory.h>

using sofa::core::objectmodel::Snapshot;

namespace py { using namespace pybind11; }
PYBIND11_MAKE_OPAQUE(std::vector<sofa::core::objectmodel::Snapshot::DataInfo>);
PYBIND11_MAKE_OPAQUE(std::vector<sofa::core::objectmodel::Snapshot::LinkInfo>);
PYBIND11_MAKE_OPAQUE(std::vector<std::shared_ptr<sofa::core::objectmodel::Snapshot::SnapshotObject>>);
PYBIND11_MAKE_OPAQUE(std::vector<std::shared_ptr<sofa::core::objectmodel::Snapshot::SnapshotNode>>);

namespace sofapython3 {

void moduleAddSnapshot(py::module &m)
{
py::bind_vector<std::vector<Snapshot::DataInfo>>(m, "SnapshotDataInfoVector");
py::bind_vector<std::vector<Snapshot::LinkInfo>>(m, "SnapshotLinkInfoVector");
py::bind_vector<std::vector<std::shared_ptr<Snapshot::SnapshotObject>>>(m, "SnapshotObjectVector");
py::bind_vector<std::vector<std::shared_ptr<Snapshot::SnapshotNode>>>(m, "SnapshotNodeVector");

py::class_<Snapshot> snapshot(m, "Snapshot");

snapshot.def(py::init<>())
.def_readwrite("graphRoot", &Snapshot::m_graphRoot);

py::class_<Snapshot::DataInfo>(snapshot, "DataInfo")
.def(py::init<>())
.def_readwrite("name", &Snapshot::DataInfo::m_name)
.def_readwrite("type", &Snapshot::DataInfo::m_type)
.def_readwrite("value", &Snapshot::DataInfo::m_value);

py::class_<Snapshot::LinkInfo>(snapshot, "LinkInfo")
.def(py::init<>())
.def_readwrite("name", &Snapshot::LinkInfo::m_name)
.def_readwrite("type", &Snapshot::LinkInfo::m_type)
.def_readwrite("value", &Snapshot::LinkInfo::m_value);

py::class_<Snapshot::SnapshotObject, std::shared_ptr<Snapshot::SnapshotObject>>(snapshot, "SnapshotObject")
.def(py::init<>())
.def(py::init<std::string>())
.def_readwrite("name", &Snapshot::SnapshotObject::m_name)
.def_readwrite("className", &Snapshot::SnapshotObject::m_className)
.def_readwrite("pathName", &Snapshot::SnapshotObject::m_pathName)

.def_property("dataContainer",
[](Snapshot::SnapshotObject &self) -> std::vector<Snapshot::DataInfo>& { return self.m_dataContainer; },
[](Snapshot::SnapshotObject &self, const std::vector<Snapshot::DataInfo> &v) { self.m_dataContainer = v; },
py::return_value_policy::reference_internal)

.def_property("linkContainer",
[](Snapshot::SnapshotObject &self) -> std::vector<Snapshot::LinkInfo>& { return self.m_linkContainer; },
[](Snapshot::SnapshotObject &self, const std::vector<Snapshot::LinkInfo> &v) { self.m_linkContainer = v; },
py::return_value_policy::reference_internal)

.def_property("objects",
[](Snapshot::SnapshotObject &self) -> std::vector<std::shared_ptr<Snapshot::SnapshotObject>>& { return self.m_objects; },
[](Snapshot::SnapshotObject &self, const std::vector<std::shared_ptr<Snapshot::SnapshotObject>> &v) { self.m_objects = v; },
py::return_value_policy::reference_internal)

.def("clear", &Snapshot::SnapshotObject::clear)

.def("push_back", py::overload_cast<const Snapshot::DataInfo&>(&Snapshot::SnapshotObject::push_back), py::return_value_policy::reference_internal)
.def("push_back", py::overload_cast<const Snapshot::LinkInfo&>(&Snapshot::SnapshotObject::push_back), py::return_value_policy::reference_internal)
.def("push_back", py::overload_cast<const std::shared_ptr<Snapshot::SnapshotObject>&>(&Snapshot::SnapshotObject::push_back), py::return_value_policy::reference_internal);

py::class_<Snapshot::SnapshotNode, Snapshot::SnapshotObject, std::shared_ptr<Snapshot::SnapshotNode>>(snapshot, "SnapshotNode")
.def(py::init<>())
.def(py::init<const std::string&>())
.def(py::init<const Snapshot::SnapshotObject&>())

.def_property("children",
[](Snapshot::SnapshotNode &self) -> std::vector<std::shared_ptr<Snapshot::SnapshotNode>>& { return self.m_children; },
[](Snapshot::SnapshotNode &self, const std::vector<std::shared_ptr<Snapshot::SnapshotNode>> &v) { self.m_children = v; },
py::return_value_policy::reference_internal)

.def("clear", &Snapshot::SnapshotNode::clear)
.def("push_back", py::overload_cast<const std::shared_ptr<Snapshot::SnapshotNode>&>(&Snapshot::SnapshotNode::push_back), py::return_value_policy::reference_internal);
}

}
32 changes: 32 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Snapshot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2021 INRIA, USTL, UJF, CNRS, MGH *
* *
* 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: contact@sofa-framework.org *
******************************************************************************/

#pragma once

#include <pybind11/pybind11.h>
#include <sofa/core/objectmodel/Snapshot.h>
using sofa::core::objectmodel::Snapshot;

namespace sofapython3 {

void moduleAddSnapshot(pybind11::module &m);

} /// namespace sofapython3

2 changes: 2 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseCamera_doc.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Topology.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Snapshot.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler_doc.h
${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.h
Expand Down Expand Up @@ -89,6 +90,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseLink.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Topology.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_BaseMeshTopology.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_Snapshot.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_TaskScheduler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Binding_VisualParams.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Submodule_Core.cpp
Expand Down
2 changes: 2 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Submodule_Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using sofa::helper::logging::Message;
#include <SofaPython3/Sofa/Core/Binding_NodeIterator.h>
#include <SofaPython3/Sofa/Core/Binding_Prefab.h>
#include <SofaPython3/Sofa/Core/Binding_BaseLink.h>
#include <SofaPython3/Sofa/Core/Binding_Snapshot.h>
#include <SofaPython3/Sofa/Core/Binding_PointSetTopologyModifier.h>
#include <SofaPython3/Sofa/Core/Binding_PythonScriptEvent.h>
#include <SofaPython3/Sofa/Core/Binding_Topology.h>
Expand Down Expand Up @@ -157,6 +158,7 @@ PYBIND11_MODULE(Core, core)
moduleAddBaseMeshTopology(core);
moduleAddPointSetTopologyModifier(core);
moduleAddTaskScheduler(core);
moduleAddSnapshot(core);
moduleAddVisualParams(core);

// called when the module is unloaded
Expand Down
Loading