OgreSkeletonLib_usefulfns.ms 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ----------------------------------------------------------------------------------------
  2. -- ------------------------------ SOME USEFUL FUNCTIONS ----------------------------- --
  3. ----------------------------------------------------------------------------------------
  4. -- global variables
  5. global g_MAX = false;
  6. global g_MAX_use_listener = false;
  7. -- global structures
  8. struct exportOptions (sampleRate, ikSampleRate, scale, flipyz, flipNormal, exportColours, exportUV, UVchannels, exportHelpers);
  9. struct exportAnims (names, startframes, endframes, lengths) ;
  10. global Anims;
  11. global Options;
  12. global OgreExportFloater;
  13. global OgreExportOptions, OgreExportObject, OgreExportMesh, OgreExportAnimation, OgreExportMaterial, OgreExportAbout;
  14. --------------------------------------------------------------------
  15. -- compute the transform, if you want to flip Y and Z axis,
  16. -- because the axis which 'defines height' in OGRE is the Y axis,
  17. -- whereas 3dsmax uses the Z one.
  18. --------------------------------------------------------------------
  19. function flipYZTransform Tform = (
  20. local axis1,axis2,axis3,t,m
  21. -- computes the matrix
  22. axis1 = point3 1 0 0 ;
  23. axis2 = point3 0 0 1 ;
  24. axis3 = point3 0 -1 0 ;
  25. t = point3 0 0 0 ;
  26. m=matrix3 axis1 axis2 axis3 t ;
  27. -- multiplies by the inverse
  28. Tform = Tform*inverse(m) ;
  29. return Tform ;
  30. )
  31. -----------------------------------------------------------------------------
  32. -- check if the bone is the root object of the biped
  33. -----------------------------------------------------------------------------
  34. function isPelvis bipObj =
  35. (
  36. if (bipObj == undefined) then return false ;
  37. if (classof bipObj != Biped_Object) then return false;
  38. return ((biped.getNode bipObj 13) == bipObj) ;
  39. )
  40. -----------------------------------------------------------------------------
  41. -- check if the bone is the footstep object of the biped
  42. -----------------------------------------------------------------------------
  43. function isFootStep bipObj =
  44. (
  45. if (bipObj == undefined) then return false ;
  46. if (classof bipObj != Biped_Object) then return false;
  47. return ((biped.getNode bipObj 16) == bipObj) ;
  48. )
  49. --------------------------------------------------------------------
  50. -- returns if the bone is the root or not,
  51. -- ia if its parent is undefined or is not a bone.
  52. --------------------------------------------------------------------
  53. function isRoot b = (
  54. if (b.parent==undefined or not (iskindof b.parent BoneGeometry or iskindOf b.parent Biped_Object) ) then
  55. return true ;
  56. else
  57. return false ;
  58. )
  59. --------------------------------------------------------------------
  60. -- returns if the bone is the root or not,
  61. -- handles standard bones as well as biped
  62. --------------------------------------------------------------------
  63. function isRootUniversal b = (
  64. if (isRoot b) then
  65. return true;
  66. else if (isPelvis b) then
  67. return true;
  68. else
  69. return false;
  70. )
  71. --------------------------------------------------------------------
  72. -- returns if the object is a root or not,
  73. -- handles any kind oj object
  74. --------------------------------------------------------------------
  75. function isRootUniversal2 b = (
  76. bname = replaceSpaces b.name;
  77. ind = (findItem RootsList bname);
  78. if (ind == 0) then
  79. return false;
  80. else
  81. return true;
  82. )
  83. --------------------------------------------------------------------
  84. -- returns if the object is part of the skin (or physique modifier)
  85. --------------------------------------------------------------------
  86. function isPartOfModifier b sk phy = (
  87. name = replaceSpaces b.name ;
  88. if (sk!=undefined) then
  89. (
  90. for i=1 to (skinOps.GetNumberBones sk) do
  91. (
  92. bname = skinOps.GetBoneName sk i 1 ;
  93. replaceSpaces bname ;
  94. if (name == bname) then
  95. return true;
  96. )
  97. )
  98. else if (phy!=undefined) then
  99. (
  100. for i=1 to (physiqueOps.GetBoneCount $) do
  101. (
  102. bname = (physiqueOps.GetBones $)[i].name;
  103. replaceSpaces bname ;
  104. if (name == bname) then
  105. return true;
  106. )
  107. )
  108. return false;
  109. )
  110. --------------------------------------------------------------------
  111. -- creates a new array (which must be set up as an array before
  112. -- calling this function) in which there isn't the same element.
  113. -- Moreover, the array is sorted.
  114. --------------------------------------------------------------------
  115. function keepLoneValues a b= (
  116. local e, last_e ;
  117. sort a ;
  118. last_e = undefined ;
  119. for e in a do (
  120. if (e!=last_e) then
  121. append b e ;
  122. last_e = e ;
  123. )
  124. )
  125. ---------------------------------------------------------------------
  126. -- replaces " " by "_" in a string.
  127. -- when a name is for example Left Biceps max knows it at Left_Biceps
  128. -- and execute function will not work if you don't use this function
  129. ---------------------------------------------------------------------
  130. function replaceSpaces s =
  131. (
  132. for i=1 to s.count do
  133. (
  134. if (s[i] == " ") then
  135. s[i] = "_" ;
  136. )
  137. s ;
  138. )
  139. --------------------------------
  140. -- return the length of an array
  141. --------------------------------
  142. function arrayLength a =
  143. (
  144. local i ;
  145. i = 1 ;
  146. while (a[i] != undefined) do
  147. i = i + 1 ;
  148. i-1 ;
  149. )
  150. -----------------------------------------------------------------------------
  151. -- return the skin modifier or undefined if object don't have a skin modifier
  152. -----------------------------------------------------------------------------
  153. function getSkin obj =
  154. (
  155. local s,i ;
  156. s = undefined ;
  157. if obj != undefined then
  158. for i in obj.modifiers do
  159. (
  160. if iskindof i Skin do
  161. s = i ;
  162. )
  163. s ;
  164. )
  165. -----------------------------------------------------------------------------
  166. -- return the physique modifier or undefined if object don't have it
  167. -----------------------------------------------------------------------------
  168. function getPhysique obj =
  169. (
  170. local s,i ;
  171. s = undefined ;
  172. if obj != undefined then
  173. for i in obj.modifiers do
  174. (
  175. if iskindof i Physique do
  176. s = i ;
  177. )
  178. s ;
  179. )
  180. -----------------------------------------------------------------------------
  181. -- return the OctopusExport modifier or undefined if object don't have it
  182. -----------------------------------------------------------------------------
  183. function getOctopusExport obj =
  184. (
  185. local s,i ;
  186. s = undefined ;
  187. if obj != undefined then
  188. for i in obj.modifiers do
  189. (
  190. if iskindof i OctopusMeshModifier do
  191. s = i ;
  192. )
  193. s ;
  194. )
  195. --------------------------------------------------
  196. -- return an Array with the root bones of the skin
  197. --------------------------------------------------
  198. function getRoots skin =
  199. (
  200. local rootstab,n,i,c,d ;
  201. rootstab = #() ;
  202. n = skinOps.GetNumberBones skin ;
  203. for i = 1 to n do
  204. (
  205. c= skinOps.GetBoneName skin i 1 ;
  206. replaceSpaces c ;
  207. d = getNodeByName c ;
  208. if (isRoot d) then
  209. append rootstab d ;
  210. )
  211. rootstab ;
  212. )
  213. --------------------------------------------------------
  214. -- return an Array with the ID of root bones of the skin
  215. --------------------------------------------------------
  216. function getRootsId skin =
  217. (
  218. local rootstab,n,i,c,d ;
  219. rootstab = #() ;
  220. n = skinOps.GetNumberBones skin ;
  221. for i = 1 to n do
  222. (
  223. c= skinOps.GetBoneName skin i 1 ;
  224. replaceSpaces c ;
  225. d = getNodeByName c ;
  226. if (isRoot d) then
  227. append rootstab i ;
  228. )
  229. rootstab ;
  230. )
  231. -------------------------------------------------------
  232. -- return a angleAxis given a Quaternion
  233. -------------------------------------------------------
  234. function toAngleAxis q =
  235. (
  236. local angle, axis;
  237. local result;
  238. fSqrLength = q.x*q.x+q.y*q.y+q.z*q.z ;
  239. if (fSqrLength > 0.0) then
  240. (
  241. angle = ((acos q.w) * pi / 90);
  242. fInvLength = 1.0 / (sqrt fSqrLength);
  243. axis = [q.x*fInvLength, q.y*fInvLength, q.z*fInvLength];
  244. result = angleAxis angle axis;
  245. )
  246. else
  247. (
  248. angle = 0;
  249. axis = [1,0,0];
  250. result = angleAxis angle axis;
  251. )
  252. return result;
  253. )