SequenceMgr.js 32 KB

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