animator.lsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // :CATEGORY:Bird
  2. // :NAME:Roaming Owl
  3. // :AUTHOR:Ferd Frederix
  4. // :CREATED:2013-09-06
  5. // :EDITED:2013-09-18 15:39:01
  6. // :ID:707
  7. // :NUM:964
  8. // :REV:1
  9. // :WORLD:Second Life
  10. // :DESCRIPTION:
  11. // Prim animator
  12. // :CODE:
  13. //You can now animate any object, with just one script!  Setup is very simple. Just drop the script and a blank notecard into the object, click the object, and give your animation a name.  Move all the prims around, and click Record. When done, click the Animation Name, and watch it play back every move!
  14. // Downloaded from : http://www.outworldz.com/cgi/freescripts.plx?ID=1519
  15. // This program is free software; you can redistribute it and/or modify it.
  16. // Additional Licenes may apply that prevent you from selling this code
  17. // and these licenses may require you to publish any changes you make on request.
  18. //
  19. // There are literally thousands of hours of work in these scripts. Please respect
  20. // the creators wishes and follow their license requirements.
  21. //
  22. // Any License information included herein must be included in any script you give out or use.
  23. // Licenses are included in the script or comments by the original author, in which case
  24. // the authors license must be followed.
  25. // A GNU license, if attached by the author, means the original code must be FREE.
  26. // Modifications can be made and products sold with the scripts in them.
  27. // You cannot attach a license to make this GNU License
  28. // more or less restrictive. see http://www.gnu.org/copyleft/gpl.html
  29. // Creative Commons licenses apply to all scripts from the Second Life
  30. // wiki and script library and are Copyrighted by Linden Lab. See
  31. // http://creativecommons.org/licenses/
  32. // Please leave any author credits and headers intact in any script you use or publish.
  33. // If you don't like these restrictions, then don't use these scripts.
  34. //////////////////////// ORIGINAL AUTHORS CODE BEGINS ////////////////////////////////////////////
  35. // One Script Prim Animation
  36. // Original Script by: Ferd Frederix
  37. // No Spam version by: Gino Rascon
  38. integer runtime = TRUE; // set to TRUE after making the notecard
  39. string NOTECARD = "Movement"; // the notecard for this script, you can add more than 1 script and notecard, just change this line to match
  40. integer playchannel = 1; // the playback channel, this is the channel you use in LinkMessages
  41. integer debug = FALSE; // if set to TRUE, debug info appears
  42. // notecard reading
  43. string priorname; // the prior animation name so we can spot differences as we read them in
  44. integer iIndexLines; // lines in a card
  45. integer inotecardIndex = 0; // notecard counter;
  46. integer move = 0; // N movements rea from the notecard
  47. key kNoteCardLines; // the key of the notecard
  48. key kGetIndexLines; // the key of the current line
  49. //communications
  50. integer dialogchannel ; // dialog boxes
  51. integer nPrims; // total number of prims
  52. integer PrimsCounter = 0; // how many have checked in
  53. integer timercounter = 0; // how many seconds have gone by
  54. integer wantname; // flag indicating we are waiting for a name to be chatted
  55. // the list of coords
  56. list masterlist; // master list of all positions
  57. list llastPrimList; // storage of the last prim position we learned
  58. string curranimation; // what we are playing
  59. integer STRIDE = 5; // size of the master list
  60. integer lastSTRIDE = 4; // size of the last prim list
  61. integer listener; // temp listener when we are waiting for them to give us a name
  62. vector InitSize; // the initial size when the prims were recorded
  63. list LoadedAnims; // animations read from the notecard added to the Menu display
  64. // in case of hand editing, we wupe out extra stuff at end
  65. string Getline(list Input, integer line)
  66. {
  67. return llStringTrim(llList2String(Input, line),STRING_TRIM);
  68. }
  69. Record()
  70. {
  71. if (llStringLength(curranimation) > 0)
  72. {
  73. integer foundmovement = 0; // will be set if any child moved
  74. integer PrimCounter ; // skip past the root prim
  75. for (PrimCounter =2; PrimCounter <= nPrims; PrimCounter++ )
  76. {
  77. list my_list = llGetLinkPrimitiveParams(PrimCounter,[PRIM_POSITION,PRIM_ROTATION, PRIM_SIZE ]);
  78. // position is always in region coordinates, even if the prim is a child or the root prim of an attachment.
  79. // rot is always the global rotation, even if the prim is a child or the root prim of an attachment.
  80. // get current prim pos, rot and size
  81. vector vrealPrimPos = llList2Vector (my_list,0) - llGetPos(); // position subtract Global Pos
  82. vrealPrimPos /= llGetRot();
  83. rotation rrealPrimRot = llList2Rot (my_list,1) / llGetRot(); // rotation subtract Global Rot
  84. vector vrealPrimSize = llList2Vector (my_list,2); // size
  85. // compare it to the last one we had, stride of list is a 4, and it is already sorted
  86. integer iindex = (PrimCounter - 2) * lastSTRIDE; // zeroth position is PrimCounter - start, or 2
  87. // get the last thing we remembered about this prim
  88. float fprimNum = llList2Integer (llastPrimList, iindex); // must be 0,1,2, in order
  89. vector vlastPrimPos = llList2Vector (llastPrimList, iindex+1);
  90. rotation rlastPrimRot = llList2Rot (llastPrimList, iindex+2);
  91. vector vlastPrimSize = llList2Vector (llastPrimList, iindex+3);
  92. // if anything changed on this prim, we must record it.
  93. if (vlastPrimPos != vrealPrimPos ||
  94. rlastPrimRot != rrealPrimRot ||
  95. vlastPrimSize!= vrealPrimSize
  96. )
  97. {
  98. foundmovement++;
  99. // show owner any changes they mnade
  100. if (debug)
  101. {
  102. llOwnerSay("prim:" + (string) PrimCounter);
  103. if (vlastPrimPos != vrealPrimPos)
  104. llOwnerSay("pos delta :" + (string) (vrealPrimPos - vlastPrimPos));
  105. if (rlastPrimRot != rrealPrimRot)
  106. llOwnerSay("rot delta:" + (string) (llRot2Euler (rrealPrimRot - rlastPrimRot) * RAD_TO_DEG));
  107. if (vlastPrimSize != vrealPrimSize)
  108. llOwnerSay("size delta:" + (string) (vrealPrimSize - vlastPrimSize));
  109. }
  110. //Save them in the master list of all animations
  111. masterlist += curranimation;
  112. masterlist += PrimCounter;
  113. masterlist += vrealPrimPos;
  114. masterlist += rrealPrimRot;
  115. masterlist += vrealPrimSize;
  116. // save them in the last movement list
  117. integer saved = FALSE;
  118. integer i;
  119. integer imax = llGetListLength(llastPrimList);
  120. // save the changes in the last prim list so we can keep our lists smaller
  121. for ( i=0; i < imax; i += lastSTRIDE )
  122. {
  123. if (llList2Float(llastPrimList,i) == PrimCounter)
  124. {
  125. llastPrimList = llListReplaceList(llastPrimList,[vrealPrimPos],i+1,i+1);
  126. llastPrimList = llListReplaceList(llastPrimList,[rrealPrimRot],i+2,i+2);
  127. llastPrimList = llListReplaceList(llastPrimList,[vrealPrimSize],i+3,i+3);
  128. if (debug) llOwnerSay("In history at position " + (string) (i/lastSTRIDE));
  129. saved++;
  130. }
  131. }
  132. // never moved before? add it then
  133. if (! saved)
  134. {
  135. if (debug) llOwnerSay("Someone added a new prim and then clicked Record");
  136. llastPrimList += PrimCounter;
  137. llastPrimList += vrealPrimPos;
  138. llastPrimList += rrealPrimRot;
  139. llastPrimList += vrealPrimSize;
  140. }
  141. } // if
  142. } // for
  143. if (debug) llOwnerSay("history:" + llDumpList2String(llastPrimList,":"));
  144. if (!foundmovement)
  145. llOwnerSay("You must move at least one child prim.");
  146. }
  147. else
  148. {
  149. llOwnerSay("You must name your animation.");
  150. llOwnerSay("Type the new animation name on channel /" + (string) dialogchannel);
  151. wantname++;
  152. }
  153. }
  154. // on reset, record the base position in history so we can see changes
  155. Clear()
  156. {
  157. LoadedAnims = []; // wipe out Menu
  158. masterlist = []; // wipe all recordings
  159. llastPrimList = []; // wipe last animations
  160. integer PrimCounter ; // skip 1, which is the root prim
  161. integer counter = 0;
  162. // save all the current settings in memory
  163. for (PrimCounter=2; PrimCounter <= nPrims; PrimCounter++ )
  164. {
  165. list my_list = llGetLinkPrimitiveParams(PrimCounter,[PRIM_POSITION,PRIM_ROTATION, PRIM_SIZE ]);
  166. // save the local pos and rot, since llGetLinkPrimitiveParams returns global pos and rot
  167. vector primpos = llList2Vector (my_list,0) - llGetPos();
  168. rotation primrot = llList2Rot (my_list,1) / llGetRot();
  169. vector primsize = llList2Vector (my_list,2) ;
  170. llastPrimList += PrimCounter;
  171. llastPrimList += primpos;
  172. llastPrimList += primrot;
  173. llastPrimList += primsize;
  174. counter++;
  175. }
  176. if(debug) llOwnerSay("Saved " + (string) counter + " prims initial position in history");
  177. }
  178. DumpBack ()
  179. {
  180. integer i;
  181. integer imax = llGetListLength(masterlist);
  182. integer howmany = imax / STRIDE ;
  183. llOwnerSay((string) howmany + " movements recorded - copy these and paste them into the notecard");
  184. integer flag = 0;
  185. for (i = 0; i < imax; i+= STRIDE)
  186. {
  187. if ( i == 0 )
  188. llOwnerSay( "|start|" + (string) llGetScale());
  189. string saniName = llList2String(masterlist,i);
  190. curranimation = saniName;
  191. float fprimNum = llList2Integer(masterlist, i+1);
  192. integer iprimNum = (integer) fprimNum;
  193. vector vprimPos = llList2Vector(masterlist, i+2);
  194. rotation rprimRot = llList2Rot(masterlist, i+3) ;
  195. vector vprimSize = llList2Vector(masterlist, i+4);
  196. llOwnerSay("|"+ saniName + "|" + (string) iprimNum + "|" + (string) vprimPos + "|" + (string) rprimRot + "|" + (string) vprimSize );
  197. flag++;
  198. }
  199. if (! flag)
  200. llOwnerSay("No recording was made, nothing to play back." );
  201. }
  202. rotation calcChildRot(rotation rdeltaRot)
  203. {
  204. if (llGetAttached())
  205. return rdeltaRot/llGetLocalRot();
  206. else
  207. return rdeltaRot/llGetRootRotation();
  208. }
  209. PlayBack (string name)
  210. {
  211. integer i;
  212. integer imax = llGetListLength(masterlist);
  213. integer linknum = 0;
  214. for (i = 0; i < imax; i+= STRIDE)
  215. {
  216. string saniName = llList2String(masterlist,i);
  217. if (saniName == name)
  218. {
  219. float fprimNum = llList2Float(masterlist,i+1);
  220. vector vPos = llList2Vector(masterlist,i+2);
  221. rotation rRot = llList2Rot(masterlist,i+3) ;
  222. vector vprimSize = llList2Vector(masterlist,i+4) ;
  223. vector scale = llGetScale();
  224. float delta = scale.x / InitSize.x ; // see if the root prim has grown or shrunk as a percentage
  225. vPos *= delta; // add any difference in size to it positions there
  226. vprimSize *= delta; // grow the child prim, too
  227. // support negative prim numbers as a delay
  228. if (fprimNum < 0)
  229. {
  230. if (debug) llOwnerSay("Sleeping " + (string) (fprimNum * -1));
  231. llSleep((float) fprimNum * -1);
  232. }
  233. else
  234. {
  235. if (fprimNum > 1 )
  236. {
  237. // set the local pos and locat rot to the prims orientation and position
  238. rRot = calcChildRot(rRot);
  239. list actions = [PRIM_POSITION,vPos,PRIM_ROTATION,rRot,PRIM_SIZE,vprimSize];
  240. if (debug) llOwnerSay("Moving prim :" + (string) fprimNum + ":" + llDumpList2String(actions,":"));
  241. llSetLinkPrimitiveParamsFast((integer) fprimNum,actions);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. MakeMenu()
  248. {
  249. list amenu = ["Reset","Record","Help","Name","Notecard","Pause"] + LoadedAnims;
  250. llListenRemove(listener);
  251. listener = llListen(dialogchannel,"","","");
  252. amenu = llDeleteSubList(amenu,12,99);
  253. llDialog(llGetOwner(), "Pick a command",amenu,dialogchannel);
  254. }
  255. default
  256. {
  257. state_entry()
  258. {
  259. InitSize = llGetScale(); // save the size when we recorded the prims
  260. nPrims = llGetNumberOfPrims(); // how many we are recording
  261. if (debug) llOwnerSay(" Total Prims = " + (string) nPrims);
  262. Clear();
  263. string notecardname = llGetInventoryName(INVENTORY_NOTECARD,0);
  264. if (llStringLength(notecardname) > 0)
  265. {
  266. kNoteCardLines = llGetNumberOfNotecardLines(NOTECARD);
  267. kGetIndexLines = llGetNotecardLine(NOTECARD,0);
  268. }
  269. else
  270. {
  271. llOwnerSay("If you add a notecard, you can save your animations permanently in a notecard named " + NOTECARD);
  272. }
  273. dialogchannel = (integer) (llFrand(100) +600);
  274. }
  275. // read notecard on bootup
  276. dataserver(key queryid, string data)
  277. {
  278. if (queryid == kNoteCardLines)
  279. {
  280. iIndexLines = (integer) data;
  281. }
  282. if (queryid == kGetIndexLines)
  283. {
  284. if (data != EOF)
  285. {
  286. queryid = llGetNotecardLine(NOTECARD, inotecardIndex);
  287. list lLine = (llParseString2List(data, ["|"], []));
  288. string junk = llList2String(lLine,0);
  289. string aniname = llList2String(lLine,1);
  290. string aNum = (string) Getline(lLine,2);
  291. // check for the prim size,and save it, the fiorst line will look like this:
  292. // [18:06] prim position 1.2: |start|<1.02306, 1.02306, 1.02306>
  293. if (inotecardIndex == 0 && aniname == "start")
  294. {
  295. InitSize = (vector) aNum;
  296. }
  297. else if (inotecardIndex == 0 && aniname != "start")
  298. {
  299. llOwnerSay("The notecard " + NOTECARD + " is incorrect, it must begin with 'start|<x,y,z>' with the size of the original prim");
  300. }
  301. else
  302. {
  303. float Num = (float) aNum;
  304. vector vPos = (vector) Getline(lLine,3); // global for calcChild()
  305. rotation rRot = (rotation) Getline(lLine,4); // global for calcChild()
  306. vector Size = (vector) Getline(lLine,5);
  307. vector scale = llGetScale();
  308. float delta = scale.x / InitSize.x ; // see if the root prim has grown or shrunk as a percentage
  309. if (aniname != priorname)
  310. {
  311. llOwnerSay("Loading animation " + aniname);
  312. priorname = aniname;
  313. LoadedAnims += aniname;
  314. }
  315. if(Num != 1) // skip root prim
  316. {
  317. masterlist += [aniname];
  318. masterlist += [Num];
  319. masterlist += [vPos];
  320. masterlist += [rRot];
  321. masterlist += [Size];
  322. if (Num > 1) // not the pauses
  323. {
  324. rRot = calcChildRot(rRot);
  325. vPos *= delta; // add any difference in size to it positions there
  326. Size *= delta; // grow the child prim, too
  327. llSetLinkPrimitiveParamsFast((integer) Num,[PRIM_POSITION,vPos,PRIM_ROTATION,rRot,PRIM_SIZE,Size]);
  328. move++;
  329. }
  330. }
  331. }
  332. inotecardIndex++;
  333. integer InitPerCent = (integer) llRound(( (inotecardIndex+1) / (float) iIndexLines) * 100);
  334. llSetText("Initialising... \n" + (string) InitPerCent + "%" , <1,1,1>, 1.0);
  335. if (InitPerCent >= 100)
  336. llSetText("" , <1,1,1>, 1.0);
  337. kGetIndexLines = llGetNotecardLine(NOTECARD,inotecardIndex);
  338. }
  339. else
  340. {
  341. llOwnerSay("initialized with " + (string) move + " movements");
  342. llSetText("" , <1,1,1>, 1.0);
  343. }
  344. }
  345. }
  346. touch_start(integer total_number)
  347. {
  348. if (llDetectedKey(0) == llGetOwner() && ! runtime)
  349. {
  350. MakeMenu();
  351. }
  352. // add any control code here
  353. // example:
  354. // llMessageLinked(LINK_SET,playchannel,"All",""); // will play Animation named "All"
  355. }
  356. listen( integer channel, string name, key id, string message )
  357. {
  358. if (channel == dialogchannel)
  359. {
  360. if (message == "Reset")
  361. {
  362. Clear();
  363. if (debug) llOwnerSay("Reset = " + llDumpList2String(llastPrimList,":"));
  364. MakeMenu();
  365. }
  366. else if (message =="Pause")
  367. {
  368. masterlist += curranimation;
  369. masterlist += -1;
  370. masterlist += <0,0,0>; // pos
  371. masterlist += <0,0,0,1>;// rot
  372. masterlist += <0,0,0>; // size
  373. MakeMenu();
  374. }
  375. else if (message == "Record")
  376. {
  377. Record();
  378. MakeMenu();
  379. }
  380. else if (message == "Name")
  381. {
  382. llOwnerSay("Type the current animation name on channel /" + (string) dialogchannel);
  383. wantname++;
  384. MakeMenu();
  385. }
  386. else if (message =="Menu")
  387. {
  388. MakeMenu();
  389. }
  390. else if (message == "Notecard")
  391. {
  392. DumpBack();
  393. MakeMenu();
  394. }
  395. else if (message == "Help")
  396. {
  397. llLoadURL(llGetOwner(),"View online help", "http://secondlife.mitsi.com/secondlife/Posts/Prim-Animator");
  398. }
  399. else if (wantname)
  400. {
  401. curranimation = message;
  402. LoadedAnims += message;
  403. MakeMenu();
  404. llOwnerSay("Recording is ready for animation '" + curranimation + "'");
  405. llOwnerSay("Position all child prims, then select the Menu item 'Record', and repeat as necessary. When finished, click 'PlayBack' to play back the animation, or click the animation name. Click 'Name' to start a new animation sequence");
  406. wantname = 0;
  407. PrimsCounter = 0;
  408. timercounter = 0;
  409. llSetTimerEvent(1.0);
  410. }
  411. else
  412. {
  413. if (llListFindList(LoadedAnims,[message]) != -1)
  414. {
  415. PlayBack(message);
  416. MakeMenu();
  417. }
  418. }
  419. }
  420. }
  421. link_message(integer sender_num, integer num, string message, key id)
  422. {
  423. if (num == playchannel)
  424. {
  425. if (debug) llOwnerSay("playback animation " + message);
  426. PlayBack(message);
  427. }
  428. }
  429. }