Skip to content

Instantly share code, notes, and snippets.

@PopupAsylumUK
Created June 13, 2017 21:43
Show Gist options
  • Save PopupAsylumUK/c4eea1b5b0eed32edaa1f7ed6c63d601 to your computer and use it in GitHub Desktop.
Save PopupAsylumUK/c4eea1b5b0eed32edaa1f7ed6c63d601 to your computer and use it in GitHub Desktop.
An editor window for copying the local positions and rotations from 1 set of transforms to another, used in BeeBeeQ for copying a hand pose from left to right
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class CopyTransformPositions : EditorWindow {
public Transform sourceRoot;
public Transform targetRoot;
[MenuItem("Window/Copy Transforms")]
static void Init() {
// Get existing open window or if none, make a new one:
CopyTransformPositions window = (CopyTransformPositions)EditorWindow.GetWindow(typeof(CopyTransformPositions));
window.Show();
window.position = new Rect(20, 80, 500, 620);
}
static string Reverse(string input) {
char[] output = input.ToCharArray();
for (int i = 0; i < output.Length; i++) {
output[i] = input[input.Length - 1 - i];
}
return new string(output);
}
List<Transform> GetSortedHeirarchy(Transform root, bool isRoot = true) {
List<Transform> result = new List<Transform>();
if (isRoot) {
result.Add(root);
}
List<Transform> directChildren = new List<Transform>();
for (int i = 0; i < root.childCount; i++) {
directChildren.Add(root.GetChild(i));
}
directChildren.Sort((x, y) => x.name.CompareTo(y.name));
result.AddRange(directChildren);
for (int i = 0; i < root.childCount; i++) {
result.AddRange(GetSortedHeirarchy(root.GetChild(i), false));
}
return result;
}
void CopyLengths(Transform source, Transform target) {
List<Transform> sourceChildren = GetSortedHeirarchy(source);
List<Transform> targetChildren = GetSortedHeirarchy(target);
for (int i = 0; i < targetChildren.Count; i++) {
targetChildren[i].localPosition = targetChildren[i].localPosition.normalized * sourceChildren[i].localPosition.magnitude;
}
}
void Copy(Transform source, Transform target, bool flip, float lerp = 1) {
List<Transform> sourceChildren = GetSortedHeirarchy(source);
List<Transform> targetChildren = GetSortedHeirarchy(target);
for (int i = 0; i < targetChildren.Count; i++) {
Vector3 pos = sourceChildren[i].localPosition;
Quaternion rot = sourceChildren[i].localRotation;
if (flip) {
rot = new Quaternion(sourceChildren[i].localRotation.x * -1.0f,
sourceChildren[i].localRotation.y,
sourceChildren[i].localRotation.z,
sourceChildren[i].localRotation.w * -1.0f);
pos = new Vector3(-sourceChildren[i].localPosition.x, sourceChildren[i].localPosition.y, sourceChildren[i].localPosition.z);
}
pos = Vector3.Lerp(targetChildren[i].localPosition, pos, lerp);
rot = Quaternion.Lerp(targetChildren[i].localRotation, rot, lerp);
targetChildren[i].localPosition = pos;
targetChildren[i].localRotation = rot;
targetChildren[i].transform.localScale = sourceChildren[i].transform.localScale;
}
}
void RecordUndo() {
Undo.RecordObjects(GetSortedHeirarchy(sourceRoot).ToArray(), "CopyJoints");
Undo.RecordObjects(GetSortedHeirarchy(targetRoot).ToArray(), "CopyJoints");
}
private void OnGUI() {
GUILayout.BeginHorizontal();
if (GUILayout.Button("Copy")) {
if (targetRoot && sourceRoot) {
RecordUndo();
Copy(sourceRoot, targetRoot, false);
}
}
if (GUILayout.Button("Lerp", GUILayout.Width(60))) {
if (targetRoot && sourceRoot) {
RecordUndo();
Copy(sourceRoot, targetRoot, false, 0.1f);
}
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Copy Flip")) {
if (targetRoot && sourceRoot) {
RecordUndo();
Copy(sourceRoot, targetRoot, true);
}
}
if (GUILayout.Button("Lerp", GUILayout.Width(60))) {
if (targetRoot && sourceRoot) {
RecordUndo();
Copy(sourceRoot, targetRoot, true, 0.1f);
}
}
GUILayout.EndHorizontal();
if (GUILayout.Button("Copy Lengths")) {
if (targetRoot && sourceRoot) {
RecordUndo();
CopyLengths(sourceRoot, targetRoot);
}
}
EditorGUILayout.BeginHorizontal();
sourceRoot = EditorGUILayout.ObjectField(new GUIContent("Source"), sourceRoot, typeof(Transform), true) as Transform;
targetRoot = EditorGUILayout.ObjectField(new GUIContent("Target"), targetRoot, typeof(Transform), true) as Transform;
EditorGUILayout.EndHorizontal();
if (targetRoot && sourceRoot) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
List<Transform> children = GetSortedHeirarchy(sourceRoot);
for (int i = 0; i < children.Count; i++) {
EditorGUILayout.LabelField(children[i].name);
}
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical();
children = GetSortedHeirarchy(targetRoot);
for (int i = 0; i < children.Count; i++) {
EditorGUILayout.LabelField(children[i].name);
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment