Ejemplo n.º 1
0
        /// <summary>
        /// Destroys loose preset objects that live in memory and not in assets
        /// </summary>
        public static void CleanupPresetsOfType <T>() where T : PumkinPreset
        {
            var    tools    = PumkinsAvatarTools.Instance;
            string typeName = typeof(T).Name;
            var    presets  = GameObject.FindObjectsOfType <T>();

            Type t  = typeof(T);
            Type tP = typeof(PumkinPreset);

            foreach (var preset in presets)
            {
                if (preset && !Helpers.IsAssetInAssets(preset))
                {
                    PumkinsAvatarTools.LogVerbose($"Destroying orphanned {typeName}");
                    Helpers.DestroyAppropriate(preset);
                }
            }

            if (typeof(T) == typeof(PumkinsCameraPreset) || t == tP)
            {
                CameraPresets.RemoveAll(o => o == default(PumkinsCameraPreset) || !Helpers.IsAssetInAssets(o));
            }
            if (typeof(T) == typeof(PumkinsPosePreset) || t == tP)
            {
                PosePresets.RemoveAll(o => o == default(PumkinsPosePreset) || !Helpers.IsAssetInAssets(o));
            }
            if (typeof(T) == typeof(PumkinsBlendshapePreset) || t == tP)
            {
                BlendshapePresets.RemoveAll(o => o == default(PumkinsBlendshapePreset) || !Helpers.IsAssetInAssets(o));
            }

            PumkinsAvatarTools.RefreshPresetIndex <T>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// TODO: Replace with one that reads only the needed lines
        /// </summary>
        static bool ReplacePresetGUIDTemp(string filePath, string newGUID)
        {
            filePath = Helpers.AbsolutePathToLocalAssetsPath(filePath);
            if (Helpers.StringIsNullOrWhiteSpace(filePath) || Helpers.StringIsNullOrWhiteSpace(newGUID))
            {
                PumkinsAvatarTools.LogVerbose($"Filepath ({filePath}) or GUID ({newGUID}) is empty", LogType.Warning);
                return(false);
            }
            else if (!File.Exists(filePath))
            {
                PumkinsAvatarTools.Log($"File {filePath} doesn't exist. Can't fix preset references", LogType.Warning);
                return(false);
            }
            var lines = File.ReadAllLines(filePath);

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                if (!line.Contains("m_Script"))
                {
                    continue;
                }

                lines[i] = Helpers.ReplaceGUIDInLine(line, newGUID, out bool replaced);
                if (replaced)
                {
                    File.WriteAllLines(filePath, lines);
                    return(true);
                }
                break;
            }
            return(false);
        }
    public static void LoadTranslations()
    {
        var guids = AssetDatabase.FindAssets(typeof(PumkinsTranslation).Name);

        translationScriptGUID = guids[0];

        var def = PumkinsTranslation.GetOrCreateDefaultTranslation();

        for (int i = Languages.Count - 1; i >= 0; i--)
        {
            var lang = Languages[i];
            if (i == 0 && def.Equals(lang))
            {
                break;
            }

            if (!Helpers.IsAssetInAssets(lang))
            {
                Helpers.DestroyAppropriate(lang, true); //careful with allow destroying assets here
                Languages.RemoveAt(i);
            }
        }

        //LoadTranslationPresets();
        FixTranslationAssets();

        if (Languages.Count == 0 || !def.Equals(Languages[0]))
        {
            Languages.Insert(0, PumkinsTranslation.Default);
        }

        var loaded = Resources.LoadAll <PumkinsTranslation>(resourceTranslationPath);

        foreach (var l in loaded)
        {
            int i = Languages.IndexOf(l);
            if (i != -1)
            {
                Languages[i] = l;
            }
            else
            {
                Languages.Add(l);
            }
        }

        string langs = "Loaded languages: {";

        for (int i = 0; i < Languages.Count; i++)
        {
            langs += (" " + Languages[i].languageName);
            langs += (i != Languages.Count - 1) ? "," : "";
        }
        langs += " }";
        PumkinsAvatarTools.LogVerbose(langs);
    }
Ejemplo n.º 4
0
        public static void OnPoseWasChanged(PoseChangeType changeType)
        {
            //bool toggle = false;
            //var bonesToKeepDisabled = new List<DynamicBone>();
            //ToggleDynamicBonesEnabledState(SelectedAvatar, ref toggle, ref bonesToKeepDisabled);

            if (PoseChanged != null)
            {
                PoseChanged.Invoke(changeType);
            }

            //EditorApplication.delayCall += () => ToggleDynamicBonesEnabledState(SelectedAvatar, ref toggle, ref bonesToKeepDisabled);
            //PumkinsAvatarTools.RefreshDynamicBoneTransforms(SelectedAvatar);
            PumkinsAvatarTools.LogVerbose("Pose was changed and OnPoseWasChanged() was called with changeType as " + changeType.ToString());
        }
Ejemplo n.º 5
0
        static void SetupGUID <T>() where T : PumkinPreset
        {
            if (typeof(T) == typeof(PumkinPreset))
            {
                SetupAllGUIDs();    // bad redirect to setup all
                return;
            }

            string typeName = typeof(T).Name;
            var    guids    = AssetDatabase.FindAssets(typeName);
            string guid     = null;
            string path     = null;

            foreach (var g in guids)
            {
                path = AssetDatabase.GUIDToAssetPath(g);
                if (!path.EndsWith($"{typeName}.cs"))
                {
                    continue;
                }
                guid = g;
                break;
            }

            if (typeof(T) == typeof(PumkinsBlendshapePreset))
            {
                blendshapePresetScriptGUID = guid;
            }
            else if (typeof(T) == typeof(PumkinsCameraPreset))
            {
                cameraPresetScriptGUID = guid;
            }
            else if (typeof(T) == typeof(PumkinsPosePreset))
            {
                posePresetScriptGUID = guid;
            }
            else
            {
                return;
            }

            PumkinsAvatarTools.LogVerbose($"Set GUID of {typeof(T).Name} script at {path} to {guid}");
        }
Ejemplo n.º 6
0
        public static void FixScriptReferences <T>() where T : PumkinPreset
        {
            SetupGUID <T>();

            Type t  = typeof(T);
            Type pT = typeof(PumkinPreset);

            var pathsGuids = new Dictionary <string, string>();

            if (t == typeof(PumkinsBlendshapePreset) || t == pT)
            {
                pathsGuids.Add(Helpers.LocalAssetsPathToAbsolutePath(localBlendshapesPath), blendshapePresetScriptGUID);
            }
            if (t == typeof(PumkinsCameraPreset) || t == pT)
            {
                pathsGuids.Add(Helpers.LocalAssetsPathToAbsolutePath(localCamerasPath), cameraPresetScriptGUID);
            }
            if (t == typeof(PumkinsPosePreset) || t == pT)
            {
                pathsGuids.Add(Helpers.LocalAssetsPathToAbsolutePath(localPosesPath), posePresetScriptGUID);
            }

            foreach (var kv in pathsGuids)
            {
                var path = kv.Key;
                var guid = kv.Value;
                if (!Directory.Exists(path))
                {
                    PumkinsAvatarTools.LogVerbose($"Directory {path} doesn't exist. Can't fix references");
                    continue;
                }
                var files = Directory.GetFiles($"{path}", "*.asset", SearchOption.AllDirectories);
                foreach (var file in files)
                {
                    if (ReplacePresetGUIDTemp(file, guid))
                    {
                        AssetDatabase.ImportAsset(Helpers.AbsolutePathToLocalAssetsPath(file), ImportAssetOptions.ForceUpdate);
                    }
                }
                PumkinsAvatarTools.LogVerbose($"Fixed references for type {typeof(T).Name}");
            }
        }
        /// <summary>
        /// Sets hardcoded TPose.
        /// </summary>
        public static void SetTPoseHardcoded(GameObject avatar)
        {
            Undo.RegisterFullObjectHierarchyUndo(avatar, "Set TPose");

            var anim = avatar.GetComponent <Animator>();

            if (anim && anim.avatar && anim.avatar.isHuman)
            {
                Vector3    pos = avatar.transform.position;
                Quaternion rot = avatar.transform.rotation;

                avatar.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);

                var humanPoseHandler = new HumanPoseHandler(anim.avatar, avatar.transform);

                var humanPose = new HumanPose();
                humanPoseHandler.GetHumanPose(ref humanPose);

                //A long time ago CATS used to export avatars with a Armature scale of 100. This caused issues applying poses.
                //For now we'll just hardcode search for "Armature".
                //TODO: Find a better way to get the armature and check if it's scale could cause issues when tposing
                Transform armature = avatar.transform.Find("Armature");
                if (!(armature && armature.localScale == Vector3.one))
                {
                    if (humanPose.bodyPosition.y < 1 && !Mathf.Approximately(humanPose.bodyPosition.y, 1))
                    {
                        humanPose.bodyPosition.y = 1;
                        PumkinsAvatarTools.LogVerbose(Strings.PoseEditor.bodyPositionYTooSmall, LogType.Warning, humanPose.bodyPosition.y.ToString());
                    }
                }

                #region Hardcoded TPose Values
                humanPose.muscles[0]  = -6.830189E-07f;
                humanPose.muscles[1]  = 4.268869E-08f;
                humanPose.muscles[2]  = 4.268868E-08f;
                humanPose.muscles[3]  = -8.537737E-08f;
                humanPose.muscles[4]  = 0f;
                humanPose.muscles[5]  = 0f;
                humanPose.muscles[6]  = 0f;
                humanPose.muscles[7]  = 0f;
                humanPose.muscles[8]  = 0f;
                humanPose.muscles[9]  = 4.268868E-08f;
                humanPose.muscles[10] = -8.537737E-08f;
                humanPose.muscles[11] = 4.268868E-08f;
                humanPose.muscles[12] = 3.415095E-07f;
                humanPose.muscles[13] = 0f;
                humanPose.muscles[14] = 0f;
                humanPose.muscles[15] = 0f;
                humanPose.muscles[16] = 0f;
                humanPose.muscles[17] = 0f;
                humanPose.muscles[18] = 0f;
                humanPose.muscles[19] = 0f;
                humanPose.muscles[20] = 0f;
                humanPose.muscles[21] = 0.5994893f;
                humanPose.muscles[22] = 0.004985309f;
                humanPose.muscles[23] = 0.00297395f;
                humanPose.muscles[24] = 0.9989594f;
                humanPose.muscles[25] = -0.02284526f;
                humanPose.muscles[26] = -3.449878E-05f;
                humanPose.muscles[27] = -0.0015229f;
                humanPose.muscles[28] = -4.781132E-07f;
                humanPose.muscles[29] = 0.599489f;
                humanPose.muscles[30] = 0.004985378f;
                humanPose.muscles[31] = 0.002974245f;
                humanPose.muscles[32] = 0.9989589f;
                humanPose.muscles[33] = -0.02284535f;
                humanPose.muscles[34] = -3.548912E-05f;
                humanPose.muscles[35] = -0.001522672f;
                humanPose.muscles[36] = -1.024528E-07f;
                humanPose.muscles[37] = -2.429621E-07f;
                humanPose.muscles[38] = 1.549688E-07f;
                humanPose.muscles[39] = 0.3847253f;
                humanPose.muscles[40] = 0.310061f;
                humanPose.muscles[41] = -0.103543f;
                humanPose.muscles[42] = 0.9925866f;
                humanPose.muscles[43] = 0.159403f;
                humanPose.muscles[44] = -0.01539368f;
                humanPose.muscles[45] = 0.01405432f;
                humanPose.muscles[46] = 5.680533E-08f;
                humanPose.muscles[47] = 2.701393E-07f;
                humanPose.muscles[48] = 0.3847259f;
                humanPose.muscles[49] = 0.3100605f;
                humanPose.muscles[50] = -0.1035404f;
                humanPose.muscles[51] = 0.9925874f;
                humanPose.muscles[52] = 0.1593992f;
                humanPose.muscles[53] = -0.01539393f;
                humanPose.muscles[54] = 0.01405326f;
                humanPose.muscles[55] = -0.7706841f;
                humanPose.muscles[56] = 0.423209f;
                humanPose.muscles[57] = 0.6456643f;
                humanPose.muscles[58] = 0.6362566f;
                humanPose.muscles[59] = 0.6677276f;
                humanPose.muscles[60] = -0.4597229f;
                humanPose.muscles[61] = 0.811684f;
                humanPose.muscles[62] = 0.8116837f;
                humanPose.muscles[63] = 0.6683907f;
                humanPose.muscles[64] = -0.5737826f;
                humanPose.muscles[65] = 0.8116839f;
                humanPose.muscles[66] = 0.8116843f;
                humanPose.muscles[67] = 0.6670681f;
                humanPose.muscles[68] = -0.6459302f;
                humanPose.muscles[69] = 0.8116837f;
                humanPose.muscles[70] = 0.8116839f;
                humanPose.muscles[71] = 0.666789f;
                humanPose.muscles[72] = -0.4676594f;
                humanPose.muscles[73] = 0.811684f;
                humanPose.muscles[74] = 0.8116839f;
                humanPose.muscles[75] = -0.7706831f;
                humanPose.muscles[76] = 0.4232127f;
                humanPose.muscles[77] = 0.6456538f;
                humanPose.muscles[78] = 0.6362569f;
                humanPose.muscles[79] = 0.6678051f;
                humanPose.muscles[80] = -0.4589976f;
                humanPose.muscles[81] = 0.8116843f;
                humanPose.muscles[82] = 0.8116842f;
                humanPose.muscles[83] = 0.668391f;
                humanPose.muscles[84] = -0.5737844f;
                humanPose.muscles[85] = 0.811684f;
                humanPose.muscles[86] = 0.8116837f;
                humanPose.muscles[87] = 0.6669571f;
                humanPose.muscles[88] = -0.6492739f;
                humanPose.muscles[89] = 0.8116841f;
                humanPose.muscles[90] = 0.8116843f;
                humanPose.muscles[91] = 0.6667888f;
                humanPose.muscles[92] = -0.4676568f;
                humanPose.muscles[93] = 0.8116842f;
                humanPose.muscles[94] = 0.8116836f;
                #endregion

                humanPoseHandler.SetHumanPose(ref humanPose);
                avatar.transform.SetPositionAndRotation(pos, rot);

                PumkinsAvatarTools.OnPoseWasChanged(PumkinsAvatarTools.PoseChangeType.Reset);
            }
            else
            {
                PumkinsAvatarTools.Log(Strings.Log.cantSetPoseNonHumanoid, LogType.Warning, "TPose");
            }
        }