make_video_texture_scene.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. """A scene whose image layer's texture is a plain video file in the project folder (a texture name that is no .tex falls
  3. back to the file itself, like a user-picked file) and whose script calls the IVideoTexture API on that layer, for the
  4. coverage runs (coverage_scripts.sh).
  5. Usage: make_video_texture_scene.py <out dir> <video file> [runaway]
  6. With "runaway" the ended callback never returns (needs LWE_SCRIPT_TIMEOUT_MS), the script engine drops it."""
  7. import json, os, shutil, sys
  8. out = sys.argv[1]
  9. os.makedirs(out + '/models', exist_ok=True)
  10. os.makedirs(out + '/materials', exist_ok=True)
  11. shutil.copy(sys.argv[2], out + '/clip.mp4')
  12. SCRIPT = r"""
  13. 'use strict';
  14. let frame = 0;
  15. let ended = 0;
  16. const t = (f) => { try { return f(); } catch (e) { return undefined; } };
  17. export function update(value) {
  18. frame++;
  19. const v = t(() => thisLayer.getVideoTexture());
  20. if (!v) return value;
  21. if (frame === 3) {
  22. t(() => v.addEndedCallback(() => { ended++; }));
  23. t(() => v.addEndedCallback(function () { ended++; }));
  24. t(() => v.addEndedCallback());
  25. RUNAWAY
  26. t(() => v.addEndedCallback(7));
  27. t(() => v.isPlaying()); t(() => v.getCurrentTime()); t(() => v.duration);
  28. t(() => v.rate = 2); t(() => v.rate); t(() => v.rate = 0.5); t(() => v.rate = -1); t(() => v.rate = 'x');
  29. t(() => v.loop = false); t(() => v.loop); t(() => v.loop = true);
  30. }
  31. if (frame === 6) { t(() => v.pause()); t(() => v.isPlaying()); t(() => v.setCurrentTime(0.2)); t(() => v.getCurrentTime()); }
  32. if (frame === 9) { t(() => v.play()); t(() => v.isPlaying()); t(() => v.setCurrentTime(-3)); t(() => v.setCurrentTime('x')); t(() => v.setCurrentTime(1e9)); }
  33. if (frame === 12) { t(() => v.loop = false); t(() => v.stop()); t(() => v.isPlaying()); t(() => v.play()); }
  34. if (frame === 15) { t(() => v.loop = true); t(() => v.stop()); t(() => v.play()); t(() => v.setCurrentTime(0.1)); }
  35. if (frame === 20) { t(() => v.setCurrentTime(0.0)); t(() => v.pause()); t(() => v.play()); }
  36. return value;
  37. }
  38. """
  39. script = SCRIPT.replace("RUNAWAY", "t(() => v.addEndedCallback(() => { for (;;) { } }));" if len(sys.argv) > 3 else "")
  40. json.dump({"file": "scene.json", "type": "scene", "title": "video texture", "general": {"properties": {}}},
  41. open(out + '/project.json', 'w'))
  42. json.dump({"autosize": True, "material": "materials/vid.json"}, open(out + '/models/vid.json', 'w'))
  43. json.dump({"passes": [{"shader": "genericimage4", "blending": "translucent", "cullmode": "nocull",
  44. "depthtest": "disabled", "depthwrite": "disabled",
  45. "textures": ["clip.mp4"]}]},
  46. open(out + '/materials/vid.json', 'w'))
  47. json.dump({"version": 3, "camera": {"center": "0 0 -1", "eye": "0 0 0", "up": "0 1 0"},
  48. "general": {"orthogonalprojection": {"width": 1920, "height": 1080}, "clearcolor": "0 0 0",
  49. "bloom": False, "cameraparallax": False},
  50. "objects": [{"id": 2, "name": "vid", "image": "models/vid.json", "origin": "960 540 0",
  51. "size": "1280 720", "scale": "1 1 1", "angles": "0 0 0", "alpha": 1.0, "visible":
  52. {"script": script, "value": True}}]},
  53. open(out + '/scene.json', 'w'), indent=1)
  54. print('ok')