/// <summary> /// Copy the file of the given srcAsset into the given targetPath, which must be absolute. /// If targetPath doesn't have a file name, the srcAsset's file name will be used. /// </summary> /// <param name="srcAsset">Source asset to copy</param> /// <param name="targetPath">Absolute path of destination</param> /// <param name="type">Type of the asset</param> /// <returns></returns> public static Object CopyAndLoadAssetAtGivenPath(Object srcAsset, string targetPath, System.Type type) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath)) { string targetFolderPath = HEU_Platform.GetFolderPath(targetPath); CreatePathWithFolders(targetFolderPath); string targetFileName = HEU_Platform.GetFileName(targetPath); if (string.IsNullOrEmpty(targetFileName)) { HEU_Logger.LogErrorFormat("Copying asset failed. Destination path must end with a file name: {0}!", targetPath); return null; } if (CopyAsset(srcAssetPath, targetPath)) { // Refresh database as otherwise we won't be able to load it in the next line. SaveAndRefreshDatabase(); return LoadAssetAtPath(targetPath, type); } else { HEU_Logger.LogErrorFormat("Failed to copy and load asset from {0} to {1}!", srcAssetPath, targetPath); } } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Returns the path to the given asset, with subasset tagging if it is /// a subasset. Unity doesn't have a way to query subasset paths directly /// nor load them directly. Instead have to load the main asset first then /// traverse through all assets to find the subasset. /// </summary> /// <param name="asset">Asset to get path for</param> /// <returns>Path of given asset</returns> public static string GetAssetPathWithSubAssetSupport(Object asset) { #if UNITY_EDITOR string assetPath = AssetDatabase.GetAssetPath(asset); bool isSubAsset = HEU_AssetDatabase.IsSubAsset(asset); if (isSubAsset) { Object[] subAssets = HEU_AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath); int numSubAssets = subAssets.Length; for (int i = 0; i < numSubAssets; ++i) { if (subAssets[i] == asset) { assetPath = string.Format("{0}{1}/{2}", HEU_Defines.HEU_SUBASSET, assetPath, subAssets[i].name); break; } } } return assetPath; #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static string GetAssetRootPath(Object asset) { #if UNITY_EDITOR string assetPath = GetAssetPath(asset); if (!string.IsNullOrEmpty(assetPath)) { // We'll strip the path until we're under AssetCache/Baked/assetName or AssetCache/Working/assetName string assetTypePath = GetAssetBakedPath(); if (!assetPath.StartsWith(assetTypePath)) { assetTypePath = GetAssetWorkingPath(); if (!assetPath.StartsWith(assetTypePath)) { return null; } } string removedBakedPath = assetPath.Replace(assetTypePath + HEU_Platform.DirectorySeparator, ""); string[] splits = removedBakedPath.Split(HEU_Platform.DirectorySeparator); if (!string.IsNullOrEmpty(splits[0])) { string rootPath = HEU_Platform.BuildPath(assetTypePath, splits[0]); Debug.AssertFormat(AssetDatabase.IsValidFolder(rootPath), "Calculated root path {0} is invalid for asset at {1}.", rootPath, assetPath); return rootPath; } } return null; #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Creates all folders in the given path if they don't exist. /// </summary> /// <param name="inPath">The path to create folders for</param> public static void CreatePathWithFolders(string inPath) { #if UNITY_EDITOR string pathBuild = ""; string[] folders = inPath.Split(HEU_Platform.DirectorySeparator); foreach (string folder in folders) { if (string.IsNullOrEmpty(folder)) { break; } string nextPath = ""; if (string.IsNullOrEmpty(pathBuild)) { nextPath = folder; } else { nextPath = HEU_Platform.BuildPath(pathBuild, folder); } if (!AssetDatabase.IsValidFolder(nextPath)) { //HEU_Logger.LogFormat("{0}: Creating folder: {1}/{2}", HEU_Defines.HEU_NAME, pathBuild, folder); AssetDatabase.CreateFolder(pathBuild, folder); } pathBuild = nextPath; } #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); #endif }
/// <summary> /// Loads a copy of the srcAsset at copyPath. Creates a copy if not found. /// </summary> /// <param name="srcAsset">The source asset object</param> /// <param name="copyPath">The full path to the copy</param> /// <param name="type">The type of source asset</param> /// <param name="bOverwriteExisting">Whether to overwrite existing copy if found</param> /// <returns>Returns loaded copy if exists or created, otherwise null</returns> public static Object CopyAndLoadAssetAtAnyPath(Object srcAsset, string copyPath, System.Type type, bool bOverwriteExisting) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath)) { CreatePathWithFolders(copyPath); string fileName = HEU_Platform.GetFileName(srcAssetPath); string fullCopyPath = HEU_Platform.BuildPath(copyPath, fileName); if ((!bOverwriteExisting && HEU_Platform.DoesFileExist(fullCopyPath)) || CopyAsset(srcAssetPath, fullCopyPath)) { // Refresh database as otherwise we won't be able to load it in the next line. SaveAndRefreshDatabase(); return LoadAssetAtPath(fullCopyPath, type); } else { HEU_Logger.LogErrorFormat("Failed to copy and load asset from {0} to {1}!", srcAssetPath, fullCopyPath); } } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
// SESSION DEBUG ---------------------------------------------------------------------------------------------- /// <summary> /// Load a HIP file into given session. /// The user will be prompted to choose a HIP file via Unity's file dialog. /// </summary> /// <param name="bCookNodes">True if nodes should be cooked on load</param> /// <param name="session">Session to load into. If null, will use default session</param> /// <returns>True if successfully loaded the HIP file</returns> public static bool LoadSessionFromHIP(bool bCookNodes, HEU_SessionBase session = null) { if (session == null || !session.IsSessionValid()) { session = GetOrCreateDefaultSession(); if (session == null || !session.IsSessionValid()) { session.SetSessionErrorMsg("No valid session found. Unable to load session!", true); return false; } } #if UNITY_EDITOR string lastPath = HEU_PluginSettings.LastLoadHIPPath; string fileExt = "hip;*.hiplc;*.hipnc"; string filePath = UnityEditor.EditorUtility.OpenFilePanel("Open Houdini HIP", lastPath, fileExt); if (!string.IsNullOrEmpty(filePath)) { HEU_PluginSettings.LastLoadHIPPath = filePath; bool bResult = session.LoadHIPFile(filePath, bCookNodes); if (bResult) { // TODO HEU_Logger.LogWarning("This is currently not supported in the plugin!"); } } #else session.SetSessionErrorMsg("Load session only supported in Unity Editor!", true); #endif return false; }
public static void CreateAsset(Object asset, string path) { #if UNITY_EDITOR AssetDatabase.CreateAsset(asset, path); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); #endif }
public static void AddObjectToAsset(UnityEngine.Object objectToAdd, UnityEngine.Object assetObject) { #if UNITY_EDITOR AssetDatabase.AddObjectToAsset(objectToAdd, assetObject); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); #endif }
public static Object[] LoadAllAssetRepresentationsAtPath(string assetPath) { #if UNITY_EDITOR return AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static string GetAssetPath(Object asset) { #if UNITY_EDITOR return AssetDatabase.GetAssetPath(asset); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static Vector2[] GeneratePerTriangle(Mesh meshSrc) { #if UNITY_EDITOR return Unwrapping.GeneratePerTriangleUV(meshSrc); #else HEU_Logger.LogWarning("GeneratePerTriangle is unavailable at runtime!"); return null; #endif }
public static bool IsAssetInAssetCacheWorkingFolder(Object asset) { #if UNITY_EDITOR string assetPath = GetAssetPath(asset); return HEU_AssetDatabase.IsPathInAssetCacheWorkingFolder(assetPath); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return false; #endif }
public static string GetAssetOrScenePath(Object inputObject) { #if UNITY_EDITOR return AssetDatabase.GetAssetOrScenePath(inputObject); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static bool CopyAsset(string path, string newPath) { #if UNITY_EDITOR return AssetDatabase.CopyAsset(path, newPath); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return false; #endif }
/// <summary> /// Returns true if this gameobject has been saved in a scene. /// </summary> /// <returns>True if gameobject has been saved in a scene.</returns> public static bool IsAssetSavedInScene(GameObject go) { #if UNITY_EDITOR string scenePath = GetAssetOrScenePath(go); return !string.IsNullOrEmpty(scenePath); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return false; #endif }
public static string GetAssetBakedPathWithAssetName(string assetName) { #if UNITY_EDITOR return HEU_Platform.BuildPath(GetAssetBakedPath(), assetName); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Returns true if the material resides in the asset database. /// </summary> /// <param name="assetObject">The material object to check</param> /// <returns>True if the material resides in the asset database</returns> public static bool ContainsAsset(Object assetObject) { #if UNITY_EDITOR return AssetDatabase.Contains(assetObject); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return false; #endif }
/// <summary> /// Returns a unique path for the given path. /// </summary> /// <param name="path">The input path to find unique path for</param> /// <returns>A unique path for the given path.</returns> public static string GetUniqueAssetPath(string path) { #if UNITY_EDITOR return AssetDatabase.GenerateUniqueAssetPath(path); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static void GenerateSecondaryUVSet(Mesh meshsrc) { #if UNITY_EDITOR UnwrapParam param; UnwrapParam.SetDefaults(out param); Unwrapping.GenerateSecondaryUVSet(meshsrc, param); #else HEU_Logger.LogWarning("GenerateSecondaryUVSet is unavailable at runtime!"); #endif }
public static Shader FindPluginShader(string shaderName) { #if UNITY_EDITOR return FindShader(shaderName); #else // TODO RUNTIME: Shader.Find is not available in non-Editor mode, so need to figure out a replacement in runtime. HEU_Logger.LogWarning("Houdini Engine is unable to load shaders in non-Editor mode!"); return null; #endif }
public static Object[] LoadAllAssetsAtPath(string assetPath) { #if UNITY_EDITOR return AssetDatabase.LoadAllAssetsAtPath(assetPath); #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Delete the asset object. /// </summary> /// <param name="asset">The asset object to delete</param> public static void DeleteAssetAtPath(string path) { #if UNITY_EDITOR if (!string.IsNullOrEmpty(path)) { AssetDatabase.DeleteAsset(path); } #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); #endif }
public static void DeleteAssetIfInBakedFolder(Object asset) { #if UNITY_EDITOR string assetPath = GetAssetPath(asset); if (HEU_AssetDatabase.IsPathInAssetCacheBakedFolder(assetPath)) { AssetDatabase.DeleteAsset(assetPath); } #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); #endif }
/// <summary> /// Returns true if the given path is inside the Working/ subfolder /// of the plugin's asset cache /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool IsPathInAssetCacheWorkingFolder(string path) { #if UNITY_EDITOR if (path.StartsWith(Application.dataPath)) { path = GetAssetRelativePath(path); } string workingPath = GetAssetWorkingPath(); return path.StartsWith(workingPath); #else HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return false; #endif }
/// <summary> /// Loads a copy of the srcAsset at copyPath, which must reside in the asset cache. Creates a copy if not found. /// This does nothing if srcAsset resides outside the asset cache. /// </summary> /// <param name="srcAsset">The source asset object</param> /// <param name="copyPath">The full path to the copy</param> /// <param name="type">The type of source asset</param> /// <param name="bOverwriteExisting">Whether to overwrite existing copy if found</param> /// <returns>Returns loaded copy if exists or created, otherwise null</returns> public static Object CopyAndLoadAssetFromAssetCachePath(Object srcAsset, string copyPath, System.Type type, bool bOverwriteExisting) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath) && IsPathInAssetCache(srcAssetPath)) { return CopyAndLoadAssetAtAnyPath(srcAsset, copyPath, type, bOverwriteExisting); } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static string GetAssetCachePath() { #if UNITY_EDITOR string rootPath = HEU_Platform.BuildPath("Assets", HEU_PluginSettings.AssetCachePath); if (!AssetDatabase.IsValidFolder(rootPath)) { CreatePathWithFolders(rootPath); } return rootPath; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return ""; #endif }
public static void ExecuteTool(int toolSlot) { if (_currentSelectedShelf < 0 && _currentSelectedShelf >= _shelves.Count) { HEU_Logger.LogWarning("Invalid shelf selected. Unable to apply tool."); return; } if (toolSlot < 0 || toolSlot >= _shelves[_currentSelectedShelf]._tools.Count) { HEU_Logger.LogWarning("Invalid tool selected. Unable to apply tool."); return; } HEU_ShelfToolData toolData = _shelves[_currentSelectedShelf]._tools[toolSlot]; GameObject[] selectedObjects = HEU_EditorUtility.GetSelectedObjects(); string assetPath = toolData._assetPath; if (toolData._toolType == HEU_ShelfToolData.ToolType.GENERATOR) { Matrix4x4 targetMatrix = HEU_EditorUtility.GetSelectedObjectsMeanTransform(); Vector3 position = HEU_HAPIUtility.GetPosition(ref targetMatrix); Quaternion rotation = HEU_HAPIUtility.GetQuaternion(ref targetMatrix); Vector3 scale = HEU_HAPIUtility.GetScale(ref targetMatrix); scale = Vector3.one; ExecuteToolGenerator(toolData._name, assetPath, position, rotation, scale); } else if (selectedObjects.Length == 0) { ExecuteToolNoInput(toolData._name, assetPath); } else if (toolData._toolType == HEU_ShelfToolData.ToolType.OPERATOR_SINGLE) { ExecuteToolOperatorSingle(toolData._name, assetPath, selectedObjects); } else if (toolData._toolType == HEU_ShelfToolData.ToolType.OPERATOR_MULTI) { ExecuteToolOperatorMultiple(toolData._name, assetPath, selectedObjects); } else if (toolData._toolType == HEU_ShelfToolData.ToolType.BATCH) { ExecuteToolBatch(toolData._name, assetPath, selectedObjects); } }
public static string CreateUniqueBakePath(string assetName) { #if UNITY_EDITOR string assetBakedPath = GetAssetBakedPathWithAssetName(assetName); assetBakedPath = AssetDatabase.GenerateUniqueAssetPath(assetBakedPath); if (!HEU_Platform.DoesPathExist(assetBakedPath)) { CreatePathWithFolders(assetBakedPath); } return assetBakedPath; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static string GetAssetBakedPath() { #if UNITY_EDITOR string dbRoot = GetAssetCachePath(); string bakedPath = HEU_Platform.BuildPath(dbRoot, HEU_Defines.HEU_BAKED_PATH); if (!AssetDatabase.IsValidFolder(bakedPath)) { AssetDatabase.CreateFolder(dbRoot, HEU_Defines.HEU_BAKED_PATH); } return bakedPath; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Create a unique asset cache folder for the given asset path. /// The given asset path should be the HDA's path in the project. /// </summary> /// <param name="suggestedAssetPath">A suggested path to try. Will use default if empty or null./param> /// <returns>Unique asset cache folder for given asset path</returns> public static string CreateAssetCacheFolder(string suggestedAssetPath, int hash = 0) { #if UNITY_EDITOR // We create a unique folder inside our plugin's asset database cache folder. string assetDBPath = GetAssetCachePath(); string assetWorkingPath = HEU_Platform.BuildPath(assetDBPath, HEU_Defines.HEU_WORKING_PATH); if (!AssetDatabase.IsValidFolder(assetWorkingPath)) { AssetDatabase.CreateFolder(assetDBPath, HEU_Defines.HEU_WORKING_PATH); } string fileName = HEU_Platform.GetFileNameWithoutExtension(suggestedAssetPath); if (string.IsNullOrEmpty(fileName)) { fileName = "AssetCache"; HEU_Logger.LogWarningFormat("Unable to get file name from {0}. Using default value: {1}.", suggestedAssetPath, fileName); } if (HEU_PluginSettings.ShortenFolderPaths && fileName.Length >= 3 && hash != 0) { fileName = fileName.Substring(0, 3) + hash; } string fullPath = HEU_Platform.BuildPath(assetWorkingPath, fileName); // Gives us the unique folder path, which we then separate out to create this folder fullPath = AssetDatabase.GenerateUniqueAssetPath(fullPath); CreatePathWithFolders(fullPath); if (!AssetDatabase.IsValidFolder(fullPath)) { HEU_Logger.LogErrorFormat("Unable to create a valid asset cache folder: {0}! Check directory permission or that enough space is available!", fullPath); fullPath = null; } return fullPath; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? HEU_Logger.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }