Skip to content
Snippets Groups Projects
Commit 0ab2e3c9 authored by Mathias Paulin's avatar Mathias Paulin :speech_balloon:
Browse files

[nodeGraph] add saveToJson.

parent e2c4fbc5
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,16 @@ std::string Node::getUuid() const { ...@@ -33,7 +33,16 @@ std::string Node::getUuid() const {
return struuid; return struuid;
} }
bool Node::setUuid(const std::string& uid) {
if ( m_uuid.is_nil() ) {
auto id = uuids::uuid::from_string(uid);
if ( id ) {
m_uuid = id.value();
return true;
}
}
return false;
}
Node::Node( const std::string& instanceName, const std::string& typeName ) : Node::Node( const std::string& instanceName, const std::string& typeName ) :
m_typeName{ typeName }, m_instanceName{ instanceName } {} m_typeName{ typeName }, m_instanceName{ instanceName } {}
......
...@@ -118,6 +118,10 @@ class NodeBasedRenderer_LIBRARY_API Node ...@@ -118,6 +118,10 @@ class NodeBasedRenderer_LIBRARY_API Node
/// Gets the UUID of the node as a string /// Gets the UUID of the node as a string
std::string getUuid() const; std::string getUuid() const;
/// Sets the UUID of the node from a valid string string
/// @return true if the uuid is set, false if the node already have a valid uid
bool setUuid(const std::string& uid);
/// return the deletable status of the node /// return the deletable status of the node
/// Default is true. If a node must not be deleted (e.g. RenderObjectSource Node in the rendergraph), override this method. /// Default is true. If a node must not be deleted (e.g. RenderObjectSource Node in the rendergraph), override this method.
bool isDeletable() { return m_isDeletable;} bool isDeletable() { return m_isDeletable;}
......
...@@ -115,13 +115,25 @@ void RenderGraph::loadFromJson( const std::string& jsonFilePath ) { ...@@ -115,13 +115,25 @@ void RenderGraph::loadFromJson( const std::string& jsonFilePath ) {
std::string name = n["model"]["name"]; std::string name = n["model"]["name"];
std::string id = n["id"]; std::string id = n["id"];
if ( name == "RenderObjects" ) if ( name == "RenderObjects" )
{ nodeById.emplace( id, getDataNode<NodeTypeRenderObject>() ); } {
getDataNode<NodeTypeRenderObject>()->fromJson( n );
nodeById.emplace( id, getDataNode<NodeTypeRenderObject>() );
}
else if ( name == "Cameras" ) else if ( name == "Cameras" )
{ nodeById.emplace( id, getDataNode<NodeTypeCamera>() ); } {
getDataNode<NodeTypeCamera>()->fromJson( n );
nodeById.emplace( id, getDataNode<NodeTypeCamera>() );
}
else if ( name == "Lights" ) else if ( name == "Lights" )
{ nodeById.emplace( id, getDataNode<NodeTypeLight>() ); } {
getDataNode<NodeTypeLight>()->fromJson( n );
nodeById.emplace( id, getDataNode<NodeTypeLight>() );
}
else if ( name == "Display Sink" ) else if ( name == "Display Sink" )
{ nodeById.emplace( id, getDisplayNode() ); } {
getDisplayNode()->fromJson( n );
nodeById.emplace( id, getDisplayNode() );
}
else else
{ {
auto newNode = NodeFactory::createNode( name, n ); auto newNode = NodeFactory::createNode( name, n );
...@@ -196,6 +208,43 @@ void RenderGraph::loadFromJson( const std::string& jsonFilePath ) { ...@@ -196,6 +208,43 @@ void RenderGraph::loadFromJson( const std::string& jsonFilePath ) {
<< std::endl; << std::endl;
} }
} }
}
void RenderGraph::saveToJson( const std::string& jsonFilePath ) {
nlohmann::json nodes = nlohmann::json::array();
nlohmann::json connections = nlohmann::json::array();
for(const auto&n : m_nodes) {
nlohmann::json nodeData;
n->toJson(nodeData);
nodes.push_back(nodeData);
int numPort=0;
for( const auto & input : n->getInputs() ) {
if( input->isLinked() ) {
nlohmann::json link = nlohmann::json::object();
link["in_id"] = n->getUuid();
link["in_index"] = numPort;
auto portOut = input->getLink();
auto nodeOut = portOut->getNode();
int outPortIndex = 0;
for( const auto& p : nodeOut->getOutputs() ) {
if ( p.get() == portOut ) {
break;
}
outPortIndex++;
}
link["out_id"] = nodeOut->getUuid();
link["out_index"] = outPortIndex;
connections.push_back( link );
}
numPort++;
}
}
nlohmann::json graphJson;
graphJson["nodes"] = nodes;
graphJson["connections"] = connections;
std::ofstream file(jsonFilePath);
file << std::setw(4) << graphJson << std::endl;
} }
bool RenderGraph::addNode( Node* newNode ) { bool RenderGraph::addNode( Node* newNode ) {
......
...@@ -39,6 +39,10 @@ class NodeBasedRenderer_LIBRARY_API RenderGraph : public Node ...@@ -39,6 +39,10 @@ class NodeBasedRenderer_LIBRARY_API RenderGraph : public Node
/// @param jsonFilePath The path to the JSON file. /// @param jsonFilePath The path to the JSON file.
void loadFromJson( const std::string& jsonFilePath ); void loadFromJson( const std::string& jsonFilePath );
/// Saves nodes and links to a JSON file.
/// @param jsonFilePath The path to the JSON file.
void saveToJson( const std::string& jsonFilePath );
/// Adds a node to the render graph. /// Adds a node to the render graph.
/// @param newNode The node to add to the render graph. /// @param newNode The node to add to the render graph.
bool addNode( Node* newNode ); bool addNode( Node* newNode );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment