void RemapPaths(AnimationRetargetingData targetingData, SerializedObject so) { foreach (PathMapping mapping in targetingData.pathMappings) { List <SerializedProperty> props = propertiesFor(so, pathPropName, mapping.fromPath, mapping.matchType, pathProperties); if (props.Count == 0) { log("[AttributeMapper] did not find property for path:" + mapping.fromPath); } else { foreach (SerializedProperty prop in props) { if (mapping.matchType == MatchType.ExactMatch) { log("[AttributeMapper] Renaming " + mapping.fromPath + " to " + mapping.toPath); prop.stringValue = mapping.toPath; } else if (mapping.matchType == MatchType.Contains) { string newVal = prop.stringValue.Replace(mapping.fromPath, mapping.toPath); log("[AttributeMapper] Renaming " + prop.stringValue + " to " + newVal); prop.stringValue = newVal; } } } } }
public void Retarget(AnimationRetargetingData targetingData, List <AnimationClip> selectedClips) { initLogger(); foreach (AnimationClip clip in selectedClips) { log("[AttributeMapper] Retargeting clip:" + clip); SerializedObject so = new SerializedObject(clip); RemapAttributes(targetingData, so); RemapPaths(targetingData, so); so.ApplyModifiedProperties(); } flushLog(); }
void updateRetargetingOptions() { retargetingOptions = new List <AnimationRetargetingData>(); string[] guids = AssetDatabase.FindAssets("t:AnimationRetargetingData"); retargetingOptionsLabels = new string[guids.Length + 1]; for (int i = 0; i < guids.Length; i++) { string guid = guids[i]; string assetPath = AssetDatabase.GUIDToAssetPath(guid); AnimationRetargetingData option = (AnimationRetargetingData)AssetDatabase.LoadAssetAtPath(assetPath, typeof(AnimationRetargetingData)); retargetingOptions.Add(option); retargetingOptionsLabels[i] = option.name; } }
void RemapAttributes(AnimationRetargetingData targetingData, SerializedObject so) { foreach (AttributeMapping mapping in targetingData.attributeMappings) { List <SerializedProperty> props = propertiesFor(so, attributePropName, targetingData.inputPrefix + "." + mapping.fromPath, MatchType.ExactMatch, baseProperties); if (props.Count == 0) { log("[AttributeMapper] did not find property:" + mapping.fromPath); } else { switch (mapping.action) { case AttributeMappingAction.Rename: log("[AttributeMapper] Renaming " + mapping.fromPath + " to " + mapping.toPath); foreach (SerializedProperty prop in props) { // log("[AttributeMapper] found property...modifying."+ prop.propertyPath); prop.stringValue = targetingData.outputPrefix + "." + mapping.toPath; } break; case AttributeMappingAction.Copy: foreach (SerializedProperty prop in props) { string initialPath = prop.propertyPath; SerializedProperty parentProp = parentOf(prop); int index; if (tryGetIndexOf(initialPath, out index)) { parentProp.InsertArrayElementAtIndex(index); SerializedProperty dupeProp = parentProp.GetArrayElementAtIndex(index + 1).FindPropertyRelative("attribute"); // log("[AttributeMapper.COPY] found property...modifying."+ dupeProp.propertyPath); dupeProp.stringValue = targetingData.outputPrefix + "." + mapping.toPath; } else { log("[AttributeMapper] did not find index...."); } } break; case AttributeMappingAction.Delete: foreach (SerializedProperty prop in props) { string initialPath = prop.propertyPath; SerializedProperty parentProp = parentOf(prop); int index; if (tryGetIndexOf(initialPath, out index)) { parentProp.DeleteArrayElementAtIndex(index); } else { log("[AttributeMapper] did not find index...."); } } break; } } } }
void OnGUI() { scrollPos = EditorGUILayout.BeginScrollView(scrollPos); EditorGUILayout.BeginHorizontal(); if (retargetingIndex >= retargetingOptions.Count) { retargetingIndex = retargetingOptions.Count - 1; targetingData = retargetingOptions[retargetingIndex]; } int newIndex = EditorGUILayout.Popup("Select An Option", retargetingIndex, retargetingOptionsLabels); if (newIndex != retargetingIndex) { retargetingIndex = newIndex; } targetingData = retargetingOptions[retargetingIndex]; if (targetingData != null) { if (GUILayout.Button("Copy", GUILayout.Width(100))) { string copyPath = generatePathName(targetingData.name); AnimationRetargetingData copy = Instantiate(targetingData); copy.isPreset = false; saveAsset(copyPath, copy); copy.name = Path.GetFileNameWithoutExtension(copyPath); targetingData = copy; updateRetargetingOptions(); updateRetargetingIndex(); } } if (GUILayout.Button("New", GUILayout.Width(100))) { createInstance <AnimationRetargetingData>("Animation Retargeting", ref targetingData); updateRetargetingOptions(); updateRetargetingIndex(); } EditorGUILayout.EndHorizontal(); // targetingData = (AnimationRetargetingData)EditorGUILayout.ObjectField(targetingData, typeof(AnimationRetargetingData), false); if (targetingData != null) { SerializedObject so = new SerializedObject(targetingData); SerializedObject windowSO = new SerializedObject(this); EditorGUILayout.HelpBox("Drag Animation Clips, Animators, or Folders into the Items list below to retarget all clips inside them.", MessageType.Info); SerializedProperty itemsProp = windowSO.FindProperty("items"); EditorGUILayout.PropertyField(itemsProp, new GUIContent("Items"), true); if (targetingData.isPreset) { EditorGUILayout.HelpBox("'" + targetingData.name + "' is a preset and cannot be modified. In order to edit it, make a copy first.", MessageType.Info); GUI.enabled = false; } SerializedProperty inputPrefix = so.FindProperty("inputPrefix"); EditorGUILayout.PropertyField(inputPrefix, new GUIContent("Input Prefix")); SerializedProperty outputPrefix = so.FindProperty("outputPrefix"); EditorGUILayout.PropertyField(outputPrefix, new GUIContent("Output Prefix")); SerializedProperty attributeMappings = so.FindProperty("attributeMappings"); if (targetingData.attributeMappings.Count == 0) { EditorGUILayout.HelpBox("You need to add an 'attribute mapping' to retarget your animation properties from one path to another. This is the name of the property you are animating.", MessageType.Info); } EditorGUILayout.PropertyField(attributeMappings, new GUIContent("Attribute Mappings"), true); SerializedProperty pathMappings = so.FindProperty("pathMappings"); EditorGUILayout.PropertyField(pathMappings, new GUIContent("Path Mappings"), true); if (targetingData.isPreset) { GUI.enabled = true; } so.ApplyModifiedProperties(); windowSO.ApplyModifiedProperties(); if (GUILayout.Button("Retarget")) { AttributeMapper remapper = new AttributeMapper(); List <AnimationClip> clips = getClipsFromItems(items); remapper.Retarget(targetingData, clips); } } else { if (retargetingOptions.Count == 0) { // display help EditorGUILayout.HelpBox("In order to retarget animations, you need to create an Animation Retargeting scriptable object or select one of the ones that comes with the project.", MessageType.Info); if (GUILayout.Button("Create Animation Retargeting Data")) { createInstance <AnimationRetargetingData>("Animation Retargeting", ref targetingData); } } else { // the interface should show up as normal. } } EditorGUILayout.EndScrollView(); }