Prim_Animation_Compiler_1983.lsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Script Name: Prim_Animation_Compiler.lsl
  2. // Author: Ferd Frederix
  3. // This prim animator compiler is a very useful and easy to use script for Second Life and OpenSim. Setup is very simple. Just drop the two script into the object, click the object, and give your animation a name. Move all the prims around, and click Record. When done, click the Compile button. It writes fast, optimized code for you.
  4. // Downloaded from : http://www.outworldz.com/cgi/freescripts.plx?ID=1691
  5. // This program is free software; you can redistribute it and/or modify it.
  6. // Additional Licenes may apply that prevent you from selling this code
  7. // and these licenses may require you to publish any changes you make on request.
  8. //
  9. // There are literally thousands of hours of work in these scripts. Please respect
  10. // the creators wishes and Copyright law and follow their license requirements.
  11. //
  12. // License information included herein must be included in any script you give out or use.
  13. // Licenses may also be included in the script or comments by the original author, in which case
  14. // the authors license must be followed, and their licenses override any licenses outlined in this header.
  15. //
  16. // You cannot attach a license to any of these scripts to make any license more or less restrictive.
  17. //
  18. // All scripts by avatar Ferd Frederix, unless stated otherwise in the script, are licensed as Creative Commons By Attribution and Non-Commercial.
  19. // Commercial use is NOT allowed - no resale of my scripts in any form.
  20. // This means you cannot sell my scripts but you can give them away if they are FREE.
  21. // Scripts by Ferd Frederix may be sold when included in a new object that actually uses these scripts. Putting my script in a prim and selling it on marketplace does not constitute a build.
  22. // For any reuse or distribution, you must make clear to others the license terms of my works. This is done by leaving headers intact.
  23. // See http://creativecommons.org/licenses/by-nc/3.0/ for more details and the actual license agreement.
  24. // You must leave any author credits and any headers intact in any script you use or publish.
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////
  26. // If you don't like these restrictions and licenses, then don't use these scripts.
  27. //////////////////////// ORIGINAL AUTHORS CODE BEGINS ////////////////////////////////////////////
  28. // For more details on how this script works, see <a href="http://www.outworldz.com/secondlife/posts/prim-compiler/">http://www.outworldz.com/secondlife/posts/prim-compiler/</a>
  29. //
  30. //This is the Animator menu system. You will also need to the compiler script. You can adjust the script for OpenSim or Second Life with one variable set to TRUE or FALSE for memory checking.
  31. // ______ _ ______ _ _
  32. // | ___| | | | ___| | | (_)
  33. // | |_ ___ _ __ __| | | |_ _ __ ___ __| | ___ _ __ ___ __
  34. // | _/ _ \ '__/ _` | | _| '__/ _ \/ _` |/ _ \ '__| \ \/ /
  35. // | || __/ | | (_| | | | | | | __/ (_| | __/ | | |> <
  36. // \_| \___|_| \__,_| \_| |_| \___|\__,_|\___|_| |_/_/\_\
  37. //
  38. // fred@mitsi.com
  39. // 2-19-2012
  40. // Animator front-end for Compiler Script
  41. // This is free software, it is not for sale at any price
  42. integer playchannel = 1; // the playback channel, this is the channel you use in LinkMessages
  43. integer debug = FALSE; // if set to TRUE, debug info appears
  44. integer dialogchannel ; // dialog boxes
  45. integer nPrims; // total number of prims
  46. integer wantname; // flag indicating we are waiting for a name to be chatted
  47. integer wantnum; // flag asking for numbers to be chatted.
  48. // Link messages
  49. integer CLEAR = -234756;
  50. integer DATA = -234757;
  51. integer COMPILE = -234758;
  52. integer DIE = -234759;
  53. // the list of coords
  54. list masterlist; // master list of all positions
  55. list llastPrimList; // storage of the last prim position we learned
  56. string curranimation; // what we are playing
  57. integer STRIDE = 5; // size of the master list
  58. integer lastSTRIDE = 3; // size of the last prim list
  59. integer listener; // temp listener when we are waiting for them to give us a name
  60. vector InitSize; // the initial size when the prims were recorded
  61. list LoadedAnims; // animations added to the Menu display
  62. DEBUG(string msg)
  63. {
  64. if (debug) llOwnerSay("// " + msg);
  65. }
  66. Record()
  67. {
  68. if (llStringLength(curranimation) > 0)
  69. {
  70. integer foundmovement = 0; // will be set if any child moved
  71. integer PrimCounter ; // skip past the root prim
  72. for (PrimCounter =2; PrimCounter <= nPrims; PrimCounter++ )
  73. {
  74. //DEBUG("Checking Prim " + (string) PrimCounter);
  75. list my_list = llGetLinkPrimitiveParams(PrimCounter,[PRIM_POSITION,PRIM_ROTATION, PRIM_SIZE ]);
  76. // position is always in region coordinates, even if the prim is a child or the root prim of an attachment.
  77. // rot is always the global rotation, even if the prim is a child or the root prim of an attachment.
  78. // get current prim pos, rot and size
  79. vector vrealPrimPos = llList2Vector (my_list,0) - llGetPos(); // position subtract Global Pos
  80. vrealPrimPos /= llGetRot();
  81. rotation rrealPrimRot = llList2Rot (my_list,1) / llGetRot(); // rotation subtract Global Rot
  82. vector vrealPrimSize = llList2Vector (my_list,2); // size
  83. // compare it to the last one we had
  84. integer iindex = (PrimCounter - 2) * lastSTRIDE; // zeroth position is PrimCounter - start, or 2
  85. // get the last thing we remembered about this prim
  86. vector vlastPrimPos = llList2Vector (llastPrimList, iindex);
  87. rotation rlastPrimRot = llList2Rot (llastPrimList, iindex+1);
  88. vector vlastPrimSize = llList2Vector (llastPrimList, iindex+2);
  89. //if (debug)
  90. //{
  91. // vrealPrimPos = <1,2,llFrand(1)>; // make a small change for debugging in LSL Editor
  92. //}
  93. // if anything changed on this prim, we must record it.
  94. if (vlastPrimPos != vrealPrimPos ||
  95. rlastPrimRot != rrealPrimRot ||
  96. vlastPrimSize!= vrealPrimSize
  97. )
  98. {
  99. foundmovement++;
  100. //Save changes in the master list of all animations
  101. masterlist += curranimation;
  102. masterlist += (float) PrimCounter;
  103. masterlist += vrealPrimPos;
  104. masterlist += rrealPrimRot;
  105. masterlist += vrealPrimSize;
  106. string msg = curranimation + "|" + (string) PrimCounter + "|" + (string) vrealPrimPos + "|" + (string) rrealPrimRot + "|" + (string) vrealPrimSize ;
  107. llMessageLinked(LINK_ROOT,DATA,msg,"");
  108. // update the last movement list
  109. llastPrimList = llListReplaceList(llastPrimList,[vrealPrimPos],iindex,iindex);
  110. llastPrimList = llListReplaceList(llastPrimList,[rrealPrimRot],iindex+1,iindex+1);
  111. llastPrimList = llListReplaceList(llastPrimList,[vrealPrimSize],iindex+2,iindex+2);
  112. } // if
  113. else
  114. {
  115. //DEBUG( (string) PrimCounter);
  116. }
  117. } // for
  118. //DEBUG("history:" + llDumpList2String(llastPrimList,":"));
  119. if (foundmovement)
  120. {
  121. llOwnerSay("Recorded " + (string) foundmovement + " prims" );
  122. }
  123. else
  124. {
  125. llOwnerSay("You must move at least one child prim.");
  126. }
  127. }
  128. else
  129. {
  130. llOwnerSay("You must name your animation.");
  131. llOwnerSay("Type the new animation name on channel /" + (string) dialogchannel);
  132. wantname++;
  133. }
  134. }
  135. // on reset, record the base position in history so we can see changes
  136. Clear()
  137. {
  138. llMessageLinked(LINK_ROOT,CLEAR,"","");
  139. LoadedAnims = []; // wipe out Menu
  140. masterlist = []; // wipe all recordings
  141. llastPrimList = []; // wipe last animations
  142. integer PrimCounter ; // skip 1, which is the root prim
  143. integer counter = 0;
  144. // save all the current settings in memory
  145. for (PrimCounter=2; PrimCounter <= nPrims; PrimCounter++ )
  146. {
  147. list my_list = llGetLinkPrimitiveParams(PrimCounter,[PRIM_POSITION,PRIM_ROTATION, PRIM_SIZE ]);
  148. // save the local pos and rot, since llGetLinkPrimitiveParams returns global pos and rot
  149. vector primpos = llList2Vector (my_list,0) - llGetPos();
  150. primpos /= llGetRot();
  151. rotation primrot = llList2Rot (my_list,1) / llGetRot();
  152. vector primsize = llList2Vector (my_list,2) ;
  153. llastPrimList += primpos;
  154. llastPrimList += primrot;
  155. llastPrimList += primsize;
  156. counter++;
  157. }
  158. llOwnerSay("There are " + (string) counter + " animatable prims");
  159. }
  160. DumpBack ()
  161. {
  162. integer imax = llGetListLength(masterlist);
  163. integer howmany = imax / STRIDE ;
  164. llOwnerSay("Preprocessing " + (string) howmany + " movements. " );
  165. llMessageLinked(LINK_ROOT,COMPILE,"","");
  166. }
  167. rotation calcChildRot(rotation rdeltaRot)
  168. {
  169. if (llGetAttached())
  170. return rdeltaRot/llGetLocalRot();
  171. else
  172. return rdeltaRot/llGetRootRotation();
  173. }
  174. PlayBack (string name)
  175. {
  176. integer i;
  177. integer imax = llGetListLength(masterlist);
  178. integer linknum = 0;
  179. for (i = 0; i < imax; i+= STRIDE)
  180. {
  181. string saniName = llList2String(masterlist,i);
  182. vector scale = llGetScale();
  183. float delta = scale.x / InitSize.x ; // see if the root prim has grown or shrunk as a percentage
  184. if (saniName == name)
  185. {
  186. float fprimNum = llList2Float(masterlist,i+1);
  187. vector vPos = llList2Vector(masterlist,i+2);
  188. rotation rRot = llList2Rot(masterlist,i+3) ;
  189. vector vprimSize = llList2Vector(masterlist,i+4) ;
  190. vPos *= delta; // add any difference in size to it positions there
  191. vprimSize *= delta; // grow the child prim, too
  192. // support negative prim numbers as a delay
  193. if (fprimNum < 0)
  194. {
  195. // DEBUG("Sleeping " + (string) (fprimNum * -1));
  196. llSleep((float) fprimNum * -1);
  197. }
  198. else
  199. {
  200. // set the local pos and locat rot to the prims orientation and position
  201. rRot = calcChildRot(rRot);
  202. list actions = [PRIM_POSITION,vPos,PRIM_ROTATION,rRot,PRIM_SIZE,vprimSize];
  203. //DEBUG("Moving prim :" + (string) fprimNum + ":" + llDumpList2String(actions,":"));
  204. llSetLinkPrimitiveParamsFast((integer) fprimNum,actions);
  205. }
  206. }
  207. }
  208. }
  209. MakeMenu()
  210. {
  211. list amenu = ["Reset","Record","Help","Name","Compile","Pause", "Finish"] + LoadedAnims;
  212. llListenRemove(listener);
  213. listener = llListen(dialogchannel,"","","");
  214. amenu = llDeleteSubList(amenu,12,99);
  215. llDialog(llGetOwner(), "Pick a command",amenu,dialogchannel);
  216. }
  217. default
  218. {
  219. state_entry()
  220. {
  221. InitSize = llGetScale(); // save the size when we recorded the prims
  222. nPrims = llGetNumberOfPrims(); // how many we are recording
  223. llOwnerSay("Prim count = " + (string) nPrims);
  224. Clear();
  225. dialogchannel = (integer) (llFrand(100) +600);
  226. MakeMenu();
  227. }
  228. touch_start(integer total_number)
  229. {
  230. if (llDetectedKey(0) == llGetOwner())
  231. {
  232. llOwnerSay("FreeMemory="+(string)llGetFreeMemory());
  233. MakeMenu();
  234. }
  235. }
  236. listen( integer channel, string name, key id, string message )
  237. {
  238. if (message == "Finish")
  239. {
  240. llMessageLinked(LINK_ROOT,DIE,"die","");
  241. if (! debug)
  242. {
  243. llRemoveInventory(llGetScriptName());
  244. }
  245. }
  246. else if (message == "Reset")
  247. {
  248. llResetScript();
  249. }
  250. else if (wantnum)
  251. {
  252. float num = (float) message;
  253. if (num > 0.0)
  254. {
  255. num = -1 * (num /1000);
  256. masterlist += curranimation;
  257. masterlist += num;
  258. masterlist += <0,0,0>; // pos
  259. masterlist += <0,0,0,1>;// rot
  260. masterlist += <0,0,0>; // size
  261. string msg = curranimation + "|" + (string) num + "|<0,0,0>|<0,0,0,1>|<0,0,0>" ;
  262. llMessageLinked(LINK_ROOT,DATA,msg,"");
  263. }
  264. else
  265. {
  266. llOwnerSay("Please enter numbers in milliseconds. 1000=1000ms = 1 second. 500=500ms = 1/2 a second.");
  267. llOwnerSay("What is a milliHelen? A milliHelen is a face that can launch one ship!");
  268. }
  269. wantnum = FALSE;
  270. MakeMenu();
  271. }
  272. else if (message =="Pause")
  273. {
  274. wantnum= TRUE;
  275. llListenRemove(listener);
  276. listener = llListen(dialogchannel,"","","");
  277. llTextBox(llGetOwner(), "Enter a delay in milliseconds:\n1000 = 1 second\n500 = 1/2 second",dialogchannel);
  278. }
  279. else if (message == "Record")
  280. {
  281. Record();
  282. MakeMenu();
  283. }
  284. else if (message == "Name")
  285. {
  286. llOwnerSay("Enter a new name, or type the animation name on channel /" + (string) dialogchannel);
  287. wantname = TRUE;
  288. MakeMenu();
  289. llTextBox(llGetOwner(),"Enter an Animation Name",dialogchannel);
  290. }
  291. else if (message == "Compile")
  292. {
  293. DumpBack();
  294. MakeMenu();
  295. }
  296. else if (message == "Help")
  297. {
  298. llLoadURL(llGetOwner(),"View online help", "http://secondlife.mitsi.com/secondlife/Posts/Prim-Compiler");
  299. }
  300. else if (wantname)
  301. {
  302. curranimation = message;
  303. LoadedAnims += message;
  304. MakeMenu();
  305. llOwnerSay("Recording is ready for animation '" + curranimation + "'");
  306. llOwnerSay("Position any child prims, then select the menu item 'Record', and repeat as necessary. When finished, click 'Compile' to save the animation, or click the animation name to play it back. Click 'Name' to start a new animation sequence");
  307. wantname = 0;
  308. }
  309. else
  310. {
  311. if (llListFindList(LoadedAnims,[message]) != -1)
  312. {
  313. PlayBack(message);
  314. MakeMenu();
  315. }
  316. }
  317. }
  318. }