Tools.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using Autodesk.Max;
  8. using BabylonExport.Entities;
  9. using SharpDX;
  10. using System.Reflection;
  11. namespace Max2Babylon
  12. {
  13. public static class Tools
  14. {
  15. public static IntPtr GetNativeHandle(this INativeObject obj)
  16. {
  17. #if MAX2015 || MAX2016
  18. return obj.NativePointer;
  19. #else
  20. return obj.Handle;
  21. #endif
  22. }
  23. static Assembly GetWrappersAssembly()
  24. {
  25. return Assembly.Load("Autodesk.Max.Wrappers, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
  26. }
  27. public static IIGameCamera AsGameCamera(this IIGameObject obj)
  28. {
  29. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameCamera");
  30. var constructor = type.GetConstructors()[0];
  31. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  32. unsafe
  33. {
  34. var voidPtr = obj.GetNativeHandle().ToPointer();
  35. return (IIGameCamera)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  36. }
  37. }
  38. public static IIGameMesh AsGameMesh(this IIGameObject obj)
  39. {
  40. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameMesh");
  41. var constructor = type.GetConstructors()[0];
  42. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  43. unsafe
  44. {
  45. var voidPtr = obj.GetNativeHandle().ToPointer();
  46. return (IIGameMesh)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  47. }
  48. }
  49. public static IIGameLight AsGameLight(this IIGameObject obj)
  50. {
  51. var type = GetWrappersAssembly().GetType("Autodesk.Max.Wrappers.IGameLight");
  52. var constructor = type.GetConstructors()[0];
  53. // var pointerType = GetWrappersAssembly().GetType("IGameCamera");
  54. unsafe
  55. {
  56. var voidPtr = obj.GetNativeHandle().ToPointer();
  57. return (IIGameLight)constructor.Invoke(new object[] { obj.GetNativeHandle(), false });
  58. }
  59. }
  60. public const float Epsilon = 0.001f;
  61. public static IPoint3 XAxis { get { return Loader.Global.Point3.Create(1, 0, 0); } }
  62. public static IPoint3 YAxis { get { return Loader.Global.Point3.Create(0, 1, 0); } }
  63. public static IPoint3 ZAxis { get { return Loader.Global.Point3.Create(0, 0, 1); } }
  64. public static IPoint3 Origin { get { return Loader.Global.Point3.Create(0, 0, 0); } }
  65. public static IInterval Forever
  66. {
  67. get { return Loader.Global.Interval.Create(int.MinValue, int.MaxValue); }
  68. }
  69. public static IMatrix3 Identity { get { return Loader.Global.Matrix3.Create(XAxis, YAxis, ZAxis, Origin); } }
  70. #if !MAX2015 && !MAX2016
  71. unsafe public static int GetParamBlockIndex(IIParamBlock paramBlock, string name)
  72. {
  73. for (short index = 0; index < paramBlock.NumParams; index++)
  74. {
  75. IGetParamName gpn = Loader.Global.GetParamName.Create("", index);
  76. paramBlock.NotifyDependents(Tools.Forever, (UIntPtr)gpn.Handle.ToPointer(), RefMessage.GetParamName, (SClass_ID)0xfffffff0, false, null);
  77. if (gpn.Name == name)
  78. {
  79. return index;
  80. }
  81. }
  82. return -1;
  83. }
  84. public static int GetParamBlockValueInt(IIParamBlock paramBlock, string name)
  85. {
  86. var index = Tools.GetParamBlockIndex(paramBlock, name);
  87. if (index == -1)
  88. {
  89. return 0;
  90. }
  91. return paramBlock.GetInt(index, 0);
  92. }
  93. public static float GetParamBlockValueFloat(IIParamBlock paramBlock, string name)
  94. {
  95. var index = Tools.GetParamBlockIndex(paramBlock, name);
  96. if (index == -1)
  97. {
  98. return 0;
  99. }
  100. return paramBlock.GetFloat(index, 0);
  101. }
  102. public static float[] GetParamBlockValueColor(IIParamBlock paramBlock, string name)
  103. {
  104. var index = Tools.GetParamBlockIndex(paramBlock, name);
  105. if (index == -1)
  106. {
  107. return null;
  108. }
  109. return paramBlock.GetColor(index, 0).ToArray();
  110. }
  111. #endif
  112. public static Vector3 ToEulerAngles(this IQuat q)
  113. {
  114. // Store the Euler angles in radians
  115. var pitchYawRoll = new Vector3();
  116. double sqw = q.W * q.W;
  117. double sqx = q.X * q.X;
  118. double sqy = q.Y * q.Y;
  119. double sqz = q.Z * q.Z;
  120. // If quaternion is normalised the unit is one, otherwise it is the correction factor
  121. double unit = sqx + sqy + sqz + sqw;
  122. double test = q.X * q.Y + q.Z * q.W;
  123. if (test > 0.4999f * unit) // 0.4999f OR 0.5f - EPSILON
  124. {
  125. // Singularity at north pole
  126. pitchYawRoll.Y = 2f * (float)Math.Atan2(q.X, q.W); // Yaw
  127. pitchYawRoll.X = (float)Math.PI * 0.5f; // Pitch
  128. pitchYawRoll.Z = 0f; // Roll
  129. return pitchYawRoll;
  130. }
  131. if (test < -0.4999f * unit) // -0.4999f OR -0.5f + EPSILON
  132. {
  133. // Singularity at south pole
  134. pitchYawRoll.Y = -2f * (float)Math.Atan2(q.X, q.W); // Yaw
  135. pitchYawRoll.X = -(float)Math.PI * 0.5f; // Pitch
  136. pitchYawRoll.Z = 0f; // Roll
  137. return pitchYawRoll;
  138. }
  139. pitchYawRoll.Y = (float)Math.Atan2(2f * q.Y * q.W - 2f * q.X * q.Z, sqx - sqy - sqz + sqw); // Yaw
  140. pitchYawRoll.X = (float)Math.Asin(2f * test / unit); // Pitch
  141. pitchYawRoll.Z = (float)Math.Atan2(2f * q.X * q.W - 2f * q.Y * q.Z, -sqx + sqy - sqz + sqw); // Roll
  142. return pitchYawRoll;
  143. }
  144. public static float[] ToArray(this IGMatrix gmat)
  145. {
  146. //float eulX =0, eulY=0, eulZ=0;
  147. //unsafe
  148. //{
  149. // gmat.Rotation.GetEuler( new IntPtr(&eulX), new IntPtr(&eulY), new IntPtr(&eulZ));
  150. //}
  151. //return (Matrix.Scaling(gmat.Scaling.X, gmat.Scaling.Y, gmat.Scaling.Z) * Matrix.RotationYawPitchRoll(eulY, eulX, eulZ) * Matrix.Translation(gmat.Translation.X, gmat.Translation.Y, gmat.Translation.Z)).ToArray();
  152. var r0 = gmat.GetRow(0);
  153. var r1 = gmat.GetRow(1);
  154. var r2 = gmat.GetRow(2);
  155. var r3 = gmat.GetRow(3);
  156. return new float[] {r0.X, r0.Y, r0.Z, r0.W,
  157. r1.X, r1.Y,r1.Z, r1.W,
  158. r2.X, r2.Y,r2.Z, r2.W,
  159. r3.X, r3.Y,r3.Z, r3.W,};
  160. }
  161. public static void PreparePipeline(IINode node, bool deactivate)
  162. {
  163. var obj = node.ObjectRef;
  164. if (obj == null || obj.SuperClassID != SClass_ID.GenDerivob)
  165. {
  166. return;
  167. }
  168. var derivedObject = obj as IIDerivedObject;
  169. if (derivedObject == null)
  170. {
  171. return;
  172. }
  173. for (var index = 0; index < derivedObject.NumModifiers; index++)
  174. {
  175. var modifier = derivedObject.GetModifier(index);
  176. //if (modifier.ClassID.PartA == 9815843 && modifier.ClassID.PartB == 87654) // Skin
  177. //{
  178. // if (deactivate)
  179. // {
  180. // modifier.DisableMod();
  181. // }
  182. // else
  183. // {
  184. // modifier.EnableMod();
  185. // }
  186. //}
  187. }
  188. }
  189. public static VNormal[] ComputeNormals(IMesh mesh, bool optimize)
  190. {
  191. var vnorms = new VNormal[mesh.NumVerts];
  192. var fnorms = new Vector3[mesh.NumFaces];
  193. for (var index = 0; index < mesh.NumVerts; index++)
  194. {
  195. vnorms[index] = new VNormal();
  196. }
  197. for (var index = 0; index < mesh.NumFaces; index++)
  198. {
  199. var face = mesh.Faces[index];
  200. Vector3 v0 = mesh.Verts[(int)face.V[0]].ToVector3();
  201. Vector3 v1 = mesh.Verts[(int)face.V[1]].ToVector3();
  202. Vector3 v2 = mesh.Verts[(int)face.V[2]].ToVector3();
  203. fnorms[index] = Vector3.Cross((v1 - v0), (v2 - v1));
  204. for (var j = 0; j < 3; j++)
  205. {
  206. vnorms[(int)face.V[j]].AddNormal(fnorms[index], optimize ? 1 : face.SmGroup);
  207. }
  208. fnorms[index].Normalize();
  209. }
  210. for (var index = 0; index < mesh.NumVerts; index++)
  211. {
  212. vnorms[index].Normalize();
  213. }
  214. return vnorms;
  215. }
  216. public static bool IsEqualTo(this float[] value, float[] other)
  217. {
  218. if (value.Length != other.Length)
  219. {
  220. return false;
  221. }
  222. return !value.Where((t, i) => Math.Abs(t - other[i]) > Epsilon).Any();
  223. }
  224. public static float[] ToArray(this IMatrix3 value)
  225. {
  226. var row0 = value.GetRow(0).ToArraySwitched();
  227. var row1 = value.GetRow(1).ToArraySwitched();
  228. var row2 = value.GetRow(2).ToArraySwitched();
  229. var row3 = value.GetRow(3).ToArraySwitched();
  230. return new[]
  231. {
  232. row0[0], row0[1], row0[2], 0,
  233. row2[0], row2[1], row2[2], 0,
  234. row1[0], row1[1], row1[2], 0,
  235. row3[0], row3[1], row3[2], 1
  236. };
  237. }
  238. public static IPoint3 ToPoint3(this Vector3 value)
  239. {
  240. return Loader.Global.Point3.Create(value.X, value.Y, value.Z);
  241. }
  242. public static Vector3 ToVector3(this IPoint3 value)
  243. {
  244. return new Vector3(value.X, value.Y, value.Z);
  245. }
  246. public static Quaternion ToQuat(this IQuat value)
  247. {
  248. return new Quaternion(value.X, value.Z, value.Y, value.W);
  249. }
  250. public static float[] ToArray(this IQuat value)
  251. {
  252. return new[] { value.X, value.Z, value.Y, value.W };
  253. }
  254. public static float[] Scale(this IColor value, float scale)
  255. {
  256. return new[] { value.R * scale, value.G * scale, value.B * scale };
  257. }
  258. public static float[] ToArray(this IPoint4 value)
  259. {
  260. return new[] { value.X, value.Y, value.Z, value.W };
  261. }
  262. public static float[] ToArray(this IPoint3 value)
  263. {
  264. return new[] { value.X, value.Y, value.Z };
  265. }
  266. public static float[] ToArray(this IPoint2 value)
  267. {
  268. return new[] { value.X, value.Y };
  269. }
  270. public static float[] ToArraySwitched(this IPoint2 value)
  271. {
  272. return new[] { value.X, 1.0f - value.Y };
  273. }
  274. public static float[] ToArraySwitched(this IPoint3 value)
  275. {
  276. return new[] { value.X, value.Z, value.Y };
  277. }
  278. public static float[] ToArray(this IColor value)
  279. {
  280. return new[] { value.R, value.G, value.B };
  281. }
  282. public static IEnumerable<IINode> Nodes(this IINode node)
  283. {
  284. for (int i = 0; i < node.NumberOfChildren; ++i)
  285. if (node.GetChildNode(i) != null)
  286. yield return node.GetChildNode(i);
  287. }
  288. public static IEnumerable<IINode> NodeTree(this IINode node)
  289. {
  290. foreach (var x in node.Nodes())
  291. {
  292. yield return x;
  293. foreach (var y in x.NodeTree())
  294. yield return y;
  295. }
  296. }
  297. public static IEnumerable<IINode> NodesListBySuperClass(this IINode rootNode, SClass_ID sid)
  298. {
  299. return from n in rootNode.NodeTree() where n.ObjectRef != null && n.EvalWorldState(0, false).Obj.SuperClassID == sid select n;
  300. }
  301. public static IEnumerable<IINode> NodesListBySuperClasses(this IINode rootNode, SClass_ID[] sids)
  302. {
  303. return from n in rootNode.NodeTree() where n.ObjectRef != null && sids.Any(sid => n.EvalWorldState(0, false).Obj.SuperClassID == sid) select n;
  304. }
  305. public static float ConvertFov(float fov)
  306. {
  307. return (float)(2.0f * Math.Atan(Math.Tan(fov / 2.0f) / Loader.Core.ImageAspRatio));
  308. }
  309. public static bool HasParent(this IINode node)
  310. {
  311. return node.ParentNode != null && node.ParentNode.ObjectRef != null;
  312. }
  313. public static bool IsInstance(this IAnimatable node)
  314. {
  315. var data = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  316. if (data != null)
  317. {
  318. return data.Data[0] != 0;
  319. }
  320. return false;
  321. }
  322. public static void MarkAsInstance(this IAnimatable node)
  323. {
  324. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1, new byte[] { 1 });
  325. }
  326. public static Guid GetGuid(this IAnimatable node)
  327. {
  328. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 0);
  329. Guid uid;
  330. if (uidData != null)
  331. {
  332. uid = new Guid(uidData.Data);
  333. }
  334. else
  335. {
  336. uid = Guid.NewGuid();
  337. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 0, uid.ToByteArray());
  338. }
  339. return uid;
  340. }
  341. public static string GetLocalData(this IAnimatable node)
  342. {
  343. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  344. if (uidData != null)
  345. {
  346. return System.Text.Encoding.UTF8.GetString(uidData.Data);
  347. }
  348. return "";
  349. }
  350. public static void SetLocalData(this IAnimatable node, string value)
  351. {
  352. var uidData = node.GetAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  353. if (uidData != null)
  354. {
  355. node.RemoveAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1);
  356. }
  357. node.AddAppDataChunk(Loader.Class_ID, SClass_ID.Basenode, 1, System.Text.Encoding.UTF8.GetBytes(value));
  358. }
  359. public static IMatrix3 GetWorldMatrix(this IINode node, int t, bool parent)
  360. {
  361. var tm = node.GetNodeTM(t, Forever);
  362. var ptm = node.ParentNode.GetNodeTM(t, Forever);
  363. if (!parent)
  364. return tm;
  365. if (node.ParentNode.SuperClassID == SClass_ID.Camera)
  366. {
  367. var r = ptm.GetRow(3);
  368. ptm.IdentityMatrix();
  369. ptm.SetRow(3, r);
  370. }
  371. ptm.Invert();
  372. return tm.Multiply(ptm);
  373. }
  374. public static IMatrix3 GetWorldMatrixComplete(this IINode node, int t, bool parent)
  375. {
  376. var tm = node.GetObjTMAfterWSM(t, Forever);
  377. var ptm = node.ParentNode.GetObjTMAfterWSM(t, Forever);
  378. if (!parent)
  379. return tm;
  380. if (node.ParentNode.SuperClassID == SClass_ID.Camera)
  381. {
  382. var r = ptm.GetRow(3);
  383. ptm.IdentityMatrix();
  384. ptm.SetRow(3, r);
  385. }
  386. ptm.Invert();
  387. return tm.Multiply(ptm);
  388. }
  389. public static ITriObject GetMesh(this IObject obj)
  390. {
  391. var triObjectClassId = Loader.Global.Class_ID.Create(0x0009, 0);
  392. if (obj.CanConvertToType(triObjectClassId) == 0)
  393. return null;
  394. return obj.ConvertToType(0, triObjectClassId) as ITriObject;
  395. }
  396. public static bool IsAlmostEqualTo(this float[] current, float[] other, float epsilon)
  397. {
  398. if (current == null && other == null)
  399. {
  400. return true;
  401. }
  402. if (current == null || other == null)
  403. {
  404. return false;
  405. }
  406. if (current.Length != other.Length)
  407. {
  408. return false;
  409. }
  410. for (var i = 0; i < current.Length; ++i)
  411. {
  412. if (Math.Abs(current[i] - other[i]) > epsilon)
  413. {
  414. return false;
  415. }
  416. }
  417. return true;
  418. }
  419. public static bool IsAlmostEqualTo(this IPoint4 current, IPoint4 other, float epsilon)
  420. {
  421. if (Math.Abs(current.X - other.X) > epsilon)
  422. {
  423. return false;
  424. }
  425. if (Math.Abs(current.Y - other.Y) > epsilon)
  426. {
  427. return false;
  428. }
  429. if (Math.Abs(current.Z - other.Z) > epsilon)
  430. {
  431. return false;
  432. }
  433. if (Math.Abs(current.W - other.W) > epsilon)
  434. {
  435. return false;
  436. }
  437. return true;
  438. }
  439. public static bool IsAlmostEqualTo(this IPoint3 current, IPoint3 other, float epsilon)
  440. {
  441. if (Math.Abs(current.X - other.X) > epsilon)
  442. {
  443. return false;
  444. }
  445. if (Math.Abs(current.Y - other.Y) > epsilon)
  446. {
  447. return false;
  448. }
  449. if (Math.Abs(current.Z - other.Z) > epsilon)
  450. {
  451. return false;
  452. }
  453. return true;
  454. }
  455. public static bool IsAlmostEqualTo(this IPoint2 current, IPoint2 other, float epsilon)
  456. {
  457. if (Math.Abs(current.X - other.X) > epsilon)
  458. {
  459. return false;
  460. }
  461. if (Math.Abs(current.Y - other.Y) > epsilon)
  462. {
  463. return false;
  464. }
  465. return true;
  466. }
  467. public static void SetStringProperty(this IINode node, string propertyName, string defaultState)
  468. {
  469. string state = defaultState;
  470. #if MAX2015 || MAX2016
  471. node.SetUserPropString(propertyName, state);
  472. #else
  473. node.SetUserPropString(ref propertyName, ref state);
  474. #endif
  475. }
  476. public static bool GetBoolProperty(this IINode node, string propertyName, int defaultState = 0)
  477. {
  478. int state = defaultState;
  479. #if MAX2015 || MAX2016
  480. node.GetUserPropBool(propertyName, ref state);
  481. #else
  482. node.GetUserPropBool(ref propertyName, ref state);
  483. #endif
  484. return state == 1;
  485. }
  486. public static string GetStringProperty(this IINode node, string propertyName, string defaultState)
  487. {
  488. string state = defaultState;
  489. #if MAX2015 || MAX2016
  490. node.GetUserPropString(propertyName, ref state);
  491. #else
  492. node.GetUserPropString(ref propertyName, ref state);
  493. #endif
  494. return state;
  495. }
  496. public static float GetFloatProperty(this IINode node, string propertyName, float defaultState = 0)
  497. {
  498. float state = defaultState;
  499. #if MAX2015 || MAX2016
  500. node.GetUserPropFloat(propertyName, ref state);
  501. #else
  502. node.GetUserPropFloat(ref propertyName, ref state);
  503. #endif
  504. return state;
  505. }
  506. public static float[] GetVector3Property(this IINode node, string propertyName)
  507. {
  508. float state0 = 0;
  509. string name = propertyName + "_x";
  510. #if MAX2015 || MAX2016
  511. node.GetUserPropFloat(name, ref state0);
  512. #else
  513. node.GetUserPropFloat(ref name, ref state0);
  514. #endif
  515. float state1 = 0;
  516. name = propertyName + "_y";
  517. #if MAX2015 || MAX2016
  518. node.GetUserPropFloat(name, ref state1);
  519. #else
  520. node.GetUserPropFloat(ref name, ref state1);
  521. #endif
  522. float state2 = 0;
  523. name = propertyName + "_z";
  524. #if MAX2015 || MAX2016
  525. node.GetUserPropFloat(name, ref state2);
  526. #else
  527. node.GetUserPropFloat(ref name, ref state2);
  528. #endif
  529. return new[] { state0, state1, state2 };
  530. }
  531. public static bool PrepareCheckBox(CheckBox checkBox, IINode node, string propertyName, int defaultState = 0)
  532. {
  533. var state = node.GetBoolProperty(propertyName, defaultState);
  534. if (checkBox.CheckState == CheckState.Indeterminate)
  535. {
  536. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  537. }
  538. else
  539. {
  540. if (checkBox.ThreeState)
  541. {
  542. if (!state && checkBox.CheckState == CheckState.Checked ||
  543. state && checkBox.CheckState == CheckState.Unchecked)
  544. {
  545. checkBox.CheckState = CheckState.Indeterminate;
  546. return true;
  547. }
  548. }
  549. else
  550. {
  551. checkBox.CheckState = state ? CheckState.Checked : CheckState.Unchecked;
  552. return true;
  553. }
  554. }
  555. return false;
  556. }
  557. public static void PrepareCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName, int defaultState = 0)
  558. {
  559. checkBox.CheckState = CheckState.Indeterminate;
  560. foreach (var node in nodes)
  561. {
  562. if (PrepareCheckBox(checkBox, node, propertyName, defaultState))
  563. {
  564. break;
  565. }
  566. }
  567. }
  568. public static void PrepareTextBox(TextBox textBox, IINode node, string propertyName, string defaultValue = "")
  569. {
  570. var state = node.GetStringProperty(propertyName, defaultValue);
  571. textBox.Text = state;
  572. }
  573. public static void PrepareComboBox(ComboBox comboBox, IINode node, string propertyName, string defaultValue)
  574. {
  575. comboBox.SelectedItem = node.GetStringProperty(propertyName, defaultValue);
  576. }
  577. public static void UpdateCheckBox(CheckBox checkBox, IINode node, string propertyName)
  578. {
  579. if (checkBox.CheckState != CheckState.Indeterminate)
  580. {
  581. #if MAX2015 || MAX2016
  582. node.SetUserPropBool(propertyName, checkBox.CheckState == CheckState.Checked);
  583. #else
  584. node.SetUserPropBool(ref propertyName, checkBox.CheckState == CheckState.Checked);
  585. #endif
  586. }
  587. }
  588. public static void UpdateCheckBox(CheckBox checkBox, List<IINode> nodes, string propertyName)
  589. {
  590. foreach (var node in nodes)
  591. {
  592. UpdateCheckBox(checkBox, node, propertyName);
  593. }
  594. }
  595. public static void UpdateTextBox(TextBox textBox, List<IINode> nodes, string propertyName)
  596. {
  597. foreach (var node in nodes)
  598. {
  599. var value = textBox.Text;
  600. #if MAX2015 || MAX2016
  601. node.SetUserPropString(propertyName, value);
  602. #else
  603. node.SetUserPropString(ref propertyName, ref value);
  604. #endif
  605. }
  606. }
  607. public static void PrepareNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName, float defaultState = 0)
  608. {
  609. nup.Value = (decimal)nodes[0].GetFloatProperty(propertyName, defaultState);
  610. }
  611. public static void UpdateNumericUpDown(NumericUpDown nup, List<IINode> nodes, string propertyName)
  612. {
  613. foreach (var node in nodes)
  614. {
  615. #if MAX2015 || MAX2016
  616. node.SetUserPropFloat(propertyName, (float)nup.Value);
  617. #else
  618. node.SetUserPropFloat(ref propertyName, (float)nup.Value);
  619. #endif
  620. }
  621. }
  622. public static void PrepareVector3Control(Vector3Control vector3Control, IINode node, string propertyName, float defaultX = 0, float defaultY = 0, float defaultZ = 0)
  623. {
  624. vector3Control.X = node.GetFloatProperty(propertyName + "_x", defaultX);
  625. vector3Control.Y = node.GetFloatProperty(propertyName + "_y", defaultY);
  626. vector3Control.Z = node.GetFloatProperty(propertyName + "_z", defaultZ);
  627. }
  628. public static void UpdateVector3Control(Vector3Control vector3Control, IINode node, string propertyName)
  629. {
  630. string name = propertyName + "_x";
  631. #if MAX2015 || MAX2016
  632. node.SetUserPropFloat(name, vector3Control.X);
  633. #else
  634. node.SetUserPropFloat(ref name, vector3Control.X);
  635. #endif
  636. name = propertyName + "_y";
  637. #if MAX2015 || MAX2016
  638. node.SetUserPropFloat(name, vector3Control.Y);
  639. #else
  640. node.SetUserPropFloat(ref name, vector3Control.Y);
  641. #endif
  642. name = propertyName + "_z";
  643. #if MAX2015 || MAX2016
  644. node.SetUserPropFloat(name, vector3Control.Z);
  645. #else
  646. node.SetUserPropFloat(ref name, vector3Control.Z);
  647. #endif
  648. }
  649. public static void UpdateVector3Control(Vector3Control vector3Control, List<IINode> nodes, string propertyName)
  650. {
  651. foreach (var node in nodes)
  652. {
  653. UpdateVector3Control(vector3Control, node, propertyName);
  654. }
  655. }
  656. public static void UpdateComboBox(ComboBox comboBox, IINode node, string propertyName)
  657. {
  658. var value = comboBox.SelectedItem.ToString();
  659. #if MAX2015 || MAX2016
  660. node.SetUserPropString(propertyName, value);
  661. #else
  662. node.SetUserPropString(ref propertyName, ref value);
  663. #endif
  664. }
  665. public static void UpdateComboBox(ComboBox comboBox, List<IINode> nodes, string propertyName)
  666. {
  667. foreach (var node in nodes)
  668. {
  669. UpdateComboBox(comboBox, node, propertyName);
  670. }
  671. }
  672. public static IMatrix3 ExtractCoordinates(IINode meshNode, BabylonAbstractMesh babylonMesh, bool exportQuaternionsInsteadOfEulers)
  673. {
  674. var wm = meshNode.GetWorldMatrix(0, meshNode.HasParent());
  675. babylonMesh.position = wm.Trans.ToArraySwitched();
  676. var parts = Loader.Global.AffineParts.Create();
  677. Loader.Global.DecompAffine(wm, parts);
  678. if (exportQuaternionsInsteadOfEulers)
  679. {
  680. babylonMesh.rotationQuaternion = parts.Q.ToArray();
  681. }
  682. else
  683. {
  684. var rotate = new float[3];
  685. IntPtr xPtr = Marshal.AllocHGlobal(sizeof(float));
  686. IntPtr yPtr = Marshal.AllocHGlobal(sizeof(float));
  687. IntPtr zPtr = Marshal.AllocHGlobal(sizeof(float));
  688. parts.Q.GetEuler(xPtr, yPtr, zPtr);
  689. Marshal.Copy(xPtr, rotate, 0, 1);
  690. Marshal.Copy(yPtr, rotate, 1, 1);
  691. Marshal.Copy(zPtr, rotate, 2, 1);
  692. var temp = rotate[1];
  693. rotate[0] = -rotate[0] * parts.F;
  694. rotate[1] = -rotate[2] * parts.F;
  695. rotate[2] = -temp * parts.F;
  696. babylonMesh.rotation = rotate;
  697. }
  698. babylonMesh.scaling = parts.K.ToArraySwitched();
  699. return wm;
  700. }
  701. }
  702. }