SequenceMgr.js 37 KB

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