[All] Introduce Snapshot feature#6168
Draft
Lucas-TJ wants to merge 85 commits into
Draft
Conversation
savesnapshot print name, type and value of a componant + unit test with a scene in order to test saveSnapshot
Implementation of BaseSnapShot, JSONSnapshot in order to be used in saveSnapshot
alxbilger
reviewed
Jul 2, 2026
alxbilger
reviewed
Jul 6, 2026
Comment on lines
+40
to
+41
| std::vector<std::string> m_recentSnapshotFiles; | ||
| std::map<std::string, std::shared_ptr<sofa::core::objectmodel::Snapshot>> m_recentSnapshots; |
Contributor
There was a problem hiding this comment.
if you only use static methods, I am not sure the non-static data members are used. So I would remove them.
Also, I am not sure about the design of the class. I don't think that this class manages anything. You seem to store all the data elsewhere.
Contributor
Author
There was a problem hiding this comment.
This class is here to store snapshots when I save in memory. I use it in SofaImGUI.
I will work on it again to make it clearer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is a Snapshot ?
A snapshot is an object that stores all information required to save the state of a simulation, so that it can be later restored.
Concept
General
A snapshot is a class structured like this :
Once the snapshot has been built, it can be exported to the format chosen by the user. For example, calling saveSnapshot with the JSON exporter serializes the snapshot into a JSON file. Loading works the same way in reverse: the snapshot is first imported from the chosen format, then used to restore the simulation state.
Step-by-step
Structure of a Snapshot
A Snapshot is organized as a graph. The root object, m_graphRoot, is the object that is exported to or imported from a file (or stored directly in memory). The graph is composed of nodes.
struct SnapshotNode. Each node contains a name, a data container, a link container, a list of components, and a list of child nodes.struct SnapshotObject. Each component contains a name, a data container, and a link container.For example,
{ "name" : "root", "data" : [{},{}], "links" : [{},{}], "components" : [ { "data" : [{},{}], "links" : [{},{}] }, { "data" : [{},{}], "links" : [{},{}] } ], "children": [] }Saving and loading snapshots rely on two visitors: SaveSnapshotVisitor and LoadSnapshotVisitor. These visitors traverse the scene graph to collect or restore data and links. Once collected, a snapshot can either be kept in memory or exported to a JSON file using SnapshotJSONExporter. The reverse process is used when loading a snapshot.
Save
Saving a snapshot relies on the SaveSnapshotVisitor, which traverses the scene graph. For each visited node or component, it calls:
Base::saveSnapshot(std::vector<std::shared_ptr<SnapshotNode>>& ).Inside
Base::saveSnapshot, the functionBase::createSnapshotObject(std::vector<std::shared_ptr<Snapshot::SnapshotNode>>&)creates either a SnapshotObject or a SnapshotNode, depending on the type of the current object.
he snapshot object is then populated with:
snapshotObject->m_name = this->getName()to store the object's name.saveInternalStateIn(*snapshotObject)to serialize the object's Internal State.Finally, the newly created SnapshotObject (or SnapshotNode) is inserted into the snapshot graph.
Load
Loading a snapshot relies on the LoadSnapshotVisitor, which traverses the scene graph and restores the saved state by calling:
Base::loadDataSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject)Base::loadLinkSnapshot(const std::shared_ptr<Snapshot::SnapshotObject>& snapshotObject)Base::loadInternalStateFrom(const Snapshot::SnapshotObject& snapshot)loadDataSnapshotuse :BaseData::read(const std::string& value)to restore data values from the snapshot.loadLinkSnapshotuse :BaseLink::readFromSnapshot(const std::string& value)to restore links from the snapshot.SnapshotManager
Overview
SnapshotManager is responsible for storing and managing snapshots, whether they are kept in memory or stored on disk.
API
Snapshots are stored in two separate containers:
m_snapshotsFromMemoryfor in-memory snapshots andm_snapshotsFromFilesfor snapshots stored on disk. TheaddSnapshotFromMemoryandaddSnapshotFromFilefunctions are used to add snapshots to these containers.Although in-memory and on-disk snapshots are handled separately, their APIs follow the same overall design.
doMemorySaveanddoMemoryLoadsave and restore snapshots in memory.doSaveToanddoLoadTosave and load snapshots from disk.doSaveToallows the output file format to be specified.The
doSaveTofunction also provides an isGroup parameter, and thedoLoadToGroupfunction is available for loading a collection of snapshots. These features are intended for saving and restoring groups of snapshots within a single file.SnapshotJSONExporter
Overview
SnapshotJSONExporter provides the functionality required to export a a snapshot to JSON and import it back from a JSON file. It relies on the nlohmann/json library.
API
The implementation is split in two parts : one for exporting snapshots and one for importing them.
For exporting, the
exportToJSONfunction serializes a Snapshot into a JSON object by calling the appropriateto_jsonoverloads before writing the result to a file.For importing, the
importFromfunction reads a JSON file and reconstructs a Snapshot by calling the correspondingfrom_jsonoverloads.Two additional helper functions,
fileToStringandsnapshotToString, provide serialization and deserialization to and from strings.By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).
Reviewers will merge this pull-request only if