Skip to content

Commit d31550f

Browse files
committed
[src] Add method to repair Scene Graph loading when some node are different. Will reload all Node found, remove unity GameObject not anymore mapped and add new DAGNode if needed
1 parent fd6ca03 commit d31550f

2 files changed

Lines changed: 101 additions & 28 deletions

File tree

Core/Scripts/Core/DAGNode/SofaDAGNodeManager.cs

Lines changed: 100 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,12 @@ public void ReconnectNodeGraph()
173173
AddDAGNode(dagNode);
174174

175175
// Find all other DAGNode
176-
SofaDAGNode[] nodes = ScenesManager.FindObjectsOfType<SofaDAGNode>();
176+
SofaDAGNode[] nodes = GameObject.FindObjectsByType<SofaDAGNode>(FindObjectsSortMode.None);
177177

178178
if (nbrNode != nodes.Length)
179179
{
180-
Debug.LogError("SofaDAGNodeManager Error while reconnecting the graph: " + nodes.Length + " DAGNode found in Unity instead of : " + nbrNode + " from SOFA side.");
181-
string NodeToFound = "";
182-
for (int i = 0; i < nbrNode; i++)
183-
NodeToFound = NodeToFound + m_sofaContextAPI.getDAGNodeName(i) + ",";
184-
Debug.LogError("Node to be found: " + NodeToFound);
185-
186-
string NodeFound = "";
187-
for (int i = 0; i < nodes.Length; i++)
188-
NodeFound = NodeFound + nodes[i].UniqueNameId + ",";
189-
Debug.LogError("Node found: " + NodeFound);
180+
Debug.LogWarning("SofaDAGNodeManager Error while reconnecting the graph: " + nodes.Length + " DAGNode found in Unity instead of : " + nbrNode + " from SOFA side.");
181+
RepairDAGNodeGraph();
190182
return;
191183
}
192184

@@ -199,6 +191,38 @@ public void ReconnectNodeGraph()
199191
}
200192

201193

194+
/// Method to clear a existing Node hierarchy
195+
public void ClearManager()
196+
{
197+
for (int i = 0; i < m_dagNodes.Count; i++)
198+
{
199+
SofaDAGNode node = m_dagNodes[i];
200+
node.DestroyDAGNode(true);
201+
node = null;
202+
}
203+
m_dagNodes.Clear();
204+
205+
// check if some DAGNodes were not saved in the list and have to be destroyed
206+
SofaDAGNode[] nodes = GameObject.FindObjectsByType<SofaDAGNode>(FindObjectsSortMode.None);
207+
208+
for (int i = 0; i < nodes.Length; i++)
209+
{
210+
211+
SofaDAGNode node = nodes[i];
212+
if (node == null)
213+
{
214+
Debug.LogWarning("Tryng to Destroy node already destroyed: " + i);
215+
continue;
216+
}
217+
218+
node.DestroyDAGNode(true);
219+
node = null;
220+
}
221+
m_dagNodes.Clear();
222+
// copy back the root node pointer
223+
}
224+
225+
202226
/// Method to propagate dirty value to all DAGNode of the graph.
203227
public void PropagateSetDirty(bool value)
204228
{
@@ -333,22 +357,7 @@ protected void FindDAGNodeGameObject(Transform parentTransform)
333357
}
334358
}
335359

336-
337-
/// Internal Method to clear a previous Node hierarchy
338-
protected void ClearManager()
339-
{
340-
for (int i=0; i<m_dagNodes.Count; i++)
341-
{
342-
SofaDAGNode node = m_dagNodes[i];
343-
node.DestroyDAGNode(true);
344-
node = null;
345-
}
346-
m_dagNodes.Clear();
347-
348-
// copy back the root node pointer
349-
}
350-
351-
360+
352361
/// Method to refresh the full DAGNode graph. Will ask the number of DAGNode on the Sofa side and compare to what is stored.
353362
protected void RefreshDAGNodeGraph()
354363
{
@@ -425,5 +434,69 @@ protected void AddDAGNode(SofaDAGNode dagNode)
425434
if (!found)
426435
m_dagNodes.Add(dagNode);
427436
}
437+
438+
439+
protected void RepairDAGNodeGraph()
440+
{
441+
SofaDAGNode[] unityDAGNodes = GameObject.FindObjectsByType<SofaDAGNode>(FindObjectsSortMode.None);
442+
// Store existing node in a map for easier search
443+
var unityNodeMap = new Dictionary<string, SofaDAGNode>();
444+
for (int i = 0; i < unityDAGNodes.Length; i++)
445+
{
446+
unityNodeMap[unityDAGNodes[i].UniqueNameId] = unityDAGNodes[i];
447+
}
448+
449+
// Reconnect reloaded SOFA Node
450+
int nbrSofaNode = m_sofaContextAPI.getNbrDAGNode();
451+
for (int i = 0; i < nbrSofaNode; i++)
452+
{
453+
string uniqId = m_sofaContextAPI.getDAGNodeName(i);
454+
if (unityNodeMap.ContainsKey(uniqId)) // if found, reconnect it and remove from map
455+
{
456+
AddDAGNode(unityNodeMap[uniqId]);
457+
unityNodeMap[uniqId].Reconnect(m_sofaContext);
458+
459+
unityNodeMap.Remove(uniqId);
460+
}
461+
else // if not found, this is a new DAGNode, we add it to the hierarchy
462+
{
463+
Debug.LogWarning("SOFA Node not found in current Unity Graph. Will add it: " + uniqId);
464+
465+
//string NodeName = m_sofaContextAPI.getDAGNodeName(i);
466+
string nodeDisplayName = m_sofaContextAPI.getDAGNodeDisplayName(i);
467+
468+
GameObject nodeGO = new GameObject("SofaNode - " + nodeDisplayName);
469+
SofaDAGNode dagNode = nodeGO.AddComponent<SofaDAGNode>();
470+
dagNode.Create(m_sofaContext, uniqId, nodeDisplayName);
471+
AddDAGNode(dagNode);
472+
473+
// temporary child of sofaContext until reordering ndoes
474+
nodeGO.transform.parent = m_sofaContext.gameObject.transform;
475+
476+
string parentName = dagNode.ParentNodeName;
477+
478+
if (parentName == "root") // under root node
479+
continue;
480+
481+
// search for parent (no optimisation needed here)
482+
foreach (SofaDAGNode snodeP in m_dagNodes)
483+
{
484+
if (snodeP.UniqueNameId == parentName)
485+
{
486+
dagNode.gameObject.transform.parent = snodeP.gameObject.transform;
487+
break;
488+
}
489+
}
490+
491+
}
492+
}
493+
494+
// Check if some Node are still in the map. They are no longer mapped to a SOFA node. Remove them
495+
foreach (KeyValuePair<string, SofaDAGNode> entry in unityNodeMap)
496+
{
497+
Debug.LogWarning("Unity DAG Node not reconnected. Will remove it: " + entry.Key);
498+
entry.Value.DestroyDAGNode(true);
499+
}
500+
}
428501
}
429502
}

Core/Scripts/SofaContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ public void ClearSofaScene()
651651
{
652652
m_impl.stop();
653653
m_impl.unload();
654-
m_nodeGraphMgr.LoadNodeGraph();
654+
m_nodeGraphMgr.ClearManager();
655655
m_impl.start();
656656
}
657657

0 commit comments

Comments
 (0)