SequenceMgr.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 debug = true;
  16. var debugFile;
  17. var parametersPath = local.parameters ;
  18. var sequence ;
  19. var sequencePath = local.parameters.sequence ;
  20. var file ;
  21. var filePath = local.parameters.sequenceFile ;
  22. var newGroupLayers = local.values.newGroup.layers ;
  23. var groups = local.getChild("values").groups ;
  24. var cues = local.getChild("values").keys.cues ;
  25. // var cuesList = []; //holds cues containers reference
  26. var layers = local.getChild("values").keys.layers ;
  27. // var layerList = {}; //holds layer container as object with reference and an array of keys containers references
  28. var rebuildValues = script.addTrigger("rebuild values", "adds a key at current cursor position" );
  29. var refreshKeys = script.addTrigger("refresh keys", "truc");
  30. var printKeys = script.addTrigger("printKeys","truc");
  31. var clearSeq = script.addTrigger("clear Sequence","truc");
  32. var sequence_structure = { params: {},
  33. groups:{},
  34. keys:{
  35. cues:{},
  36. layers:{}
  37. }
  38. };
  39. //WAS tl_groups
  40. /*group0 : { inCue : {pan : "key", tilt : "key", zoom : "key", dim : "key", focus : "key"},
  41. outCue : {pan : "key", tilt : "key", zoom : "key", dim : "key", focus : "key"}
  42. }*/
  43. ///////////////////// SCRIPT FUNCTIONS //////////////////////////////
  44. function init() {
  45. script.log("LOADED SEQUENCE GROUPING MANAGEMENT MODULE ");
  46. //adapt update rate
  47. updateSyncRate();
  48. updateSynchronize();
  49. // layers = local.getChild("values").keys.layers ;
  50. // printMethodsAndProperties(layers);
  51. //test if a sequence is already choosen else refresh and unlock sequence enum
  52. sequence = sequencePath.getKey();
  53. if(sequence == ""){refreshSequenceEnum();}
  54. else{sequencePath.setAttribute("readonly", true);}
  55. }
  56. function scriptParameterChanged(param){
  57. if (param.is(printKeys) ){printKeys();}
  58. else if(param.is(rebuildValues)){struct_buildValuesFromSeq();}
  59. else if (param.is(refreshKeys)){
  60. script.log("refreshKeys");
  61. refreshKeysList();
  62. }
  63. else if (param.is(clearSeq)){seq_clearSequence();}
  64. else {script.log("scriptparamchanged" + param.name);}
  65. }
  66. function update(deltaTime){
  67. if(doSync){
  68. syncDelta += deltaTime ;
  69. // script.log(syncDelta);
  70. if (syncDelta >= syncRatems){
  71. syncDelta = 0. ;
  72. struct_buildValuesFromSeq();
  73. }
  74. }
  75. }
  76. /////////////////////////// HELPERS /////////////////////////////////
  77. function printMethodsAndProperties(obj){
  78. script.log(" ");
  79. var name = obj.getControlAddress() == undefined ? "object" : obj.getControlAddress();
  80. // var name = "object";
  81. script.log(" METHODS OF " + name);
  82. var methods = util.getObjectMethods(obj);
  83. for(var i=0 ; i<methods.length ; i++){
  84. script.log(" "+ methods[i]);
  85. }
  86. script.log(" PARAMETERS OF " + name);
  87. var props = util.getObjectProperties(obj, true, false);
  88. for(var i=0 ; i<props.length ; i++){
  89. script.log(" "+ props[i]);
  90. }
  91. script.log(" OBJECTS OF " + name);
  92. var props = util.getObjectProperties(obj, false, true);
  93. for(var i=0 ; i<props.length ; i++){
  94. script.log(" "+ props[i]);
  95. }
  96. }
  97. function printKeys(){
  98. var seq = root.sequences.sequence.layers.mapping.automation.getItems() ;
  99. if(seq.length){
  100. for(var i=0; i<seq.length; i++){
  101. script.log (">> "+seq[i].name + " " + seq[i].position.get() + " " + seq[i].listSize.get() );
  102. }
  103. }
  104. else{script.log("nothing to print");}
  105. }
  106. /////////////////////// PARAMETERS FUNCTIONS ///////////////////////
  107. // Module callback
  108. function moduleParameterChanged(param){
  109. if (param.is(parametersPath.importExport.sequenceFile)) {}
  110. else if (param.is(parametersPath.importExport.loadSequence)){importSequenceFile();}
  111. else if (param.is(parametersPath.importExport.exportSequence)) { exportSequenceFile();}
  112. else if (param.is(parametersPath.synchronize)){updateSynchronize();}
  113. else if (param.is(parametersPath.syncRate)){updateSyncRate();}
  114. else if (param.is(parametersPath.changeSequence)){refreshSequenceEnum();}
  115. else if (param.is(parametersPath.sequence)){
  116. sequence = sequencePath.getKey();
  117. script.log("watching "+ sequence);
  118. }
  119. else{script.log("received module param :" + param.name);}
  120. }
  121. function updateSynchronize(){
  122. doSync = parametersPath.synchronize.get();
  123. script.log(doSync ? "sync ON" : "sync OFF" );
  124. }
  125. function updateSyncRate(){
  126. syncRatems = parametersPath.syncRate.get() / 1000.;
  127. script.log("new sync rate : " + syncRatems);
  128. // script.setUpdateRate(syncRatems);
  129. }
  130. //refresh Enum
  131. function refreshSequenceEnum(){
  132. script.log("refresh list of sequences");
  133. local.parameters.sequence.setAttribute("readonly",false);
  134. if(root.sequences.getItems().length){
  135. local.parameters.sequence.removeOptions();
  136. for(var i=0 ; i<root.sequences.getItems().length ; i++ ){
  137. local.parameters.sequence.addOption(root.sequences.getItems()[i].name,i);
  138. }
  139. }
  140. }
  141. function testSequenceFile(){
  142. var file = local.parameters.sequenceFile;
  143. //test if a file is declared
  144. if( file.getAbsolutePath() != ""){
  145. script.log("testing file " + file.getAbsolutePath() );
  146. var data = file.readFile(true);
  147. // test if expected fields are absent
  148. if (data.groups == undefined){
  149. script.log("file structure not recognized");
  150. refreshSequenceEnum();
  151. }
  152. //if all is well, let's load in sequence_structure
  153. else {
  154. script.log("ready to import");
  155. return 1;
  156. // loadSequenceFile();
  157. }
  158. }
  159. else {
  160. script.log("no file to load");
  161. refreshSequenceEnum();
  162. return 0;
  163. }
  164. }
  165. function importSequenceFile(){
  166. script.log("loading file");
  167. sequence_structure = local.parameters.sequenceFile.readFile(true);
  168. //lock enum with curent sequence name
  169. var sequenceList = local.parameters.sequence;
  170. sequenceList.removeOptions();
  171. sequenceList.addOption(sequence_structure.params.sequence, 0);
  172. local.parameters.sequence.setAttribute("readonly",true);
  173. }
  174. function exportSequenceFile(){
  175. script.log("exporting file");
  176. local.parameters.sequenceFile.writeFile(JSON.stringify(sequence_structure), true);
  177. }
  178. /////////////////////// VALUES FUNCTIONS ///////////////////////
  179. /* Values hold all keys, presented in group containers
  180. a nogroup container shows keys that are not grouped
  181. */
  182. // Value callback
  183. function moduleValueChanged(value){
  184. if(!inibValuesChanged){
  185. doSync = false;
  186. script.log(value.getParent().name);
  187. doSync = true;
  188. }
  189. if (value.is(local.values.newGroup.create)){createNewGroup();}
  190. // script.log(value.getParent().name);
  191. // if(value.getParent().name = "cue"){script.log("changed");}
  192. if (value.name == "addGroup"){createNewGroup();}
  193. }
  194. //rebuild values.keys structure from sequence
  195. function struct_buildValuesFromSeq(){
  196. inibValuesChanged = true;
  197. var tl_seq = root.sequences.getItemWithName(sequence);
  198. //build cues if needed
  199. var tl_cues = tl_seq.cues.getItems();
  200. cues = local.getChild("values").keys.cues ;
  201. var cuesList = util.getObjectProperties(cues, true, false);
  202. // script.log("cueList : " +cuesList.length);
  203. // script.log("tl List : " +tl_cues.length);
  204. if (cuesList.length != tl_cues.length){
  205. var delta = cuesList.length-tl_cues.length;
  206. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  207. delta = Math.abs(delta);
  208. script.log("change "+ delta + (addRemove ? " up" : " down"));
  209. for(var i=0; i<delta; i++){
  210. // script.log(i);
  211. if(addRemove){
  212. var newCue = cues.addContainer("newCue"+i);
  213. newCue.addFloatParameter("time", "position in time", 0., 0.);
  214. newCue.setCollapsed(true);
  215. }
  216. else {
  217. script.log(util.getObjectProperties(cues, true, false)[0]);
  218. cues.removeContainer(util.getObjectProperties(cues, true, false)[0]);
  219. }
  220. // cuesList = ;
  221. }
  222. }
  223. //update cues
  224. cues = local.getChild("values").keys.cues ;
  225. var cuesList = util.getObjectProperties(cues, true, false);
  226. if(tl_cues.length){
  227. for (var i=0; i<tl_cues.length; i++){
  228. var currentCue = cues.getChild(cuesList[i]);
  229. currentCue.setName(tl_cues[i].name);
  230. currentCue.time.set(tl_cues[i].time.get());
  231. }
  232. }
  233. //build layers if needed
  234. var layersChanged = false ;
  235. var tl_layers = tl_seq.layers.getItems();
  236. layers = local.getChild("values").keys.layers ;
  237. var layersList = util.getObjectProperties(layers, true, false);
  238. if (layersList.length != tl_layers.length){
  239. layersChanged = true ;
  240. var delta = layersList.length-tl_layers.length;
  241. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  242. delta = Math.abs(delta);
  243. script.log("change "+ delta + (addRemove ? " up" : " down"));
  244. for(var i=0; i<delta; i++){
  245. // script.log(i);
  246. if(addRemove){
  247. var newLayer = layers.addContainer("newLayer"+i);
  248. // newLayer.addFloatParameter("time", "position in time", 0., 0.);
  249. newLayer.setCollapsed(true);
  250. }
  251. else {
  252. script.log(util.getObjectProperties(layers, true, false)[0]);
  253. layers.removeContainer(util.getObjectProperties(layers, true, false)[0]);
  254. }
  255. // cuesList = ;
  256. }
  257. }
  258. //update layers value if there was a change in structure
  259. if(layersChanged){
  260. layers = local.getChild("values").keys.layers ;
  261. var layerList = util.getObjectProperties(layers, true, false);
  262. if(tl_layers.length){
  263. for (var i=0; i<tl_layers.length; i++){
  264. var currentLayer = layers.getChild(layerList[i]);
  265. currentLayer.setName(tl_layers[i].name);
  266. //
  267. }
  268. }
  269. }
  270. //build keys if needed
  271. layers = local.getChild("values").keys.layers ;
  272. layersList = util.getObjectProperties(layers, true, false);
  273. if(layersList.length){
  274. for(var layerIndex=0; layerIndex<layersList.length; layerIndex++){
  275. var currentLayer = layers.getChild(layersList[layerIndex]);
  276. var keyList = util.getObjectProperties(currentLayer, true, false);
  277. var tl_lay = tl_seq.layers.getItemWithName(layersList[layerIndex]);
  278. var tl_keys;
  279. if(tl_lay.automation!=undefined){tl_keys = tl_lay.automation.getItems();}
  280. //rebuild key structure for currentLayer
  281. if (keyList.length != tl_keys.length){
  282. var delta = keyList.length-tl_keys.length;
  283. var addRemove = delta > 0 ? false : true ; //0 to remove, 1 to add
  284. delta = Math.abs(delta);
  285. script.log("change "+ delta + (addRemove ? " up" : " down"));
  286. for(var i=0; i<delta; i++){
  287. // script.log(i);
  288. if(addRemove){
  289. var newKey = currentLayer.addContainer("newkey"+i);
  290. newKey.addFloatParameter("time", "position in time", 0., 0.);
  291. newKey.addFloatParameter("value", "vertical value", 0., 0.);
  292. newKey.setCollapsed(true);
  293. }
  294. else {
  295. script.log(util.getObjectProperties(currentLayer, true, false)[0]);
  296. currentLayer.removeContainer(util.getObjectProperties(currentLayer, true, false)[0]);
  297. }
  298. }
  299. }
  300. currentLayer = layers.getChild(layersList[layerIndex]);
  301. keyList = util.getObjectProperties(currentLayer, true, false);
  302. //update values for currentLayer
  303. if(tl_keys.length){
  304. for(var i=0; i<tl_keys.length; i++){
  305. var currentKey = currentLayer.getChild(keyList[i]);
  306. currentKey.setName(tl_keys[i].name);
  307. currentKey.time.set(tl_keys[i].position.get());
  308. currentKey.value.set(tl_keys[i].value.get());
  309. }
  310. }
  311. }
  312. }
  313. inibValuesChanged = false;
  314. }
  315. function struct_exportToFile(){}
  316. // function rebuildValuesStructure() {
  317. // script.log("updating values structure");
  318. // //empty current structure
  319. // // local.values.removeAll();
  320. // local.getChild("Values").removeContainer("newGroup");
  321. // local.getChild("Values").removeContainer("groups");
  322. // local.getChild("Values").removeContainer("ungrouped");
  323. // //rebuild containers
  324. // var ng_cont = local.values.addContainer("New group");
  325. // var g_cont = local.values.addContainer("groups");
  326. // var ug_cont = local.values.addContainer("ungrouped");
  327. // // rebuild new group
  328. // ng_cont.addTrigger("add group", "adds a new group at cursor time with below values");
  329. // ng_cont.addFloatParameter("group time", "time position where the group will be created ", 0., 0.);
  330. // var layer_cont = ng_cont.addContainer("layers values");
  331. // var layers = util.getObjectProperties(sequence_structure.keys.layers) ;
  332. // for (var i=0; i<layers.length; i++){
  333. // //add a float value in each layer for group creation
  334. // layer_cont.addFloatParameter(layers[i], "", 0., 0., 1.);
  335. // ug_cont.addContainer(layers[i]);
  336. // }
  337. // //rebuild keys grouped/ungrouped
  338. // //first create group containers in groups
  339. // // //then create layer containers in ungrouped
  340. // // var layer_cont = ug_cont.addContainer("layers values");
  341. // }
  342. /////////////////////// TIMELINE FUNCTIONS ///////////////////////
  343. function seq_clearSequence(){
  344. var seq = root.sequences.getItemWithName(sequence) ;
  345. script.log("clearing all keys and cues of sequence : "+seq.name);
  346. seq.cues.removeAll();
  347. var layers = seq.layers.getItems();
  348. if(layers.length){
  349. for (var i = 0; i<layers.length; i++){
  350. layers[i].automation.removeAll();
  351. }
  352. }
  353. }
  354. //use Values structure to rebuild the sequence
  355. function seq_buildSeqFromValues(){}
  356. //use file to rebuild sequence
  357. function seq_buildSeqFromFile(){}
  358. // function refreshKeysList(){
  359. // var sequence = root.sequences.getItemWithName(sequence_structure.params.sequence);
  360. // var cues = sequence.cues.getItems();
  361. // sequence_structure.keys.cues = {};
  362. // if(cues.length){
  363. // for (var i = 0 ; i < cues.length ; i++){
  364. // sequence_structure.keys.cues['cue'+i] = {index : i};
  365. // sequence_structure.keys['cues']['cue'+i]['name'] = cues[i].name;
  366. // sequence_structure.keys['cues']['cue'+i]['niceName'] = cues[i].niceName;
  367. // sequence_structure.keys['cues']['cue'+i]['time'] = cues[i].time.get();
  368. // // script.log(tl_keyframes['cues']['cue'+i].time);
  369. // }
  370. // }
  371. // var layers = sequence.layers.getItems();
  372. // sequence_structure.keys.layers={};
  373. // if(layers.length){
  374. // for (var i = 0 ; i < layers.length ; i++){
  375. // sequence_structure['keys']['layers'][layers[i].name] = {};
  376. // // script.log(currentLayer.name);
  377. // if(layers[i].automation.getItems().length){
  378. // // script.log(currentLayer.automation.items.length);
  379. // for (var j = 0 ; j < layers[i].automation.getItems().length ; j++){
  380. // var currentkey = layers[i].automation.getItems()[j] ;
  381. // sequence_structure['keys']['layers'][layers[i].name][currentkey.name] = {
  382. // position : currentkey.position.get(),
  383. // value : currentkey.value.get(),
  384. // name : currentkey.name,
  385. // niceName : currentkey.niceName,
  386. // //la propriete "group" est écrite
  387. // group : currentkey.group
  388. // // group : currentkey.hasChild("group") != undefined ? currentkey.group.get() : "ungrouped"
  389. // };
  390. // // script.log(tl_keyframes['keys'][currentLayer.name][currentkey.name].value);
  391. // }
  392. // }
  393. // }
  394. // }
  395. // // myFile.writeFile(JSON.stringify(tl_keyframes), 1);
  396. // }
  397. // function writeKeyGroup(){
  398. // var groups = sequence_structure.groups
  399. // }
  400. function createNewGroup(){
  401. script.log("create new group");
  402. //create a cue and a key in each layer at cursor position in selected sequence
  403. groups = local.getChild("values").groups;
  404. var groupsList = util.getObjectProperties(groups, true, false);
  405. var index = groupsList.length == undefined ? 0 : groupsList.length;
  406. var currentGroup = groups.addContainer("Group"+ index);
  407. var tl_seq = root.sequences.getItemWithName(sequence);
  408. var tl_layers = tl_seq.layers.getItems();
  409. script.log(groupList.length);
  410. //Get current sequence time position and create cue at this position, with group name
  411. var currentTime = tl_seq.currentTime.get() ;
  412. // var currentTime = local.values.newGroup.groupTime.get();
  413. script.log(currentTime);
  414. newCue = tl_seq.cues.addItem();
  415. newCue.time.set(currentTime);
  416. newCue.setName(currentGroup.name);
  417. currentGroup.addFloatParameter(newCue.name, "cue of the group", currentTime, 0.);
  418. // var struc_layers = util.getObjectProperties(sequence_structure.keys.layers);
  419. if(tl_layers.length){
  420. for(var i=0;i<tl_layers.length; i++ ){
  421. if(tl_layers[i].automation!=undefined){//only for mapping layers
  422. // script.log(sequence_structure.keys.layers[layers[i].name]);
  423. var newKey = tl_layers[i].automation.addItem();
  424. newKey.position.set(currentTime);
  425. // newKey.value.set(local.values.newGroup.layersValues[layers[i].name].get());
  426. newKey.value.set(Math.random());
  427. currentGroup.addFloatParameter(tl_layers[i].name,tl_layers[i].name+" value" , newKey.value.get(), 0.);
  428. // newKey.addStringParameter("group", "key is part of this group","mongroupe");
  429. tl_layers[i].automation.reorderItems();
  430. }
  431. }
  432. }
  433. // seq = seq.getItems();
  434. }