예제 #1
0
        public static string ImportAsTimelineClip(Scene scene)
        {
            string path = scene.FilePath;

            var importOptions = new SceneImportOptions();

            importOptions.projectAssetPath = GetSelectedAssetPath();
            importOptions.usdRootPath      = GetDefaultRoot(scene);
            importOptions.changeHandedness = BasisTransformation.FastWithNegativeScale;

            string prefabPath = GetPrefabPath(path, importOptions.projectAssetPath);
            string clipName   = Path.GetFileNameWithoutExtension(path);

            var go = new GameObject(GetObjectName(importOptions.usdRootPath, path));

            try
            {
                // Ensure we have at least one GameObject with the import settings.
                XformImporter.BuildSceneRoot(scene, go.transform, importOptions);
                SceneImporter.SavePrefab(go, prefabPath, clipName, importOptions);
                return(prefabPath);
            }
            finally
            {
                GameObject.DestroyImmediate(go);
                scene.Close();
            }
        }
예제 #2
0
        public static string ImportAsPrefab(Scene scene)
        {
            string path = scene.FilePath;

            // Time-varying data is not supported and often scenes are written without "Default" time
            // values, which makes setting an arbitrary time safer (because if only default was authored
            // the time will be ignored and values will resolve to default time automatically).
            scene.Time = 1.0;

            var importOptions = new SceneImportOptions();

            importOptions.projectAssetPath = GetSelectedAssetPath();
            importOptions.usdRootPath      = GetDefaultRoot(scene);

            string prefabPath = GetPrefabPath(path, importOptions.projectAssetPath);
            string clipName   = Path.GetFileNameWithoutExtension(path);

            var go = new GameObject(GetObjectName(importOptions.usdRootPath, path));

            try
            {
                UsdToGameObject(go, scene, importOptions);
                SceneImporter.SavePrefab(go, prefabPath, clipName, importOptions);
                return(prefabPath);
            }
            finally
            {
                Object.DestroyImmediate(go);
                scene.Close();
            }
        }
예제 #3
0
        /// <summary>
        /// Reimports the USD scene, either fully rebuilding every object or updating them in-place.
        /// </summary>
        /// <param name="forceRebuild">Destroys each GameObject before reimporting.</param>
        public void Reload(bool forceRebuild)
        {
            var options = new SceneImportOptions();

            StateToOptions(ref options);

            options.forceRebuild = forceRebuild;

            if (string.IsNullOrEmpty(options.projectAssetPath))
            {
                options.projectAssetPath = "Assets/";
                OptionsToState(options);
            }

            var root = gameObject;

            string assetPath = GetPrefabAssetPath(root);

            // The prefab asset path will be null for prefab instances.
            // When the assetPath is not null, the object is the prefab itself.
            if (!string.IsNullOrEmpty(assetPath))
            {
                if (options.forceRebuild)
                {
                    DestroyAllImportedObjects();
                }

                pxr.UsdStageLoadRules.Rule activeLoadRule = m_lastScene.Stage.GetLoadRules().GetEffectiveRuleForPath(new pxr.SdfPath("/"));
                if ((activeLoadRule == pxr.UsdStageLoadRules.Rule.AllRule && options.payloadPolicy == PayloadPolicy.DontLoadPayloads) ||
                    (activeLoadRule == pxr.UsdStageLoadRules.Rule.NoneRule && options.payloadPolicy == PayloadPolicy.LoadAll))
                {
                    ClearLastData();
                }
                SceneImporter.ImportUsd(root, GetScene(), new PrimMap(), options);

#if UNITY_EDITOR
                string clipName = Path.GetFileNameWithoutExtension(usdFullPath);
                // As an optimization, we could detect if any meshes or materials were created and only
                // rebuild the prefab in those cases.
                SceneImporter.SavePrefab(root, assetPath, clipName, options);
#endif
            }
            else
            {
                // An instance of a prefab or a vanilla game object.
                // Just reload the scene into memory and let the user decide if they want to send those
                // changes back to the prefab or not.

                if (forceRebuild)
                {
                    // First, destroy all existing USD game objects.
                    DestroyAllImportedObjects();
                }

                ClearLastData();
                SceneImporter.ImportUsd(root, GetScene(), new PrimMap(), options);
            }
        }
예제 #4
0
        /// <summary>
        /// Reimports the USD scene, either fully rebuilding every object or updating them in-place.
        /// </summary>
        /// <param name="forceRebuild">Destroys each GameObject before reimporting.</param>
        public void Reload(bool forceRebuild)
        {
            var options = new SceneImportOptions();

            StateToOptions(ref options);

            options.forceRebuild = forceRebuild;

            if (string.IsNullOrEmpty(options.projectAssetPath))
            {
                options.projectAssetPath = "Assets/";
                OptionsToState(options);
            }

            var root = gameObject;

            string assetPath = GetPrefabAssetPath(root);

            // The prefab asset path will be null for prefab instances.
            // When the assetPath is not null, the object is the prefab itself.
            if (!string.IsNullOrEmpty(assetPath))
            {
                if (options.forceRebuild)
                {
                    DestroyAllImportedObjects();
                }

                SceneImporter.ImportUsd(root, GetScene(), new PrimMap(), options);

#if UNITY_EDITOR
                string clipName = Path.GetFileNameWithoutExtension(usdFullPath);
                // As an optimization, we could detect if any meshes or materials were created and only
                // rebuild the prefab in those cases.
                SceneImporter.SavePrefab(root, assetPath, clipName, options);
#endif
            }
            else
            {
                // An instance of a prefab or a vanilla game object.
                // Just reload the scene into memory and let the user decide if they want to send those
                // changes back to the prefab or not.

                if (forceRebuild)
                {
                    // First, destroy all existing USD game objects.
                    DestroyAllImportedObjects();
                }

                m_lastScene      = null;
                m_lastPrimMap    = null;
                m_lastAccessMask = null;

                SceneImporter.ImportUsd(root, GetScene(), new PrimMap(), options);
            }
        }
예제 #5
0
        public static void MenuImportAsPrefab()
        {
            var scene = InitForOpen();

            if (scene == null)
            {
                return;
            }

            string path = scene.FilePath;

            // Time-varying data is not supported and often scenes are written without "Default" time
            // values, which makes setting an arbitrary time safer (because if only default was authored
            // the time will be ignored and values will resolve to default time automatically).
            scene.Time = 1.0;

            var importOptions = new SceneImportOptions();

            importOptions.projectAssetPath   = GetSelectedAssetPath();
            importOptions.changeHandedness   = BasisTransformation.SlowAndSafe;
            importOptions.materialImportMode = MaterialImportMode.ImportDisplayColor;
            importOptions.usdRootPath        = GetDefaultRoot(scene);

            var invalidChars = Path.GetInvalidFileNameChars();
            var prefabName   = string.Join("_", GetPrefabName(path).Split(invalidChars,
                                                                          System.StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');
            string prefabPath = importOptions.projectAssetPath + prefabName + ".prefab";

            prefabPath = AssetDatabase.GenerateUniqueAssetPath(prefabPath);
            string clipName = Path.GetFileNameWithoutExtension(path);

            var go = new GameObject(GetObjectName(importOptions.usdRootPath, path));

            try
            {
                UsdToGameObject(go, scene, importOptions);
                SceneImporter.SavePrefab(go, prefabPath, clipName, importOptions);
            }
            finally
            {
                GameObject.DestroyImmediate(go);
                scene.Close();
            }
        }
예제 #6
0
        public static void MenuImportAsTimelineClip()
        {
            var scene = InitForOpen();

            if (scene == null)
            {
                return;
            }

            string path = scene.FilePath;

            var invalidChars = Path.GetInvalidFileNameChars();
            var prefabName   = string.Join("_", GetPrefabName(path).Split(invalidChars,
                                                                          System.StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');

            string prefabPath = GetSelectedAssetPath() + prefabName + ".prefab";

            prefabPath = AssetDatabase.GenerateUniqueAssetPath(prefabPath);
            string clipName = Path.GetFileNameWithoutExtension(path);

            var importOptions = new SceneImportOptions();

            importOptions.projectAssetPath   = GetSelectedAssetPath();
            importOptions.changeHandedness   = BasisTransformation.FastWithNegativeScale;
            importOptions.materialImportMode = MaterialImportMode.ImportDisplayColor;
            importOptions.usdRootPath        = GetDefaultRoot(scene);

            var go = new GameObject(GetObjectName(importOptions.usdRootPath, path));

            try
            {
                // Ensure we have at least one GameObject with the import settings.
                XformImporter.BuildSceneRoot(scene, go.transform, importOptions);
                SceneImporter.SavePrefab(go, prefabPath, clipName, importOptions);
            }
            finally
            {
                GameObject.DestroyImmediate(go);
            }
        }