private static void ClearWorkItemResult(HEU_TOPNodeData topNode, HEU_TOPWorkResult result)
	{
	    if (result != null)
	    {
		DestroyWorkItemResultData(topNode, result);

		topNode._workResults.Remove(result);
	    }
	}
	private static HEU_TOPWorkResult GetWorkResultByID(HEU_TOPNodeData topNode, HAPI_PDG_WorkitemId workItemID)
	{
	    HEU_TOPWorkResult result = null;
	    foreach (HEU_TOPWorkResult res in topNode._workResults)
	    {
		if (res._workItemID == workItemID)
		{
		    result = res;
		    break;
		}
	    }
	    return result;
	}
		private static HEU_TOPWorkResult GetWorkResultByIndex(HEU_TOPNodeData topNode, int workItemIndex)
		{
			HEU_TOPWorkResult result = null;
			foreach (HEU_TOPWorkResult res in topNode._workResults)
			{
				if (res._workItemIndex == workItemIndex)
				{
					result = res;
					break;
				}
			}
			return result;
		}
		private static void DestroyWorkItemResultData(HEU_TOPNodeData topNode, HEU_TOPWorkResult result)
		{
			if (result._generatedGOs != null)
			{
				int numGOs = result._generatedGOs.Count;
				for (int i = 0; i < numGOs; ++i)
				{
					HEU_GeoSync geoSync = result._generatedGOs[i].GetComponent<HEU_GeoSync>();
					if (geoSync != null)
					{
						geoSync.Unload();
					}

					//Debug.LogFormat("Destroy result: " + result._generatedGOs[i].name);
					HEU_GeneralUtility.DestroyImmediate(result._generatedGOs[i]);
					result._generatedGOs[i] = null;
				}

				result._generatedGOs.Clear();
			}
		}
	/// <summary>
	/// Load the geometry generated as results of the given work item, of the given TOP node.
	/// The load will be done asynchronously.
	/// Results must be tagged with 'file', and must have a file path, otherwise will not be loaded.
	/// </summary>
	/// <param name="session">Houdini Engine session that the TOP node is in</param>
	/// <param name="topNode">TOP node that the work item belongs to</param>
	/// <param name="workItemInfo">Work item whose results to load</param>
	/// <param name="resultInfos">Results data</param>
	/// <param name="workItemID">The work item's ID. Required for clearning its results.</param>
	public void LoadResults(HEU_SessionBase session, HEU_TOPNodeData topNode, HAPI_PDG_WorkitemInfo workItemInfo, HAPI_PDG_WorkitemResultInfo[] resultInfos, HAPI_PDG_WorkitemId workItemID)
	{
	    // Create HEU_GeoSync objects, set results, and sync it

	    string workItemName = HEU_SessionManager.GetString(workItemInfo.nameSH, session);
	    //Debug.LogFormat("Work item: {0}:: name={1}, results={2}", workItemInfo.index, workItemName, workItemInfo.numResults);

	    // Clear previously generated result
	    ClearWorkItemResultByID(topNode, workItemID);

	    if (resultInfos == null || resultInfos.Length == 0)
	    {
		return;
	    }

	    HEU_TOPWorkResult result = GetWorkResultByID(topNode, workItemID);
	    if (result == null)
	    {
		result = new HEU_TOPWorkResult();
		result._workItemIndex = workItemInfo.index;
		result._workItemID = workItemID;

		topNode._workResults.Add(result);
	    }

	    // Load each result geometry
	    int numResults = resultInfos.Length;
	    for (int i = 0; i < numResults; ++i)
	    {
		if (resultInfos[i].resultTagSH <= 0 || resultInfos[i].resultSH <= 0)
		{
		    continue;
		}

		string tag = HEU_SessionManager.GetString(resultInfos[i].resultTagSH, session);
		string path = HEU_SessionManager.GetString(resultInfos[i].resultSH, session);


		//Debug.LogFormat("Result for work item {0}: result={1}, tag={2}, path={3}", result._workItemIndex, i, tag, path);

		if (string.IsNullOrEmpty(tag) || !tag.StartsWith("file"))
		{
		    continue;
		}

		string name = string.Format("{0}_{1}_{2}",
			topNode._parentName,
			workItemName,
			workItemInfo.index);

		// Get or create parent GO
		if (topNode._workResultParentGO == null)
		{
		    topNode._workResultParentGO = new GameObject(topNode._nodeName);
		    HEU_GeneralUtility.SetParentWithCleanTransform(GetLoadRootTransform(), topNode._workResultParentGO.transform);
		    topNode._workResultParentGO.SetActive(topNode._showResults);
		}

		GameObject newOrExistingGO = null;
		int existingObjectIndex = -1;
		
		for (int j = 0; j < result._generatedGOs.Count; j++)
		{
			if (result._generatedGOs[j] != null)
			{
				HEU_GeoSync oldGeoSync = result._generatedGOs[j].GetComponent<HEU_GeoSync>();
				if (oldGeoSync != null && oldGeoSync._filePath == path)
				{
					oldGeoSync.Reset();
					existingObjectIndex = j;
					newOrExistingGO = result._generatedGOs[j];
					break;
				}
			}
		}

		if (existingObjectIndex < 0)
		{
			newOrExistingGO = new GameObject(name);
			result._generatedGOs.Add(newOrExistingGO);
		}


		HEU_GeneralUtility.SetParentWithCleanTransform(topNode._workResultParentGO.transform, newOrExistingGO.transform);

		// HEU_GeoSync does the loading
		HEU_GeoSync geoSync = newOrExistingGO.GetComponent<HEU_GeoSync>();

		if (geoSync == null)
		{
			geoSync = newOrExistingGO.AddComponent<HEU_GeoSync>();
		}

		geoSync._filePath = path;
		geoSync.SetOutputCacheDirectory(_outputCachePathRoot);
		geoSync.StartSync();
	    }
	}
	public static void ClearWorkItemResultByID(HEU_TOPNodeData topNode, HAPI_PDG_WorkitemId workItemID)
	{
	    HEU_TOPWorkResult result = GetWorkResultByID(topNode, workItemID);
	    ClearWorkItemResult(topNode, result);
	}
		public static void ClearWorkItemResultByIndex(HEU_TOPNodeData topNode, int workItemIndex)
		{
			HEU_TOPWorkResult result = GetWorkResultByIndex(topNode, workItemIndex);
			ClearWorkItemResult(topNode, result);
		}