Tools.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using Autodesk.Max;
  6. using Autodesk.Max.IMXSDebugger;
  7. using MaxSharp;
  8. namespace Max2Babylon
  9. {
  10. public static class Tools
  11. {
  12. public static float[] ToArray(this IQuat value)
  13. {
  14. return new[] { value.X, value.Z, value.Y, value.W };
  15. }
  16. public static float[] Scale(this Color value, float scale)
  17. {
  18. return new[] { value.r * scale, value.g * scale, value.b * scale };
  19. }
  20. public static float[] ToArray(this Color value)
  21. {
  22. return new[] { value.r, value.g, value.b };
  23. }
  24. public static float[] ToArray(this IPoint3 value)
  25. {
  26. return new[] { value.X, value.Y, value.Z };
  27. }
  28. public static float[] ToArray(this IPoint2 value)
  29. {
  30. return new[] { value.X, value.Y };
  31. }
  32. public static float[] ToArraySwitched(this IPoint3 value)
  33. {
  34. return new[] { value.X, value.Z, value.Y };
  35. }
  36. public static float[] ToArray(this IColor value)
  37. {
  38. return new[] { value.R, value.G, value.B };
  39. }
  40. public static IEnumerable<Node> NodesListBySuperClass(this Scene scene, SuperClassID sid)
  41. {
  42. return from n in scene.NodeTree where n.Object != null && n.Object.SuperClassID == sid select n;
  43. }
  44. public static float ConvertFov(float fov)
  45. {
  46. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  47. }
  48. public static bool HasParent(this Node node)
  49. {
  50. return node.Parent != null && node.Parent.Object != null;
  51. }
  52. public static Guid GetGuid(this Animatable node)
  53. {
  54. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  55. var uidData = appData.GetChunk(0);
  56. Guid uid;
  57. if (uidData != null)
  58. {
  59. uid = new Guid(uidData);
  60. }
  61. else
  62. {
  63. uid = Guid.NewGuid();
  64. appData.AddChunk(0, uid.ToByteArray());
  65. }
  66. return uid;
  67. }
  68. public static Guid GetGuid(this IINode node)
  69. {
  70. return GetGuid(Animatable.CreateWrapper<Node>(node));
  71. }
  72. public static string GetLocalData(this Node node)
  73. {
  74. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  75. var uidData = appData.GetChunk(1);
  76. if (uidData != null)
  77. {
  78. return System.Text.Encoding.UTF8.GetString(uidData);
  79. }
  80. return "";
  81. }
  82. public static void SetLocalData(this Node node, string value)
  83. {
  84. var appData = node.GetAppData(new ClassID(Loader.Class_ID), SuperClassID.BaseNode);
  85. var uidData = appData.GetChunk(1);
  86. if (uidData != null)
  87. {
  88. appData.RemoveChunk(1);
  89. }
  90. appData.AddChunk(1, System.Text.Encoding.UTF8.GetBytes(value));
  91. }
  92. public static IMatrix3 GetWorldMatrix(this Node node, TimeValue t, bool parent)
  93. {
  94. var innerNode = node._Node;
  95. var tm = innerNode.GetNodeTM(t, Interval.Forever._IInterval);
  96. var ptm = innerNode.ParentNode.GetNodeTM(t, Interval.Forever._IInterval);
  97. if (!parent)
  98. return tm;
  99. if (innerNode.ParentNode.SuperClassID == SuperClassID.Camera)
  100. {
  101. var r = ptm.GetRow(3);
  102. ptm.IdentityMatrix();
  103. ptm.SetRow(3, r);
  104. }
  105. ptm.Invert();
  106. return tm.Multiply(ptm);
  107. }
  108. public static IMesh GetMesh(this IObject obj)
  109. {
  110. if (obj.CanConvertToType(ClassID.TriObject._IClass_ID) == 0)
  111. return null;
  112. var tri = obj.ConvertToType(0, ClassID.TriObject._IClass_ID) as ITriObject;
  113. return tri == null ? null : tri.Mesh;
  114. }
  115. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  116. {
  117. if (Math.Abs(current.X - other.X) > epsilon)
  118. {
  119. return false;
  120. }
  121. if (Math.Abs(current.Y - other.Y) > epsilon)
  122. {
  123. return false;
  124. }
  125. if (Math.Abs(current.Z - other.Z) > epsilon)
  126. {
  127. return false;
  128. }
  129. return true;
  130. }
  131. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  132. {
  133. if (Math.Abs(current.X - other.X) > epsilon)
  134. {
  135. return false;
  136. }
  137. if (Math.Abs(current.Y - other.Y) > epsilon)
  138. {
  139. return false;
  140. }
  141. return true;
  142. }
  143. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  144. {
  145. int state = defaultState;
  146. node.GetUserPropBool(ref propertyName, ref state);
  147. return state == 1;
  148. }
  149. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  150. {
  151. float state = defaultState;
  152. node.GetUserPropFloat(ref propertyName, ref state);
  153. return state;
  154. }
  155. public static float[] GetVector3Property(this IINode node, string propertyName)
  156. {
  157. float state0 = 0;
  158. string name = propertyName + "_x";
  159. node.GetUserPropFloat(ref name, ref state0);
  160. float state1 = 0;
  161. name = propertyName + "_y";
  162. node.GetUserPropFloat(ref name, ref state1);
  163. float state2 = 0;
  164. name = propertyName + "_z";
  165. node.GetUserPropFloat(ref name, ref state2);
  166. return new[] { state0, state1, state2 };
  167. }
  168. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  169. {
  170. checkBox.CheckState = CheckState.Indeterminate;
  171. foreach (var node in nodes)
  172. {
  173. var state = node.GetBoolProperty(propertyName, defaultState);
  174. if (checkBox.CheckState == CheckState.Indeterminate)
  175. {
  176. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  177. }
  178. else
  179. {
  180. if (!state && checkBox.CheckState == CheckState.Checked ||
  181. state && checkBox.CheckState == CheckState.Unchecked)
  182. {
  183. checkBox.CheckState = CheckState.Indeterminate;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  190. {
  191. foreach (var node in nodes)
  192. {
  193. if (checkBox.CheckState != CheckState.Indeterminate)
  194. {
  195. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  196. }
  197. }
  198. }
  199. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  200. {
  201. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  202. }
  203. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  204. {
  205. foreach (var node in nodes)
  206. {
  207. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  208. }
  209. }
  210. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  211. {
  212. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  213. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  214. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  215. }
  216. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  217. {
  218. string name = propertyName + "_x";
  219. node.SetUserPropFloat(ref name, vector3Control.X);
  220. name = propertyName + "_y";
  221. node.SetUserPropFloat(ref name, vector3Control.Y);
  222. name = propertyName + "_z";
  223. node.SetUserPropFloat(ref name, vector3Control.Z);
  224. }
  225. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  226. {
  227. foreach (var node in nodes)
  228. {
  229. UpdateVector3Control(vector3Control, node, propertyName);
  230. }
  231. }
  232. }
  233. }