Tools.cs 30 KB

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