make_puppet_script_scene.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. """A copy of a puppet scene wallpaper whose puppet layer runs a script that calls the animation layer and bone API
  3. (getAnimationLayer*, createAnimationLayer, playSingleAnimation, destroyAnimationLayer, bone transforms, physics
  4. impulses), for the coverage runs (coverage_scripts.sh). The wallpaper is not modified on disk.
  5. Usage: make_puppet_script_scene.py <workshop item dir> <object id of the puppet layer> <out dir> [animation ids, e.g. 105,107]
  6. The animation ids are the clips the script lays over the puppet as lasting additive and normal layers."""
  7. import json, os, shutil, subprocess, sys
  8. item, object_id, out = sys.argv[1], int(sys.argv[2]), sys.argv[3]
  9. animations = [int(x) for x in sys.argv[4].split(',')] if len(sys.argv) > 4 else [105, 107, 112]
  10. here = os.path.dirname(os.path.abspath(__file__))
  11. SCRIPT = r"""
  12. 'use strict';
  13. let frame = 0;
  14. let ended = 0;
  15. const ANIMATIONS = __ANIMATIONS__;
  16. const mats = [new Mat4(), new Mat4().translate(new Vec3(5, 3, 0)), new Mat4().scale(new Vec3(1.1, 0.9, 1))];
  17. export function cursorEnter(e) { ended += 1; }
  18. export function cursorLeave(e) { ended += 1; }
  19. export function cursorDown(e) { ended += 1; }
  20. export function cursorUp(e) { ended += 1; }
  21. export function cursorClick(e) { ended += 1; }
  22. export function update(value) {
  23. frame++;
  24. const t = (f) => { try { return f(); } catch (e) { return undefined; } };
  25. if (frame === 3) {
  26. const count = thisLayer.getAnimationLayerCount();
  27. for (let i = -1; i <= count; i++) {
  28. const l = t(() => thisLayer.getAnimationLayer(i));
  29. if (l) { t(() => l.name); t(() => l.rate = 0.7); t(() => l.blend = 0.4); t(() => l.visible = false); t(() => l.visible = true);
  30. t(() => l.getAnimation()); t(() => l.play()); t(() => l.pause()); t(() => l.stop()); }
  31. }
  32. for (const n of ['nope', '', 'kopf', 'move']) t(() => thisLayer.getAnimationLayer(n));
  33. const first = t(() => thisLayer.getAnimationLayer(0));
  34. if (first) {
  35. t(() => first.addEndedCallback(() => { ended++; }));
  36. t(() => first.addEndedCallback());
  37. t(() => first.addEndedCallback(5));
  38. t(() => first.setFrame(2)); t(() => first.getFrame()); t(() => first.isPlaying());
  39. t(() => first.fps); t(() => first.frameCount); t(() => first.duration);
  40. t(() => first.setFrame(first.frameCount + 5));
  41. }
  42. t(() => thisLayer.getAnimationLayer());
  43. t(() => thisLayer.getAnimationLayer({}));
  44. }
  45. if (frame === 4) {
  46. const made = [];
  47. for (const a of [[105], ['move'], [107, { rate: 2, blend: 0.5 }], [{ animation: 112 }], [], [9999], [null],
  48. [112, '{"rate":1,"blend":1,"name":"extra"}']]) {
  49. made.push(t(() => thisLayer.createAnimationLayer(...a)));
  50. made.push(t(() => thisLayer.playSingleAnimation(...a)));
  51. }
  52. for (const m of made) { if (m) { t(() => m.addEndedCallback(() => { ended++; })); t(() => m.setFrame(0)); t(() => m.rate = 8); t(() => m.rate = 1.5); t(() => m.getAnimation()); t(() => m.name); } }
  53. t(() => thisLayer.getAnimationLayerCount());
  54. for (const m of made) t(() => thisLayer.destroyAnimationLayer(m));
  55. t(() => thisLayer.destroyAnimationLayer(made[0]));
  56. t(() => thisLayer.destroyAnimationLayer('extra'));
  57. t(() => thisLayer.destroyAnimationLayer('nope'));
  58. t(() => thisLayer.destroyAnimationLayer(0));
  59. t(() => thisLayer.destroyAnimationLayer(-1));
  60. t(() => thisLayer.destroyAnimationLayer(99));
  61. t(() => thisLayer.destroyAnimationLayer({}));
  62. t(() => thisLayer.destroyAnimationLayer());
  63. }
  64. if (frame === 11) {
  65. // lasting layers over the puppet: additive ones and normal ones at several blends, so the pose has to weigh them
  66. ANIMATIONS.forEach((id, i) => {
  67. t(() => thisLayer.createAnimationLayer({ animation: id, additive: i % 2 === 0, blend: 0.3 + 0.2 * i, rate: 1 + i,
  68. visible: true, name: 'lasting' + i }));
  69. });
  70. t(() => thisLayer.createAnimationLayer({ animation: ANIMATIONS[0], additive: false, blend: 1, visible: true }));
  71. }
  72. if (frame >= 5 && frame <= 8) {
  73. const bones = thisLayer.getBoneCount();
  74. for (let i = -1; i <= bones; i += Math.max(1, Math.floor(bones / 12))) {
  75. t(() => thisLayer.getBoneParentIndex(i));
  76. for (const m of mats) {
  77. t(() => thisLayer.getBoneTransform(i));
  78. t(() => thisLayer.setBoneTransform(i, m));
  79. t(() => thisLayer.getLocalBoneTransform(i));
  80. t(() => thisLayer.setLocalBoneTransform(i, m));
  81. }
  82. t(() => thisLayer.getLocalBoneAngles(i));
  83. t(() => thisLayer.setLocalBoneAngles(i, new Vec3(0, 0, 0.2 * frame)));
  84. t(() => thisLayer.getLocalBoneOrigin(i));
  85. t(() => thisLayer.setLocalBoneOrigin(i, new Vec3(1, 2, 0)));
  86. t(() => thisLayer.applyBonePhysicsImpulse(i, new Vec3(30, 0, 0), new Vec3(0, 10, 0)));
  87. t(() => thisLayer.resetBonePhysicsSimulation(i));
  88. }
  89. for (const n of ['', 'nope', 'root']) {
  90. t(() => thisLayer.getBoneIndex(n));
  91. t(() => thisLayer.getBoneParentIndex(n));
  92. t(() => thisLayer.getBoneTransform(n));
  93. t(() => thisLayer.setLocalBoneAngles(n, new Vec3(0, 0, 1)));
  94. t(() => thisLayer.applyBonePhysicsImpulse(n, new Vec3(1, 0, 0), new Vec3(0, 1, 0)));
  95. t(() => thisLayer.resetBonePhysicsSimulation(n));
  96. }
  97. t(() => thisLayer.getBoneIndex(0));
  98. t(() => thisLayer.setBoneTransform(0));
  99. t(() => thisLayer.applyBonePhysicsImpulse(0));
  100. }
  101. if (frame >= 10 && frame <= 24) {
  102. const effects = thisLayer.getEffectCount();
  103. for (let i = 0; i < effects; i++) {
  104. const e = t(() => thisLayer.getEffect(i));
  105. if (e) { t(() => e.visible = (frame + i) % 3 !== 0); t(() => e.name = 'fx' + frame); t(() => e.name); t(() => e.name = 5); }
  106. }
  107. if (frame % 4 === 0) t(() => thisLayer.colorBlendMode = frame % 8 === 0 ? 0 : 9);
  108. }
  109. return value;
  110. }
  111. """
  112. os.makedirs(out, exist_ok=True)
  113. subprocess.run([sys.executable, '-I', os.path.join(here, '..', 'unpack_pkg.py'), os.path.join(item, 'scene.pkg'), out],
  114. check=True, stdout=subprocess.DEVNULL)
  115. shutil.copy(os.path.join(item, 'project.json'), os.path.join(out, 'project.json'))
  116. path = os.path.join(out, 'scene.json')
  117. scene = json.load(open(path))
  118. for obj in scene['objects']:
  119. if obj.get('id') == object_id:
  120. obj['visible'] = {'script': SCRIPT.replace('__ANIMATIONS__', json.dumps(animations)), 'value': True}
  121. break
  122. else:
  123. sys.exit('no object %d in %s' % (object_id, item))
  124. json.dump(scene, open(path, 'w'))
  125. print('ok')