Advertisement
Artelis

Dance Command

Feb 24th, 2022
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Newtonsoft.Json;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. public class Scale
  8. {
  9.     public double x;
  10.     public double y;
  11.     public String filter;
  12. }
  13.  
  14. public class Crop
  15. {
  16.     public int top;
  17.     public int bottom;
  18.     public int left;
  19.     public int right;
  20. }
  21.  
  22. public class Item
  23. {
  24.     public int id;
  25.     public string name;
  26. }
  27.  
  28. public class Position
  29. {
  30.     public double x;
  31.     public double y;
  32. }
  33.  
  34. public class Transform
  35. {
  36.     public Position position;
  37.     public double rotation;
  38.     public Item item;
  39.     public Scale scale;
  40.     public Crop crop;
  41. }
  42.  
  43. public class VideoInfo
  44. {
  45.     public int baseWidth;
  46.     public int baseHeight;
  47.     public int outputWidth;
  48.     public int outputHeight;
  49.     public String scaleType;
  50.     public double fps;
  51.     public String videoFormat;
  52.     public String colorSpace;
  53.     public String colorRange;
  54. }
  55.  
  56. public class SourceSettings
  57. {
  58.     public String souceName;
  59.     public String sourceType;
  60.     public Object sourceSettings;
  61. }
  62.  
  63. public class Scene
  64. {
  65.     public String name;
  66. }
  67.  
  68. public class SceneItem
  69. {
  70.     public int itemId;
  71.     public String sourceKind;
  72.     public String sourceName;
  73.     public String sourceType;
  74. }
  75.  
  76. public class SceneItemList
  77. {
  78.     public String sourceName;
  79.     public List<SceneItem> sceneItems;
  80. }
  81.  
  82. public class DuplicatedSceneItemResponse
  83. {
  84.     public String scene;
  85.     public Item item;
  86. }
  87.  
  88. public class CPHInline
  89. {
  90.     int tweenCount;
  91.     public void Init()
  92.     {
  93.         tweenCount = 0;
  94.     }
  95.  
  96.     public bool Execute()
  97.     {
  98.         int dadScale = 10;
  99.         int shakeLength = 36*dadScale;
  100.         int spinTimes = 3*dadScale;
  101.         int xSpacing = 500;
  102.         int ySpacing = 300;
  103.         String sourceName = "Lumix Greenscreen";
  104.         List<int> ids = new List<int>();
  105.         int listLength = 9;
  106.         double scale = 0.75;
  107.  
  108.         Transform t = getTransform(sourceName);
  109.         t.visible = false;
  110.         t.scale.x *= scale;
  111.         t.scale.y *= scale;
  112.         setTransform(t, sourceName);
  113.         int tWidth = (int)Math.Abs((t.sourceWidth * t.scale.x)) - (int)Math.Abs((t.crop.left * t.scale.x));
  114.         int tHeight = (int)Math.Abs((t.sourceHeight * t.scale.y));
  115.  
  116.         VideoInfo info = getVideoInfo();
  117.        
  118.         int cX = info.baseWidth/2;
  119.         int cY = info.baseHeight/2;
  120.  
  121.         SceneItem cam1 = getSceneItemNamed(sourceName);
  122.  
  123.         for(int i = 0; i < listLength; i++)
  124.         {
  125.             for(int j = 0; j < 4; j++) {
  126.                 ids.Add(copySource(sourceName, -3000,3000));
  127.                 Transform nt = getTransform("",ids[i*4+j]);
  128.                 nt.visible = true;
  129.                 setTransform(nt, "",ids[i*4+j]);
  130.             }
  131.         }
  132.  
  133.         int id1 = cam1.itemId;
  134.         t.visible = true;
  135.         setTransform(t, "", id1);
  136.  
  137.         //phase 1:
  138.         //  4 corners
  139.         q.setPosition(tWidth/2, tHeight/2, "", id1);
  140.         int id2 = copySource(sourceName, tWidth/2, info.baseHeight - tHeight/2);
  141.         int id3 = copySource(sourceName, info.baseWidth - tWidth/2, tHeight/2);
  142.         int id4 = copySource(sourceName, info.baseWidth - tWidth/2, info.baseHeight - tHeight/2);
  143.  
  144.         setRotation(45, "", id2);
  145.         setRotation(135, "", id1);
  146.         setRotation(225, "", id3);
  147.         setRotation(315, "", id4);
  148.  
  149.         Thread.Sleep(3000);
  150.  
  151.         //phase 2:
  152.         //  4 corner pass through
  153.         tween(info.baseWidth - tWidth, info.baseHeight - tHeight, 30, "", 180, id1);
  154.         tween(-info.baseWidth + tWidth, -info.baseHeight + tHeight, 30, "", 180, id4);
  155.         tween(info.baseWidth - tWidth, -info.baseHeight + tHeight, 30, "", 180, id2);
  156.         tween(-info.baseWidth + tWidth, info.baseHeight - tHeight, 30, "", 180, id3);
  157.        
  158.  
  159.         Thread.Sleep(3000);
  160.  
  161.         //phase 3:
  162.         //  Straighten and Cascade
  163.         tween(0, 0, 5, "", 45, id1);
  164.         tween(0, tHeight, 5, "", -45, id2);
  165.         tween(0, -tHeight, 5, "", 135, id3);
  166.         tween(0, 0, 5, "", -135, id4);
  167.         Thread.Sleep(500);
  168.         int index = 0;
  169.         int spacing = 200;
  170.         for(int i = 0; i < listLength; i++)
  171.         {
  172.             setRotation(-180, "", ids[index+1]);
  173.             setRotation(180, "", ids[index+2]);
  174.  
  175.             setPosition(info.baseWidth - tWidth/2 - i*spacing, info.baseHeight - tHeight/2, "", ids[index]);
  176.             setPosition(info.baseWidth - tWidth/2 - i*spacing, (int)(tHeight*1.5), "", ids[index+1]);
  177.             setPosition(tWidth/2 + i*spacing, info.baseHeight - (int)(tHeight*1.5), "", ids[index+2]);
  178.             setPosition(tWidth/2 + i*spacing, tHeight/2, "", ids[index+3]);
  179.             index += 4;
  180.             Thread.Sleep(50);
  181.         }
  182.  
  183.         for(int i = 0; i < listLength; i++)
  184.         {
  185.             deleteSource("", ids[i*4]);
  186.             deleteSource("", ids[i*4+1]);
  187.             deleteSource("", ids[i*4+2]);
  188.             deleteSource("", ids[i*4+3]);
  189.             Thread.Sleep(50);
  190.         }
  191.  
  192.         Thread.Sleep(2000);
  193.  
  194.         //phase 4:
  195.         //  Spin away
  196.         tween(-3000, 0, 60, "", 9000, id2);
  197.         tween(-1000, 0, 80, "", 9000, id3);
  198.         tween(-1000, 0, 80, "", 9000, id4);
  199.         Thread.Sleep(3000);
  200.  
  201.         deleteSource("", id2);
  202.         deleteSource("", id3);
  203.         deleteSource("", id4);
  204.         foreach(int i in ids) deleteSource("", i);
  205.        
  206.        
  207.         t.scale.x /= scale;
  208.         t.scale.y /= scale;
  209.         setTransform(t, "", id1);
  210.  
  211.         return true;
  212.     }
  213.  
  214.     SourceSettings getSourceSettings(String sourceName)
  215.     {
  216.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  217.         getRequest.Add("sourceName", sourceName);
  218.         String requestResponse = CPH.ObsSendRaw("GetSourceSettings", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  219.         SourceSettings responseJson = JsonConvert.DeserializeObject<SourceSettings>(requestResponse);
  220.         return responseJson;
  221.     }
  222.  
  223.     Scene getCurrentScene()
  224.     {
  225.         String requestResponse = CPH.ObsSendRaw("GetCurrentScene", "");
  226.         Scene sceneObj = JsonConvert.DeserializeObject<Scene>(requestResponse);
  227.         return sceneObj;
  228.     }
  229.  
  230.     SceneItemList getSceneItemList()
  231.     {
  232.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  233.         getRequest.Add("sourceName", "garbage");
  234.         String requestResponse = CPH.ObsSendRaw("GetSceneItemList", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  235.         SceneItemList responseJson = JsonConvert.DeserializeObject<SceneItemList>(requestResponse);
  236.         return responseJson;
  237.     }
  238.  
  239.     SceneItem getSceneItemNamed(String name)
  240.     {
  241.         SceneItemList list = getSceneItemList();
  242.         foreach (SceneItem i in list.sceneItems)
  243.         {
  244.             if (i.sourceName.Equals(name))
  245.             {
  246.                 return i;
  247.             }
  248.         }
  249.  
  250.         return null;
  251.     }
  252.  
  253.     SceneItem getSceneItemById(int id)
  254.     {
  255.         SceneItemList list = getSceneItemList();
  256.         foreach (SceneItem i in list.sceneItems)
  257.         {
  258.             if (i.itemId == id)
  259.             {
  260.                 return i;
  261.             }
  262.         }
  263.  
  264.         return null;
  265.     }
  266.  
  267.     void deleteSource(String name, int id = -1)
  268.     {
  269.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  270.         Dictionary<String, object> itemRequest = new Dictionary<String, object>();
  271.         if (!name.Equals(""))
  272.             itemRequest.Add("name", name);
  273.         if (id != -1)
  274.             itemRequest.Add("id", id);
  275.         updateRequest.Add("item", itemRequest);
  276.         CPH.ObsSendRaw("DeleteSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  277.     }
  278.  
  279.     int copySource(String name, int x = 0, int y = 0, int rotation = 0)
  280.     {
  281.         SceneItem item = getSceneItemNamed(name);
  282.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  283.         Dictionary<String, object> itemRequest = new Dictionary<String, object>();
  284.         itemRequest.Add("id", item.itemId);
  285.         updateRequest.Add("item", itemRequest);
  286.         String response = CPH.ObsSendRaw("DuplicateSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  287.         DuplicatedSceneItemResponse newItemResponse = JsonConvert.DeserializeObject<DuplicatedSceneItemResponse>(response);
  288.         Transform t = getTransform("", item.itemId);
  289.         t.item = new Item();
  290.         t.item.id = newItemResponse.item.id;
  291.         t.item.name = newItemResponse.item.name;
  292.         setTransform(t, "", newItemResponse.item.id);
  293.         setPosition(x, y, "", newItemResponse.item.id);
  294.         setRotation(rotation, "", newItemResponse.item.id);
  295.         return newItemResponse.item.id;
  296.     }
  297.  
  298.     void setPosition(int x, int y, String item, int id = -1)
  299.     {
  300.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  301.         if (id == -1)
  302.             updateRequest.Add("item", item);
  303.         else
  304.         {
  305.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  306.             itemDict.Add("id", id);
  307.             updateRequest.Add("item", itemDict);
  308.         }
  309.  
  310.         Dictionary<String, object> position = new Dictionary<String, object>();
  311.         position.Add("x", x);
  312.         position.Add("y", y);
  313.         updateRequest.Add("position", position);
  314.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  315.     }
  316.  
  317.     void setRotation(int angle, String item, int id = -1)
  318.     {
  319.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  320.         if (id == -1)
  321.             updateRequest.Add("item", item);
  322.         else
  323.         {
  324.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  325.             itemDict.Add("id", id);
  326.             updateRequest.Add("item", itemDict);
  327.         }
  328.  
  329.         updateRequest.Add("rotation", angle);
  330.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  331.     }
  332.  
  333.     Transform getTransform(String item, int id = -1)
  334.     {
  335.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  336.         if (id == -1)
  337.             getRequest.Add("item", item);
  338.         else
  339.         {
  340.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  341.             itemDict.Add("id", id);
  342.             getRequest.Add("item", itemDict);
  343.         }
  344.  
  345.         String requestResponse = CPH.ObsSendRaw("GetSceneItemProperties", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  346.         Transform t = JsonConvert.DeserializeObject<Transform>(requestResponse);
  347.         return t;
  348.     }
  349.  
  350.     void setTransform(Transform t, String itemName, int id = -1)
  351.     {
  352.         SceneItem item;
  353.         if (id == -1)
  354.             item = getSceneItemNamed(itemName);
  355.         else
  356.             item = getSceneItemById(id);
  357.         if (item == null)
  358.             return;
  359.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(t, Formatting.Indented));
  360.     }
  361.  
  362.     void TweenThread(double x, double y, int frames, String item, double rotation = 0, int id = -1)
  363.     {
  364.         Transform t;
  365.         if (id == -1)
  366.             t = getTransform(item);
  367.         else
  368.             t = getTransform("", id);
  369.         double sX = t.position.x;
  370.         double sY = t.position.y;
  371.         double sR = t.rotation;
  372.         for (int i = 1; i - 1 < frames; i++)
  373.         {
  374.             Thread.Sleep((int)(1.0 / 60.0 * 1000.0));
  375.             Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  376.             if (id == -1)
  377.                 updateRequest.Add("item", item);
  378.             else
  379.             {
  380.                 Dictionary<String, object> itemDict = new Dictionary<String, object>();
  381.                 itemDict.Add("id", id);
  382.                 updateRequest.Add("item", itemDict);
  383.             }
  384.  
  385.             Dictionary<String, object> position = new Dictionary<String, object>();
  386.             position.Add("x", sX + (x * ((double)i / (double)frames)));
  387.             position.Add("y", sY + (y * ((double)i / (double)frames)));
  388.             updateRequest.Add("position", position);
  389.             updateRequest.Add("rotation", sR + (rotation * ((double)i / (double)frames)));
  390.             CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  391.         }
  392.  
  393.         tweenCount--;
  394.     }
  395.  
  396.     public Task tween(double x, double y, int frames, String item, double rotation = 0, int id = -1)
  397.     {
  398.         tweenCount++;
  399.         Task t = new Task(() => TweenThread(x, y, frames, item, rotation, id));
  400.         t.Start();
  401.         return t;
  402.     }
  403.  
  404.     VideoInfo getVideoInfo()
  405.     {
  406.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  407.         String requestResponse = CPH.ObsSendRaw("GetVideoInfo", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  408.         VideoInfo responseJson = JsonConvert.DeserializeObject<VideoInfo>(requestResponse);
  409.         return responseJson;
  410.     }
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement