SequenceMgr.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. SEQUENCE GROUPING MANAGEMENT MODULE
  3. this module builds a representation of a sequence in values using containers
  4. It allows to create groups of keys, a group being a cue and one key per layer.
  5. In a group, all keys sync their time on cue's time, only value can be modified
  6. The sequence with grouping can then be exported and reloaded.
  7. The module checks periodicaly if timeline sequence structure (cues, layers and keys) and
  8. module values structure are in sync. If out of sync, you may either rebuild values structure
  9. from sequence or rebuild sequence from values
  10. */
  11. var syncRatems = 100;
  12. var syncDelta = 0.;
  13. var doSync = false ;
  14. var inibValuesChanged = false ;
  15. var doSyncKeysCues = false ;
  16. var saveFile = script.addFileParameter("auto save sequence","");
  17. var saveRatems = 30;
  18. var saveDelta = 0.;
  19. var debug = true;
  20. var debugFile;
  21. var parametersPath = local.getChild("parameters") ;
  22. var sequence = '' ;
  23. var sequencePath = local.getChild("parameters").sequence ;
  24. var file ;
  25. var filePath = local.parameters.sequenceFile ;
  26. var newGroupLayers = local.values.newGroup.layers ;
  27. // var groups = local.getChild("values").groups ;
  28. var cues = local.getChild("values").keys.cues ;
  29. // var cuesList = []; //holds cues containers reference
  30. var layers = local.getChild("values").keys.layers ;
  31. // var layerList = {}; //holds layer container as object with reference and an array of keys containers references
  32. var rebuildValues = script.addTrigger("rebuild values", "adds a key at current cursor position" );
  33. var refreshKeys = script.addTrigger("refresh keys", "truc");
  34. var TrigprintKeys = script.addTrigger("printKeys","truc");
  35. var clearSeq = script.addTrigger("clear Sequence","truc");
  36. var UIposGet = script.addTrigger("get UIPos", "");
  37. var viewZoom = script.addFloatParameter("View Zoom", "", 0.5, 0., 1.); //part of sequence time to show
  38. var viewFollowCursor = script.addBoolParameter("follow Cursor", "", false);
  39. var followCursor = false ;
  40. var followZoom = 0.5 ;
  41. var followRatems = 1.;
  42. var followDelta = 0.;
  43. // var sequence_structure = { params: {},
  44. // groups:{},
  45. // keys:{
  46. // cues:{},
  47. // layers:{}
  48. // }
  49. // };
  50. //WAS tl_groups
  51. /*group0 : { inCue : {pan : "key", tilt : "key", zoom : "key", dim : "key", focus : "key"},
  52. outCue : {pan : "key", tilt : "key", zoom : "key", dim : "key", focus : "key"}
  53. }*/
  54. ///////////////////// SCRIPT FUNCTIONS //////////////////////////////
  55. function init() {
  56. script.log("LOADED SEQUENCE GROUPING MANAGEMENT MODULE ");
  57. //adapt update rate
  58. updateSyncRate();
  59. updateSynchronize();
  60. // script.log(root.sequences.truc.viewStartTime.get();)
  61. saveFile.set("sequence_autosave.json");
  62. saveFile.setAttribute("readonly", true);
  63. // importSequenceFile(saveFile);
  64. // var audioLayer = root.sequences.truc.layers.audio ;
  65. // var newClip = audioLayer.clips.addItem();
  66. // newClip.filePath.set("/home/titi/work/media/Sons/Mono/Aaaah.wav");
  67. // newClip.setName("testAudio");
  68. // newClip.startTime.set(12);
  69. // printMethodsAndProperties(root.sequences.truc);
  70. // script.log(local.getChild("values").groups.group0.cue.get());
  71. //test if a sequence is already choosen else refresh and unlock sequence enum
  72. sequence = local.getChild("parameters").sequence.getKey();
  73. // var sequenceIndex = local.getChild("parameters").sequence.get();
  74. // script.log(sequenceIndex + " " + sequence);
  75. if(sequence == ""){refreshSequenceEnum();}
  76. else{
  77. sequencePath.removeOptions();
  78. sequencePath.addOption(sequence,0);
  79. sequencePath.setAttribute("readonly", true);
  80. }
  81. sequence = local.getChild("parameters").sequence.getKey();
  82. }
  83. function scriptParameterChanged(param){
  84. if (param.is(TrigprintKeys) ){printKeys();}
  85. else if(param.is(viewZoom)) {
  86. script.log("view zoom changed");
  87. followZoom = param.get();
  88. }
  89. if (param.is(viewFollowCursor)){
  90. script.log("follow cursor " + (param.get() ? "ON" : "OFF"));
  91. followCursor = param.get();
  92. }
  93. else if(param.is(rebuildValues)){struct_buildValuesFromSeq();}
  94. else if (param.is(refreshKeys)){
  95. script.log("refreshKeys");
  96. refreshKeysList();
  97. }
  98. else {script.log("scriptparamchanged" + param.name);}
  99. }
  100. function update(deltaTime){
  101. if(sequence!=""){
  102. // script.log(doSync);
  103. if(doSync){
  104. syncDelta += deltaTime ;
  105. // script.log(doSync);
  106. if (syncDelta >= syncRatems){
  107. syncDelta = 0. ;
  108. var autosave = struct_buildValuesFromSeq();
  109. if(autosave){exportSequenceFile(saveFile);}
  110. }
  111. }
  112. // saveDelta += deltaTime ;
  113. // if (saveDelta >= saveRatems){
  114. // saveDelta = 0. ;
  115. // exportSequenceFile(saveFile);
  116. // script.log("sequence autosave");
  117. // }
  118. followDelta += deltaTime ;
  119. if(followCursor && (followDelta>followRatems)){
  120. followDelta = 0. ;
  121. seq_followCursor() ;
  122. }
  123. }
  124. }
  125. /////////////////////////// HELPERS /////////////////////////////////
  126. function printMethodsAndProperties(obj){
  127. script.log(" ");
  128. // var name = obj.getControlAddress() == undefined ? "object" : obj.getControlAddress();
  129. var name = "object";
  130. script.log(" METHODS OF " + name);
  131. var methods = util.getObjectMethods(obj);
  132. for(var i=0 ; i<methods.length ; i++){
  133. script.log(" "+ methods[i]);
  134. }
  135. script.log(" PARAMETERS OF " + name);
  136. var props = util.getObjectProperties(obj, true, false);
  137. for(var i=0 ; i<props.length ; i++){
  138. script.log(" "+ props[i]);
  139. }
  140. script.log(" OBJECTS OF " + name);
  141. var props = util.getObjectProperties(obj, false, true);
  142. for(var i=0 ; i<props.length ; i++){
  143. script.log(" "+ props[i]);
  144. }
  145. }
  146. function printKeys(){
  147. var seq = root.sequences.sequence.layers.mapping.automation.getItems() ;
  148. if(seq.length){
  149. for(var i=0; i<seq.length; i++){
  150. script.log (">> "+seq[i].name + " " + seq[i].position.get() + " " + seq[i].listSize.get() );
  151. }
  152. }
  153. else{script.log("nothing to print");}
  154. }
  155. /////////////////////// PARAMETERS FUNCTIONS ///////////////////////
  156. // Module callback
  157. function moduleParameterChanged(param){
  158. parametersPath = local.getChild("parameters") ;
  159. script.log("received module param :" + param.name);
  160. if (param.is(parametersPath.importExport.sequenceFile)) {}
  161. else if (param.is(parametersPath.importExport.importSequence)){importSequenceFile(parametersPath.importExport.sequenceFile);}
  162. else if (param.is(parametersPath.importExport.exportSequence)) {
  163. exportSequenceFile(parametersPath.importExport.sequenceFile);
  164. exportSequenceFile(saveFile);
  165. }
  166. else if (param.is(parametersPath.synchronize)){updateSynchronize();}
  167. else if (param.is(parametersPath.syncRate)){updateSyncRate();}
  168. else if (param.is(parametersPath.monitorKeysCues)){monitorKeysCues();}
  169. else if (param.is(parametersPath.clearSequence)){seq_clearSequence();}
  170. else if (param.is(parametersPath.reorderSequence)){seq_reorderSequence();}
  171. else if (param.is(parametersPath.changeSequence)){refreshSequenceEnum();}
  172. else if (param.is(parametersPath.sequence)){
  173. sequence = sequencePath.getKey();
  174. if(sequence!=""){
  175. script.log("watching "+ sequence);
  176. local.parameters.sequence.setAttribute("readonly",true);
  177. }
  178. }
  179. else{script.log("received module param :" + param.name);}
  180. }
  181. function updateSynchronize(){
  182. // var parametersPath = local.getChild("parameters") ;
  183. doSync = parametersPath.synchronize.get();
  184. script.log(doSync ? "sync ON" : "sync OFF" );
  185. }
  186. function updateSyncRate(){
  187. // var parametersPath = local.getChild("parameters") ;
  188. syncRatems = parametersPath.syncRate.get() / 1000.;
  189. script.log("new sync rate : " + syncRatems);
  190. // script.setUpdateRate(syncRatems);
  191. }
  192. function monitorKeysCues(){
  193. // var parametersPath = local.getChild("parameters") ;
  194. doSyncKeysCues = parametersPath.monitorKeysCues.get() ;
  195. script.log(doSyncKeysCues ? "syncKeys ON" : "syncKeys OFF" );
  196. // script.setUpdateRate(syncRatems);
  197. }
  198. //refresh Enum
  199. function refreshSequenceEnum(){
  200. script.log("refresh list of sequences");
  201. local.parameters.sequence.setAttribute("readonly",false);
  202. if(root.sequences.getItems().length){
  203. local.parameters.sequence.removeOptions();
  204. local.parameters.sequence.addOption("", 0);
  205. for(var i=0 ; i<root.sequences.getItems().length ; i++ ){
  206. local.parameters.sequence.addOption(root.sequences.getItems()[i].name,i+1);
  207. }
  208. }
  209. }
  210. function importSequenceFile(loadFile){
  211. doSync = false ;
  212. script.log("loading file");
  213. loadData = loadFile.readFile(true);
  214. //load parameters
  215. var parameters = local.getChild("parameters");
  216. parameters.syncRate.set(loadData.parameters.SyncRate);
  217. parameters.synchronize.set(loadData.parameters.Synchronize);
  218. sequence = loadData.parameters.Sequence;
  219. if(1){
  220. parameters.sequence.removeOptions();
  221. parameters.sequence.addOption(sequence,0);
  222. parameters.sequence.setAttribute("readonly", true);
  223. // script.log(parameters.synchronize.get());
  224. //
  225. //check if Sequence already exists in sequences manager, else create it
  226. var tl_sequence = root.sequences.getItemWithName(sequence) ;
  227. if(tl_sequence == undefined){
  228. tl_sequence = root.sequences.addItem();
  229. tl_sequence.setName(sequence);
  230. }
  231. //recreate cues
  232. tl_sequence.cues.removeAll();
  233. var cuesList = util.getObjectProperties(loadData.values.keys.cues, true, false);
  234. if(cuesList.length){
  235. for(var i=0; i<cuesList.length; i++){
  236. var cue = tl_sequence.cues.addItem();
  237. cue.setName(cuesList[i]);
  238. cue.time.set(loadData.values.keys.cues[cuesList[i]].time);
  239. }
  240. }
  241. //recreate layers and keys
  242. tl_sequence.layers.removeAll();
  243. var layersList = util.getObjectProperties(loadData.values.keys.layers, true, false);
  244. if(layersList.length){
  245. for(var i=0; i<layersList.length; i++ ){
  246. var layerType = loadData.values.keys.layers[layersList[i]].type ;
  247. var layer = tl_sequence.layers.addItem(layerType);
  248. layer.setName(layersList[i]);
  249. var keysList = util.getObjectProperties(loadData.values.keys.layers[layersList[i]], true, false);
  250. if(keysList.length){
  251. if (layerType != "Audio"){
  252. for(var j=0; j<keysList.length; j++){
  253. var key = layer.automation.addItem();
  254. key.setName(keysList[j]);
  255. key.value.set(loadData.values.keys.layers[layersList[i]][keysList[j]].value);
  256. key.position.set(loadData.values.keys.layers[layersList[i]][keysList[j]].time);
  257. }
  258. layer.automation.reorderItems();
  259. }
  260. else {
  261. for(var j=0; j<keysList.length; j++){
  262. var clip = layer.clips.addItem();
  263. clip.setName(keysList[j]);
  264. clip.filePath.set(loadData.values.keys.layers[layersList[i]][keysList[j]].filePath);
  265. clip.startTime.set(loadData.values.keys.layers[layersList[i]][keysList[j]].startTime);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. //recreate groups
  272. var groupList = util.getObjectProperties(loadData.values.groups, true, false);
  273. if(groupList.length){
  274. for(var i=0; i<groupList.length; i++){
  275. var group = local.getChild("Values").groups.addContainer(groupList[i]);
  276. var groupLayers = util.getObjectProperties(loadData.values.groups[groupList[i]], true, false);
  277. var cue = group.addFloatParameter("cue", "", 0., 0.);
  278. group.setName(groupList[i]);
  279. cue.set(loadData.values.groups[groupList[i]].time);
  280. var groupKeysList = util.getObjectProperties(loadData.values.groups[groupList[i]].groupKeys, true, false);
  281. if (groupKeysList.length){
  282. var keysContainer = group.addContainer("keys");
  283. keysContainer.setCollapsed(true);
  284. for (var j=0; j<groupKeysList.length; j++){
  285. group.addFloatParameter(groupKeysList[j], "", 0., 0., 1.);
  286. keysContainer.addStringParameter(groupKeysList[j], "", loadData.values.groups[groupList[i]]['groupKeys'][groupKeysList[j]]['key']);
  287. }
  288. }
  289. }
  290. }
  291. // script.log(layersList[0]);
  292. doSync = true;
  293. // updateSynchronize() ;
  294. }
  295. }
  296. function exportSequenceFile(fileObj){
  297. script.log("exporting file");
  298. var exportData = {parameters:{}, values:{groups:{}, keys:{cues:{}, layers:{}}}};
  299. //collect parameters
  300. var parameters = local.getChild("parameters");
  301. exportData.parameters["Sequence"] = parameters.sequence.getKey();
  302. exportData.parameters["Synchronize"] = parameters.synchronize.get();
  303. exportData.parameters["Sync Rate"] = parameters.syncRate.get();
  304. //collect values
  305. var values = local.getChild("values");
  306. var groups = values.groups;
  307. var groupsList=util.getObjectProperties(groups, true, false);
  308. if(groupsList.length){
  309. for(var i=0; i<groupsList.length; i++){
  310. var exportGroup = exportData.values.groups[groupsList[i]] = {cue:"", time:0., groupKeys:{}};
  311. var currentGroup = groups.getChild(groupsList[i]);
  312. exportGroup.cue = currentGroup.cue.name;
  313. exportGroup.time = currentGroup.cue.get();
  314. var currentGroupLayers = util.getObjectProperties(currentGroup.keys, true, false);
  315. if (currentGroupLayers.length){
  316. for(var j=0; j<currentGroupLayers.length; j++){
  317. // script.log(groupsList[i]+ " "+ currentGroupLayers[j]);
  318. exportGroup.groupKeys[currentGroupLayers[j]] = {key:"", value:0};
  319. exportGroup.groupKeys[currentGroupLayers[j]].value = currentGroup.getChild(currentGroupLayers[j]).get();
  320. exportGroup.groupKeys[currentGroupLayers[j]].key = currentGroup.keys.getChild(currentGroupLayers[j]).get();
  321. }
  322. }
  323. }
  324. }
  325. var cues = values.keys.cues;
  326. var cuesList=util.getObjectProperties(cues, true, false);
  327. if(cuesList.length){
  328. for(var i=0; i<cuesList.length; i++){
  329. var exportCue = exportData.values.keys.cues[cuesList[i]] = {cue:"", time:0.};
  330. exportCue.cue = cuesList[i];
  331. exportCue.time = cues.getChild(cuesList[i]).time.get();
  332. }
  333. }
  334. var layers = values.keys.layers;
  335. var layersList=util.getObjectProperties(layers, true, false);
  336. if(layersList.length){
  337. for(var i=0; i<layersList.length; i++){
  338. var exportLayer = exportData.values.keys.layers[layersList[i]] = {};
  339. var currentLayer = layers.getChild(layersList[i]);
  340. var currentLayersKeys = util.getObjectProperties(currentLayer, true, false);
  341. exportLayer["type"] = values.keys.layerTypes.getChild(layersList[i]).get();
  342. if (currentLayersKeys.length){
  343. for(var j=0; j<currentLayersKeys.length; j++){
  344. if (exportLayer["type"]!= "Audio"){
  345. if(currentLayersKeys[j]!="type"){
  346. // script.log(currentLayersKeys[j]);
  347. // script.log(groupsList[i]+ " "+ currentGroupLayers[j]);
  348. exportLayer[currentLayersKeys[j]] = {key:"", value:0., time:0.};
  349. exportLayer[currentLayersKeys[j]].value = currentLayer.getChild(currentLayersKeys[j]).value.get();
  350. exportLayer[currentLayersKeys[j]].key = currentLayer.getChild(currentLayersKeys[j]).name;
  351. exportLayer[currentLayersKeys[j]].time = currentLayer.getChild(currentLayersKeys[j]).time.get();
  352. }
  353. }
  354. else {
  355. exportLayer[currentLayersKeys[j]] = {clip:"", filePath:0., startTime:0.};
  356. exportLayer[currentLayersKeys[j]].filePath = currentLayer.getChild(currentLayersKeys[j]).filePath.get();
  357. exportLayer[currentLayersKeys[j]].clip = currentLayer.getChild(currentLayersKeys[j]).name;
  358. exportLayer[currentLayersKeys[j]].startTime = currentLayer.getChild(currentLayersKeys[j]).startTime.get();
  359. }
  360. }
  361. }
  362. }
  363. }
  364. // exportData.groups
  365. fileObj.writeFile(JSON.stringify(exportData), true);
  366. }
  367. /////////////////////// VALUES FUNCTIONS ///////////////////////
  368. /* Values hold all keys, presented in group containers
  369. a nogroup container shows keys that are not grouped
  370. */
  371. // Value callback
  372. function moduleValueChanged(value){
  373. if(!inibValuesChanged){
  374. doSync = false;
  375. script.log(value.getParent().name);
  376. doSync = true;
  377. }
  378. if (value.is(local.values.newGroup.create)){createNewGroup();}
  379. // script.log(value.getParent().name);
  380. // if(value.getParent().name = "cue"){script.log("changed");}
  381. if (value.name == "addGroup"){createNewGroup();}
  382. }
  383. function struct_rebuildValues(){
  384. //build cues if needed
  385. var tl_cues = tl_seq.cues.getItems();
  386. cues = local.getChild("values").keys.cues ;
  387. var cuesList = util.getObjectProperties(cues, true, false);
  388. // script.log("cueList : " +cuesList.length);
  389. // script.log("tl List : " +tl_cues.length);
  390. if (cuesList.length != tl_cues.length){
  391. var delta = cuesList.length-tl_cues.length;
  392. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  393. delta = Math.abs(delta);
  394. script.log("cues "+ delta + (addRemove ? " up" : " down"));
  395. for(var i=0; i<delta; i++){
  396. // script.log(i);
  397. if(addRemove){
  398. var newCue = cues.addContainer("newCue"+i);
  399. newCue.addFloatParameter("time", "position in time", 0., 0.);
  400. newCue.setCollapsed(true);
  401. }
  402. else {
  403. // script.log(util.getObjectProperties(cues, true, false)[0]);
  404. cues.removeContainer(util.getObjectProperties(cues, true, false)[i]);
  405. }
  406. // cuesList = ;
  407. }
  408. hasUpdated = true;
  409. }
  410. //update cues
  411. cues = local.getChild("values").keys.cues ;
  412. var cuesList = util.getObjectProperties(cues, true, false);
  413. if(tl_cues.length){
  414. for (var i=0; i<tl_cues.length; i++){
  415. var currentCue = cues.getChild(cuesList[i]);
  416. currentCue.setName(tl_cues[i].name);
  417. currentCue.time.set(tl_cues[i].time.get());
  418. }
  419. }
  420. //build layers if needed
  421. var layersChanged = false ;
  422. var tl_layers = tl_seq.layers.getItems();
  423. layers = local.getChild("values").keys.layers ;
  424. var layersList = util.getObjectProperties(layers, true, false);
  425. if (layersList.length != tl_layers.length){
  426. layersChanged = true ;
  427. var delta = layersList.length-tl_layers.length;
  428. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  429. delta = Math.abs(delta);
  430. script.log("layers "+ delta + (addRemove ? " up" : " down"));
  431. for(var i=0; i<delta; i++){
  432. // script.log(i);
  433. if(addRemove){
  434. var newLayer = layers.addContainer("newLayer"+i);
  435. // newLayer.addFloatParameter("time", "position in time", 0., 0.);
  436. local.getChild("values").keys.layerTypes.addStringParameter("newLayer"+i, "", "");
  437. newLayer.setCollapsed(true);
  438. local.getChild("values").newGroup.layers.addFloatParameter("newLayer"+i, "value for this layer at group creation", 0., 0., 1.);
  439. // if (tl_layers[i].getType()!="Audio"){
  440. // local.getChild("values").newGroup.layers.addFloatParameter("newLayer"+i, "value for this layer at group creation", 0., 0., 1.);
  441. // }
  442. }
  443. else {
  444. // script.log(util.getObjectProperties(layers, true, false)[0]);
  445. layers.removeContainer(util.getObjectProperties(layers, true, false)[i]);
  446. local.getChild("values").newGroup.layers.removeParameter(util.getObjectProperties(layers, true, false)[i]);
  447. local.getChild("values").newGroup.layers.setCollapsed(true);
  448. local.getChild("values").newGroup.layers.setCollapsed(false);
  449. local.getChild("values").keys.layerTypes.removeParameter(util.getObjectProperties(layers, true, false)[i]);
  450. local.getChild("values").keys.layerTypes.setCollapsed(true);
  451. local.getChild("values").keys.layerTypes.setCollapsed(false);
  452. }
  453. // cuesList = ;
  454. }
  455. hasUpdated = true;
  456. }
  457. //update layers value if there was a change in structure
  458. if(layersChanged){
  459. layers = local.getChild("values").keys.layers ;
  460. var layerList = util.getObjectProperties(layers, true, false);
  461. layerTypes = local.getChild("values").keys.layerTypes ;
  462. var layerTypeList = util.getObjectProperties(layerTypes, true, false);
  463. var template = local.getChild("values").newGroup.layers;
  464. var layerTemplate = util.getObjectProperties(template, true, false);
  465. if(tl_layers.length){
  466. var layer_offset = 0 ;
  467. for (var i=0; i<tl_layers.length; i++){
  468. var currentLayer = layers.getChild(layerList[i]);
  469. // currentLayer.getChild("type").set(tl_layers[i].getType());
  470. currentLayer.setName(tl_layers[i].name);
  471. var currentLayerType = layerTypes.getChild(layerTypeList[i]);
  472. currentLayerType.setName(tl_layers[i].name);
  473. currentLayerType.set(tl_layers[i].getType());
  474. var currentTemplate = template.getChild(layerTemplate[i]) ;
  475. if (tl_layers[i].getType()=="Audio"){layer_offset+=1;}
  476. currentTemplate.setName(tl_layers[i+layer_offset].name);
  477. }
  478. }
  479. }
  480. //build keys if needed
  481. layers = local.getChild("values").keys.layers ;
  482. layersList = util.getObjectProperties(layers, true, false);
  483. if(layersList.length){
  484. for(var layerIndex=0; layerIndex<layersList.length; layerIndex++){
  485. // script.log(layersList[layerIndex]);
  486. var currentLayer = layers.getChild(layersList[layerIndex]);
  487. var keyList = util.getObjectProperties(currentLayer, true, false);
  488. var tl_lay = tl_seq.layers.getItemWithName(layersList[layerIndex]);
  489. var tl_keys;
  490. //test keys for automation layers
  491. if(tl_lay.automation!=undefined){
  492. tl_keys = tl_lay.automation.getItems();
  493. //rebuild key structure for currentLayer
  494. if (keyList.length != tl_keys.length){
  495. var delta = keyList.length-tl_keys.length;
  496. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  497. delta = Math.abs(delta);
  498. script.log("keys "+ delta + (addRemove ? " up" : " down"));
  499. for(var i=0; i<delta; i++){
  500. // script.log(i);
  501. if(addRemove){
  502. var newKey = currentLayer.addContainer("newkey"+i);
  503. newKey.addFloatParameter("time", "position in time", 0., 0.);
  504. newKey.addFloatParameter("value", "vertical value", 0., 0.);
  505. newKey.setCollapsed(true);
  506. }
  507. else {
  508. // script.log(util.getObjectProperties(currentLayer, true, false)[0]);
  509. currentLayer.removeContainer(util.getObjectProperties(currentLayer, true, false)[i]);
  510. }
  511. }
  512. hasUpdated = true;
  513. }
  514. }
  515. //test clips for audio layer
  516. var tl_clips ;
  517. if(tl_lay.clips!=undefined){
  518. tl_clips = tl_lay.clips.getItems();
  519. //rebuild key structure for currentLayer, "keyList" holds clips
  520. if (keyList.length != tl_clips.length){
  521. var delta = keyList.length-tl_clips.length;
  522. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  523. delta = Math.abs(delta);
  524. script.log("clips "+ delta + (addRemove ? " up" : " down"));
  525. for(var i=0; i<delta; i++){
  526. // script.log(i);
  527. if(addRemove){
  528. var newClip = currentLayer.addContainer("newclip"+i);
  529. newClip.addFileParameter("File Path", "sound file");
  530. newClip.addFloatParameter("start Time", "", 0., 0.);
  531. newClip.setCollapsed(true);
  532. }
  533. else {
  534. // script.log(util.getObjectProperties(currentLayer, true, false)[0]);
  535. currentLayer.removeContainer(util.getObjectProperties(currentLayer, true, false)[i]);
  536. }
  537. }
  538. hasUpdated = true;
  539. }
  540. }
  541. currentLayer = layers.getChild(layersList[layerIndex]);
  542. keyList = util.getObjectProperties(currentLayer, true, false);
  543. //update values for currentLayer
  544. if(tl_keys.length){
  545. for(var i=0; i<tl_keys.length; i++){
  546. var currentKey = currentLayer.getChild(keyList[i]);
  547. currentKey.setName(tl_keys[i].name);
  548. currentKey.time.set(tl_keys[i].position.get());
  549. currentKey.value.set(tl_keys[i].value.get());
  550. }
  551. }
  552. if(tl_clips.length){
  553. for(var i=0; i<tl_clips.length; i++){
  554. var currentKey = currentLayer.getChild(keyList[i]);
  555. currentKey.setName(tl_clips[i].name);
  556. currentKey.filePath.set(tl_clips[i].filePath.get());
  557. currentKey.startTime.set(tl_clips[i].startTime.get());
  558. }
  559. }
  560. }
  561. }
  562. }
  563. function struct_rebuildGroupLayers(){
  564. var layersChanged = false;
  565. var tl_layers = tl_seq.layers.getItems();
  566. layers = local.getChild("values").newGroup.layers ;
  567. var layersList = util.getObjectProperties(layers, true, false);
  568. if (layersList.length != tl_layers.length){
  569. var delta = layersList.length-tl_layers.length;
  570. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  571. delta = Math.abs(delta);
  572. script.log("Group layers "+ delta + (addRemove ? " up" : " down"));
  573. for(var i=0; i<delta; i++){
  574. // script.log(i);
  575. if(addRemove){
  576. local.getChild("values").newGroup.layers.addFloatParameter("newLayer"+i, "value for this layer at group creation", 0., 0., 1.);
  577. }
  578. else {
  579. local.getChild("values").newGroup.layers.removeParameter(util.getObjectProperties(layers, true, false)[i]);
  580. local.getChild("values").newGroup.layers.setCollapsed(true);
  581. local.getChild("values").newGroup.layers.setCollapsed(false);
  582. }
  583. // cuesList = ;
  584. }
  585. layersChanged = true;
  586. }
  587. if(layersChanged){
  588. var template = local.getChild("values").newGroup.layers;
  589. var layerTemplate = util.getObjectProperties(template, true, false);
  590. if(tl_layers.length){
  591. for (var i=0; i<tl_layers.length; i++){
  592. var currentTemplate = template.getChild(layerTemplate[i]) ;
  593. currentTemplate.setName(tl_layers[i].name);
  594. }
  595. }
  596. }
  597. }
  598. //rebuild values.keys structure from sequence
  599. function struct_buildValuesFromSeq(){
  600. inibValuesChanged = true;
  601. var hasUpdated = false ;
  602. var tl_seq = root.sequences.getItemWithName(sequence);
  603. if(tl_seq.name!="undefined"){
  604. if(doSyncKeysCues) {struct_rebuildValues();}
  605. else{struct_rebuildGroupLayers();}
  606. //Groups
  607. tl_seq = root.sequences.getItemWithName(sequence);
  608. var groups = local.getChild("values").groups ;
  609. var groupList = util.getObjectProperties(groups, true, false);
  610. // printMethodsAndProperties(tl_seq.cues.getItemWithName("group0"));
  611. if(groupList.length){
  612. for (var i = 0; i<groupList.length; i++){
  613. //check if cue exist, else remove group
  614. // script.log(groupList[i]);
  615. //v1, remove cue doesn't remove group keys
  616. // var groupCue = tl_seq.cues.getItemWithName(groupList[i]);
  617. // if(groupCue.name==undefined){
  618. // script.log("cue " + groupList[i] + "not found, removing group (keys survive this)");
  619. // groups.removeContainer(groupList[i]);
  620. // hasUpdated = true;
  621. // break;
  622. // }
  623. //v2, remove cue removes group keys
  624. var groupCue = tl_seq.cues.getItemWithName(groupList[i]);
  625. if(groupCue.name==undefined){
  626. script.log("cue " + groupList[i] + "not found, removing keys and group");
  627. var groupToRemove = groups.getChild(groupList[i]);
  628. var groupToRemoveKeyslist = util.getObjectProperties(groupToRemove.keys, true, false);
  629. for(var removeKeyIndex = 0; removeKeyIndex< groupToRemoveKeyslist.length; removeKeyIndex++) {
  630. var tl_removeLayer = tl_seq.layers.getItemWithName( groupToRemoveKeyslist[removeKeyIndex]) ;
  631. var tl_removeKey = tl_removeLayer.automation.getItemWithName(groupToRemove.keys.getChild( groupToRemoveKeyslist[removeKeyIndex]).get());
  632. // script.log(tl_removeKey.name);
  633. tl_removeLayer.automation.removeItem(tl_removeKey);
  634. }
  635. groups.removeContainer(groupList[i]);
  636. hasUpdated = true;
  637. break;
  638. }
  639. //else check at key level
  640. else {
  641. groups = local.getChild("values").groups ;
  642. groupList = util.getObjectProperties(groups, true, false);
  643. var currentGroup = groups.getChild(groupList[i]);
  644. var currentGroupLayers = util.getObjectProperties (currentGroup.keys, true, false);
  645. //update cue time
  646. var groupTime = groupCue.time.get();
  647. currentGroup.cue.set(groupTime);
  648. if (currentGroupLayers.length){
  649. // script.log(currentGroupLayers.length);
  650. for(var j=0; j<currentGroupLayers.length; j++){
  651. // script.log("in loop" + j);
  652. groups = local.getChild("values").groups ;
  653. groupList = util.getObjectProperties(groups, true, false);
  654. currentGroup = groups.getChild(groupList[i]);
  655. currentGroupLayers = util.getObjectProperties (currentGroup.keys, true, false);
  656. var tl_layer = tl_seq.layers.getItemWithName(currentGroupLayers[j]);
  657. var keyName = currentGroup.keys.getChild(tl_layer.name).get();
  658. // script.log(tl_layer.name + " " +tl_layer.automation.getItemWithName(keyName).name);
  659. //if key dosn't exist anymore, remove it from group
  660. // if(tl_layer.automation.getItemWithName(keyName).name == undefined){
  661. // groups = local.getChild("values").groups ;
  662. // groupList = util.getObjectProperties(groups, true, false);
  663. // currentGroup = groups.getChild(groupList[i]);
  664. // currentGroupLayers = util.getObjectProperties (currentGroup.keys, true, false);
  665. // tl_layer = tl_seq.layers.getItemWithName(currentGroupLayers[j]);
  666. // keyName = currentGroup.keys.getChild(tl_layer.name).get();
  667. // script.log("should remove" + currentGroup.name + " " + currentGroupLayers[j]);
  668. // currentGroup.keys.removeParameter(currentGroupLayers[j]);
  669. // currentGroup.removeParameter(currentGroupLayers[j]);
  670. // currentGroup.setCollapsed(true);
  671. // currentGroup.setCollapsed(false);
  672. // hasUpdated = true;
  673. // }
  674. //if key exists, update value from tl and update time from group cue
  675. if(tl_layer.automation.getItemWithName(keyName).name != undefined){
  676. //apply groupTime
  677. var tl_key = tl_layer.automation.getItemWithName(keyName);
  678. tl_key.position.set(groupTime);
  679. //get tl value
  680. var keyValue = tl_key.value.get();
  681. var currentKey = currentGroup.getChild(tl_layer.name);
  682. // script.log(tl_layer.name + " " +keyName+" " + keyValue);
  683. // script.log(currentKey.name + " " +keyName+" " + keyValue);
  684. currentKey.set(keyValue);
  685. // script.log(currentKey.get());
  686. }
  687. }
  688. }
  689. // //if cue doesn't exist, remove whole group
  690. // if(tl_layers.length){
  691. // for (var j=0; j<tl_layers.length; j++){
  692. // if(!tl_layers[j].automation.getItemWithName(groupList[i])){
  693. // script.log("key " + groupList[i] + "not found, removing group (keys survive this)");
  694. // groups.removeContainer(groupList[i]);}
  695. // hasUpdated = true;
  696. // }
  697. // }
  698. //update group values
  699. }
  700. }
  701. }
  702. inibValuesChanged = false;
  703. return hasUpdated;
  704. }
  705. }
  706. // function rebuildValuesStructure() {
  707. // script.log("updating values structure");
  708. // //empty current structure
  709. // // local.values.removeAll();
  710. // local.getChild("Values").removeContainer("newGroup");
  711. // local.getChild("Values").removeContainer("groups");
  712. // local.getChild("Values").removeContainer("ungrouped");
  713. // //rebuild containers
  714. // var ng_cont = local.values.addContainer("New group");
  715. // var g_cont = local.values.addContainer("groups");
  716. // var ug_cont = local.values.addContainer("ungrouped");
  717. // // rebuild new group
  718. // ng_cont.addTrigger("add group", "adds a new group at cursor time with below values");
  719. // ng_cont.addFloatParameter("group time", "time position where the group will be created ", 0., 0.);
  720. // var layer_cont = ng_cont.addContainer("layers values");
  721. // var layers = util.getObjectProperties(sequence_structure.keys.layers) ;
  722. // for (var i=0; i<layers.length; i++){
  723. // //add a float value in each layer for group creation
  724. // layer_cont.addFloatParameter(layers[i], "", 0., 0., 1.);
  725. // ug_cont.addContainer(layers[i]);
  726. // }
  727. // //rebuild keys grouped/ungrouped
  728. // //first create group containers in groups
  729. // // //then create layer containers in ungrouped
  730. // // var layer_cont = ug_cont.addContainer("layers values");
  731. // }
  732. /////////////////////// TIMELINE FUNCTIONS ///////////////////////
  733. function seq_clearSequence(){
  734. var seq = root.sequences.getItemWithName(sequence) ;
  735. script.log("clearing all keys and cues of sequence : "+seq.name);
  736. seq.cues.removeAll();
  737. var layers = seq.layers.getItems();
  738. if(layers.length){
  739. for (var i = 0; i<layers.length; i++){
  740. if(layers[i].automation){layers[i].automation.removeAll();}
  741. if(layers[i].clips){layers[i].clips.removeAll();}
  742. }
  743. }
  744. //clear all groups
  745. var groups = local.getChild("values").groups;
  746. var groupsList = util.getObjectProperties(groups, true, false);
  747. if (groupsList.length){
  748. for(var i=0; i<groupsList.length; i++){
  749. groups.removeContainer(groupsList[i]);
  750. }
  751. }
  752. }
  753. function seq_reorderSequence(){
  754. var seq = root.sequences.getItemWithName(sequence) ;
  755. script.log("reordering sequence : "+seq.name);
  756. var layers = seq.layers.getItems();
  757. if(layers.length){
  758. for (var i = 0; i<layers.length; i++){
  759. if(layers[i].automation){layers[i].automation.reorderItems();}
  760. if(layers[i].clips){layers[i].clips.reorderItems();}
  761. }
  762. }
  763. }
  764. //use Values structure to rebuild the sequence
  765. function seq_buildSeqFromValues(){}
  766. //use file to rebuild sequence
  767. function seq_buildSeqFromFile(){}
  768. function seq_MoveCurrentTime(deltaSeconds){
  769. var tl_seq = root.sequences.getItemWithName(sequence);
  770. var currentTime = tl_seq.currentTime.get();
  771. tl_seq.currentTime.set(currentTime + deltaSeconds);
  772. }
  773. function createNewGroup(){
  774. script.log("create new group");
  775. //create a cue and a key in each layer at cursor position in selected sequence
  776. var groups = local.getChild("values").groups;
  777. var groupsList = util.getObjectProperties(groups, true, false);
  778. // var index = groupsList.length == undefined ? 0 : groupsList.length;
  779. var currentGroup;
  780. var tl_seq = root.sequences.getItemWithName(sequence);
  781. var tl_layers = tl_seq.layers.getItems();
  782. script.log(groupList.length);
  783. //Get current sequence time position and create cue at this position, with group name
  784. var currentTime = tl_seq.currentTime.get() ;
  785. // var currentTime = local.values.newGroup.groupTime.get();
  786. // script.log(currentTime);
  787. newCue = tl_seq.cues.addItem();
  788. newCue.time.set(currentTime);
  789. //test for holes in the sequence to rename the cue
  790. for (var i=0; i<=groupsList.length+1; i++){
  791. if(tl_seq.cues.getChild("group"+i)==undefined){
  792. newCue.setName("group"+i);
  793. currentGroup = groups.addContainer("group"+i);
  794. script.log("create new group: group"+i);
  795. break;
  796. }
  797. }
  798. currentGroup.addFloatParameter("cue", "cue of the group", currentTime, 0.);
  799. var keysContainer = currentGroup.addContainer("keys");
  800. keysContainer.setCollapsed(true);
  801. tl_seq = root.sequences.getItemWithName(sequence);
  802. tl_layers = tl_seq.layers.getItems();
  803. // var struc_layers = util.getObjectProperties(sequence_structure.keys.layers);
  804. if(tl_layers.length){
  805. for(var i=0;i<tl_layers.length; i++ ){
  806. if(tl_layers[i].automation!=undefined){//only for mapping layers
  807. // script.log(sequence_structure.keys.layers[layers[i].name]);
  808. var newKey = tl_layers[i].automation.addItem();
  809. newKey.position.set(currentTime);
  810. newKey.setName(currentGroup.name);
  811. script.log(currentGroup.name); //local.getChild("values").newGroup.layers.getChild().name
  812. newKey.value.set(local.getChild("values").newGroup.layers.getChild(tl_layers[i].name).get());
  813. // newKey.value.set(Math.random());
  814. keysContainer.addStringParameter(tl_layers[i].name, "",currentGroup.name );
  815. currentGroup.addFloatParameter(tl_layers[i].name,tl_layers[i].name+" value" , currentGroup.name, 0., 1.);
  816. // newKey.addStringParameter("group", "key is part of this group","mongroupe");
  817. tl_layers[i].automation.reorderItems();
  818. }
  819. }
  820. }
  821. // seq = seq.getItems();
  822. }
  823. function seq_followCursor(){
  824. var tl_seq = root.sequences.getItemWithName(sequence);
  825. var tl_cursor = tl_seq.currentTime.get();
  826. var tl_totalTime = tl_seq.totalTime.get() ;
  827. var viewTime = followZoom * tl_totalTime ;
  828. var startTime = tl_cursor - viewTime/2. ;
  829. var endTime = tl_cursor + viewTime/2. ;
  830. tl_seq.viewStartTime.set(startTime);
  831. tl_seq.viewEndTime.set(endTime);
  832. }