Tanoda
LeapHandsAutoRig.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System.Collections;
11using System.Collections.Generic;
12using System.Linq;
13
14namespace Leap.Unity {
16 [AddComponentMenu("Leap/Auto Rig Hands")]
17 public class LeapHandsAutoRig : MonoBehaviour {
18
19 private HandModelManager HandPoolToPopulate;
20 public Animator AnimatorForMapping;
21
22 public string ModelGroupName = null;
23 [Tooltip("Set to True if each finger has an extra trasform between palm and base of the finger.")]
24 public bool UseMetaCarpals;
25 [Header("RiggedHand Components")]
28 [Header("HandTransitionBehavior Components")]
31 [Tooltip("Test")]
32 [Header("RiggedFinger Components")]
43 [Header("Palm & Finger Direction Vectors.")]
44 public Vector3 modelFingerPointing_L = new Vector3(0, 0, 0);
45 public Vector3 modelPalmFacing_L = new Vector3(0, 0, 0);
46 public Vector3 modelFingerPointing_R = new Vector3(0, 0, 0);
47 public Vector3 modelPalmFacing_R = new Vector3(0, 0, 0);
48 [Tooltip("Toggling this value will reverse the ModelPalmFacing vectors to both RiggedHand's and all RiggedFingers. Change if hands appear backward when tracking.")]
49 [SerializeField]
50 public bool FlipPalms = false;
51 #pragma warning disable 0414
52 [SerializeField]
53 [HideInInspector]
54 private bool flippedPalmsState = false;
55 #pragma warning restore 0414
56
59 [ContextMenu("AutoRig")]
60 public void AutoRig() {
61 throw new System.NotSupportedException(
62 "AutoRig is currently disabled due to propagating non-undo-able changes " +
63 "to the editor.");
64
65 // HandPoolToPopulate = GameObject.FindObjectOfType<HandModelManager>();
66 // AnimatorForMapping = gameObject.GetComponent<Animator>();
67 // if (AnimatorForMapping != null) {
68 // if (AnimatorForMapping.isHuman == true) {
69 // AutoRigMecanim();
70 // RiggedHand_L.StoreJointsStartPose();
71 // RiggedHand_R.StoreJointsStartPose();
72 // return;
73 // }
74 // else {
75 // Debug.LogWarning("The Mecanim Avatar for this asset does not contain a valid IsHuman definition. Attempting to auto map by name.");
76 // }
77 // }
78 // AutoRigByName();
79 }
80
81 // /**Allows a start pose for the rigged hands to be created and stored anytime. */
82 // [ContextMenu("StoreStartPose")]
83 // public void StoreStartPose() {
84 // if (RiggedHand_L && RiggedHand_R) {
85 // RiggedHand_L.StoreJointsStartPose();
86 // RiggedHand_R.StoreJointsStartPose();
87 // }
88 // else Debug.LogWarning("Please AutoRig before attempting to Store Start Pose");
89 // }
90
91 // /**Uses transform names to discover and assign RiggedHands scripts,
92 // * then calls methods in the RiggedHands that use transform nanes to discover fingers.*/
93 // [ContextMenu("AutoRigByName")]
94 // void AutoRigByName() {
95 // List<string> LeftHandStrings = new List<string> { "left" };
96 // List<string> RightHandStrings = new List<string> { "right" };
97
98 // //Assigning these here since this component gets added and used at editor time
99 // HandPoolToPopulate = GameObject.FindObjectOfType<HandModelManager>();
100 // Reset();
101
102 // //Find hands and assigns RiggedHands
103 // Transform Hand_L = null;
104 // foreach (Transform t in transform) {
105 // if (LeftHandStrings.Any(w => t.name.ToLower().Contains(w))) {
106 // Hand_L = t;
107 // }
108 // }
109 // if (Hand_L != null) {
110 // RiggedHand_L = Hand_L.gameObject.AddComponent<RiggedHand>();
111 // HandTransitionBehavior_L = Hand_L.gameObject.AddComponent<HandEnableDisable>();
112 // RiggedHand_L.Handedness = Chirality.Left;
113 // RiggedHand_L.SetEditorLeapPose = false;
114 // RiggedHand_L.useMetaCarpals = UseMetaCarpals;
115 // RiggedHand_L.SetupRiggedHand();
116
117 // RiggedFinger_L_Thumb = (RiggedFinger)RiggedHand_L.fingers[0];
118 // RiggedFinger_L_Index = (RiggedFinger)RiggedHand_L.fingers[1];
119 // RiggedFinger_L_Mid = (RiggedFinger)RiggedHand_L.fingers[2];
120 // RiggedFinger_L_Ring = (RiggedFinger)RiggedHand_L.fingers[3];
121 // RiggedFinger_L_Pinky = (RiggedFinger)RiggedHand_L.fingers[4];
122
123 // modelFingerPointing_L = RiggedHand_L.modelFingerPointing;
124 // modelPalmFacing_L = RiggedHand_L.modelPalmFacing;
125
126 // RiggedHand_L.StoreJointsStartPose();
127 // }
128 // Transform Hand_R = null;
129 // foreach (Transform t in transform) {
130 // if (RightHandStrings.Any(w => t.name.ToLower().Contains(w))) {
131 // Hand_R = t;
132 // }
133 // }
134 // if (Hand_R != null) {
135 // RiggedHand_R = Hand_R.gameObject.AddComponent<RiggedHand>();
136 // HandTransitionBehavior_R = Hand_R.gameObject.AddComponent<HandEnableDisable>();
137 // RiggedHand_R.Handedness = Chirality.Right;
138 // RiggedHand_R.SetEditorLeapPose = false;
139 // RiggedHand_R.useMetaCarpals = UseMetaCarpals;
140 // RiggedHand_R.SetupRiggedHand();
141
142 // RiggedFinger_R_Thumb = (RiggedFinger)RiggedHand_R.fingers[0];
143 // RiggedFinger_R_Index = (RiggedFinger)RiggedHand_R.fingers[1];
144 // RiggedFinger_R_Mid = (RiggedFinger)RiggedHand_R.fingers[2];
145 // RiggedFinger_R_Ring = (RiggedFinger)RiggedHand_R.fingers[3];
146 // RiggedFinger_R_Pinky = (RiggedFinger)RiggedHand_R.fingers[4];
147
148 // modelFingerPointing_R = RiggedHand_R.modelFingerPointing;
149 // modelPalmFacing_R = RiggedHand_R.modelPalmFacing;
150
151 // RiggedHand_R.StoreJointsStartPose();
152 // }
153 // //Find palms and assign to RiggedHands
154 // //RiggedHand_L.palm = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftHand);
155 // //RiggedHand_R.palm = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightHand);
156
157 // if (ModelGroupName == "" || ModelGroupName != null) {
158 // ModelGroupName = transform.name;
159 // }
160 // HandPoolToPopulate.AddNewGroup(ModelGroupName, RiggedHand_L, RiggedHand_R);
161 // }
162
163 // /**Uses Mecanim transform mapping to find hands and assign RiggedHands scripts
164 // * and to find base of each finger and asisng RiggedFinger script.
165 // * Then calls methods in the RiggedHands that use transform names to discover fingers */
166 // [ContextMenu("AutoRigMecanim")]
167 // void AutoRigMecanim() {
168 // //Assigning these here since this component gets added and used at editor time
169 // AnimatorForMapping = gameObject.GetComponent<Animator>();
170 // HandPoolToPopulate = GameObject.FindObjectOfType<HandModelManager>();
171 // Reset();
172
173 // //Find hands and assign RiggedHands
174 // Transform Hand_L = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftHand);
175 // if (Hand_L.GetComponent<RiggedHand>()) {
176 // RiggedHand_L = Hand_L.GetComponent<RiggedHand>();
177 // }
178 // else RiggedHand_L = Hand_L.gameObject.AddComponent<RiggedHand>();
179 // HandTransitionBehavior_L =Hand_L.gameObject.AddComponent<HandDrop>();
180 // RiggedHand_L.Handedness = Chirality.Left;
181 // RiggedHand_L.SetEditorLeapPose = false;
182
183 // Transform Hand_R = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightHand);
184 // if (Hand_R.GetComponent<RiggedHand>()) {
185 // RiggedHand_R = Hand_R.GetComponent<RiggedHand>();
186 // }
187 // else RiggedHand_R = Hand_R.gameObject.AddComponent<RiggedHand>();
188 // HandTransitionBehavior_R = Hand_R.gameObject.AddComponent<HandDrop>();
189 // RiggedHand_R.Handedness = Chirality.Right;
190 // RiggedHand_R.SetEditorLeapPose = false;
191
192 // //Find palms and assign to RiggedHands
193 // RiggedHand_L.palm = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftHand);
194 // RiggedHand_R.palm = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightHand);
195 // RiggedHand_R.useMetaCarpals = UseMetaCarpals;
196 // RiggedHand_L.useMetaCarpals = UseMetaCarpals;
197
198 // findAndAssignRiggedFingers(UseMetaCarpals);
199
200 // RiggedHand_L.AutoRigRiggedHand(RiggedHand_L.palm, RiggedFinger_L_Pinky.transform, RiggedFinger_L_Index.transform);
201 // RiggedHand_R.AutoRigRiggedHand(RiggedHand_R.palm, RiggedFinger_R_Pinky.transform, RiggedFinger_R_Index.transform);
202 // if (ModelGroupName == "" || ModelGroupName != null) {
203 // ModelGroupName = transform.name;
204 // }
205 // HandPoolToPopulate.AddNewGroup(ModelGroupName, RiggedHand_L, RiggedHand_R);
206
207 // modelFingerPointing_L = RiggedHand_L.modelFingerPointing;
208 // modelPalmFacing_L = RiggedHand_L.modelPalmFacing;
209 // modelFingerPointing_R = RiggedHand_R.modelFingerPointing;
210 // modelPalmFacing_R = RiggedHand_R.modelPalmFacing;
211 // }
212
213 // //Find Fingers and assign RiggedFingers
214 // private void findAndAssignRiggedFingers(bool useMetaCarpals) {
215 // if (!useMetaCarpals) {
216 // RiggedFinger_L_Thumb = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftThumbProximal).gameObject.AddComponent<RiggedFinger>();
217 // RiggedFinger_L_Index = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftIndexProximal).gameObject.AddComponent<RiggedFinger>();
218 // RiggedFinger_L_Mid = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftMiddleProximal).gameObject.AddComponent<RiggedFinger>();
219 // RiggedFinger_L_Ring = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftRingProximal).gameObject.AddComponent<RiggedFinger>();
220 // RiggedFinger_L_Pinky = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftLittleProximal).gameObject.AddComponent<RiggedFinger>();
221 // RiggedFinger_R_Thumb = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightThumbProximal).gameObject.AddComponent<RiggedFinger>();
222 // RiggedFinger_R_Index = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightIndexProximal).gameObject.AddComponent<RiggedFinger>();
223 // RiggedFinger_R_Mid = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightMiddleProximal).gameObject.AddComponent<RiggedFinger>();
224 // RiggedFinger_R_Ring = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightRingProximal).gameObject.AddComponent<RiggedFinger>();
225 // RiggedFinger_R_Pinky = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightLittleProximal).gameObject.AddComponent<RiggedFinger>();
226 // }
227 // else {
228 // RiggedFinger_L_Thumb = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftThumbProximal).gameObject.AddComponent<RiggedFinger>();
229 // RiggedFinger_L_Index = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftIndexProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
230 // RiggedFinger_L_Mid = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftMiddleProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
231 // RiggedFinger_L_Ring = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftRingProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
232 // RiggedFinger_L_Pinky = AnimatorForMapping.GetBoneTransform(HumanBodyBones.LeftLittleProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
233 // RiggedFinger_R_Thumb = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightThumbProximal).gameObject.AddComponent<RiggedFinger>();
234 // RiggedFinger_R_Index = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightIndexProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
235 // RiggedFinger_R_Mid = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightMiddleProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
236 // RiggedFinger_R_Ring = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightRingProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
237 // RiggedFinger_R_Pinky = AnimatorForMapping.GetBoneTransform(HumanBodyBones.RightLittleProximal).gameObject.transform.parent.gameObject.AddComponent<RiggedFinger>();
238 // }
239 // RiggedFinger_L_Thumb.fingerType = Finger.FingerType.TYPE_THUMB;
240 // RiggedFinger_L_Index.fingerType = Finger.FingerType.TYPE_INDEX;
241 // RiggedFinger_L_Mid.fingerType = Finger.FingerType.TYPE_MIDDLE;
242 // RiggedFinger_L_Ring.fingerType = Finger.FingerType.TYPE_RING;
243 // RiggedFinger_L_Pinky.fingerType = Finger.FingerType.TYPE_PINKY;
244 // RiggedFinger_R_Thumb.fingerType = Finger.FingerType.TYPE_THUMB;
245 // RiggedFinger_R_Index.fingerType = Finger.FingerType.TYPE_INDEX;
246 // RiggedFinger_R_Mid.fingerType = Finger.FingerType.TYPE_MIDDLE;
247 // RiggedFinger_R_Ring.fingerType = Finger.FingerType.TYPE_RING;
248 // RiggedFinger_R_Pinky.fingerType = Finger.FingerType.TYPE_PINKY;
249 // }
250
251 // /**Removes existing RiggedFinger components so the auto rigging process can be rerun. */
252 // void Reset() {
253 // RiggedFinger[] riggedFingers = GetComponentsInChildren<RiggedFinger>();
254 // foreach (RiggedFinger finger in riggedFingers) {
255 // DestroyImmediate(finger);
256 // }
257 // DestroyImmediate(RiggedHand_L);
258 // DestroyImmediate(RiggedHand_R);
259 // DestroyImmediate(HandTransitionBehavior_L);
260 // DestroyImmediate(HandTransitionBehavior_R);
261 // if (HandPoolToPopulate != null) {
262 // HandPoolToPopulate.RemoveGroup(ModelGroupName);
263 // }
264 // }
265 // public void PushVectorValues() {
266 // //push palm and finger facing values to RiggedHand's and RiggedFinger's
267 // if (RiggedHand_L) {
268 // RiggedHand_L.modelFingerPointing = modelFingerPointing_L;
269 // RiggedHand_L.modelPalmFacing = modelPalmFacing_L;
270 // }
271 // if (RiggedHand_R) {
272 // RiggedHand_R.modelFingerPointing = modelFingerPointing_R;
273 // RiggedHand_R.modelPalmFacing = modelPalmFacing_R;
274 // }
275 // if (RiggedFinger_L_Thumb) {
276 // RiggedFinger_L_Thumb.modelFingerPointing = modelFingerPointing_L;
277 // RiggedFinger_L_Thumb.modelPalmFacing = modelPalmFacing_L;
278 // }
279 // if (RiggedFinger_L_Index) {
280 // RiggedFinger_L_Index.modelFingerPointing = modelFingerPointing_L;
281 // RiggedFinger_L_Index.modelPalmFacing = modelPalmFacing_L;
282 // }
283 // if (RiggedFinger_L_Mid) {
284 // RiggedFinger_L_Mid.modelFingerPointing = modelFingerPointing_L;
285 // RiggedFinger_L_Mid.modelPalmFacing = modelPalmFacing_L;
286 // }
287 // if (RiggedFinger_L_Ring) {
288 // RiggedFinger_L_Ring.modelFingerPointing = modelFingerPointing_L;
289 // RiggedFinger_L_Ring.modelPalmFacing = modelPalmFacing_L;
290 // }
291 // if (RiggedFinger_L_Pinky) {
292 // RiggedFinger_L_Pinky.modelFingerPointing = modelFingerPointing_L;
293 // RiggedFinger_L_Pinky.modelPalmFacing = modelPalmFacing_L;
294 // }
295 // if (RiggedFinger_R_Thumb) {
296 // RiggedFinger_R_Thumb.modelFingerPointing = modelFingerPointing_R;
297 // RiggedFinger_R_Thumb.modelPalmFacing = modelPalmFacing_R;
298 // }
299 // if (RiggedFinger_R_Index) {
300 // RiggedFinger_R_Index.modelFingerPointing = modelFingerPointing_R;
301 // RiggedFinger_R_Index.modelPalmFacing = modelPalmFacing_R;
302 // }
303 // if (RiggedFinger_R_Mid) {
304 // RiggedFinger_R_Mid.modelFingerPointing = modelFingerPointing_R;
305 // RiggedFinger_R_Mid.modelPalmFacing = modelPalmFacing_R;
306 // }
307 // if (RiggedFinger_R_Ring) {
308 // RiggedFinger_R_Ring.modelFingerPointing = modelFingerPointing_R;
309 // RiggedFinger_R_Ring.modelPalmFacing = modelPalmFacing_R;
310 // }
311 // if (RiggedFinger_R_Pinky) {
312 // RiggedFinger_R_Pinky.modelFingerPointing = modelFingerPointing_R;
313 // RiggedFinger_R_Pinky.modelPalmFacing = modelPalmFacing_R;
314 // }
315 // }
316
317 // //Monobehavior's OnValidate() is used to push LeapHandsAutoRig values to RiggedHand and RiggedFinger components
318 // void OnValidate() {
319 // if (FlipPalms != flippedPalmsState) {
320 // modelPalmFacing_L = modelPalmFacing_L * -1f;
321 // modelPalmFacing_R = modelPalmFacing_R * -1f;
322 // flippedPalmsState = FlipPalms;
323 // PushVectorValues();
324 // }
325 // }
326 // /**Removes the ModelGroup from HandPool that corresponds to this instance of LeapHandsAutoRig */
327 // void OnDestroy() {
328 // if (HandPoolToPopulate != null) {
329 // HandPoolToPopulate.RemoveGroup(ModelGroupName);
330 // }
331 // }
332
333 }
334
335}
The HandModelManager manages a pool of HandModelBases and makes HandRepresentations when a it detects...
A component to be attached to a HandModelBase to handle starting and ending of tracking....
HandTransitionBehavior HandTransitionBehavior_R
HandTransitionBehavior HandTransitionBehavior_L
Manages the position and orientation of the bones in a model rigged for skeletal animation.
Definition: RiggedFinger.cs:22
A skinned and jointed 3D HandModel.
Definition: RiggedHand.cs:20