Artelis

REEEE command

Feb 15th, 2022 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.06 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 shakeAmount = 200;
  99.         int shakeLength = 60;
  100.         int duplicateAmount = 4;
  101.         int spinTimes = 0;
  102.         int maxTweens = 80;
  103.         String sourceName = "Lumix Greenscreen";
  104.         String filterName = "RedColor";
  105.         List<int> newSources = new List<int>();
  106.         Transform originalTransform = getTransform(sourceName);
  107.         CPH.ObsShowFilter(CPH.ObsGetCurrentScene(), sourceName, filterName);
  108.         VideoInfo info = getVideoInfo();
  109.         setPosition(info.baseWidth / 2, info.baseHeight / 2, sourceName);
  110.         for (int i = 0; i < shakeLength; i++)
  111.         {
  112.             Task task = tween(CPH.NextDouble() * shakeAmount - shakeAmount / 2, CPH.NextDouble() * shakeAmount - shakeAmount / 2, 5, sourceName, 360 / shakeLength * spinTimes);
  113.             setPosition(info.baseWidth / 2, info.baseHeight / 2, sourceName);
  114.             foreach (int id in newSources)
  115.             {
  116.                 Transform t = getTransform("", id);
  117.                 task = tween(CPH.NextDouble() * shakeAmount - shakeAmount / 2, CPH.NextDouble() * shakeAmount - shakeAmount / 2, 5, "", 360 / shakeLength * spinTimes, id);
  118.                 setPosition((int)t.position.x, (int)t.position.y, "", id);
  119.             }
  120.  
  121.             if (i % duplicateAmount == 0 && newSources.Count < maxTweens)
  122.                 newSources.Add(copySource(sourceName, (int)(CPH.NextDouble() * info.baseWidth), (int)(CPH.NextDouble() * info.baseHeight), (int)(CPH.NextDouble() * 180)));
  123.             task.Wait();
  124.         }
  125.  
  126.         foreach (int id in newSources)
  127.         {
  128.             deleteSource("", id);
  129.         }
  130.  
  131.         setRotation((int)originalTransform.rotation, sourceName);
  132.         setPosition((int)originalTransform.position.x, (int)originalTransform.position.y, sourceName);
  133.         CPH.ObsHideFilter(CPH.ObsGetCurrentScene(), sourceName, filterName);
  134.         return true;
  135.     }
  136.  
  137.     SourceSettings getSourceSettings(String sourceName)
  138.     {
  139.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  140.         getRequest.Add("sourceName", sourceName);
  141.         String requestResponse = CPH.ObsSendRaw("GetSourceSettings", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  142.         SourceSettings responseJson = JsonConvert.DeserializeObject<SourceSettings>(requestResponse);
  143.         return responseJson;
  144.     }
  145.  
  146.     Scene getCurrentScene()
  147.     {
  148.         String requestResponse = CPH.ObsSendRaw("GetCurrentScene", "");
  149.         Scene sceneObj = JsonConvert.DeserializeObject<Scene>(requestResponse);
  150.         return sceneObj;
  151.     }
  152.  
  153.     SceneItemList getSceneItemList()
  154.     {
  155.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  156.         getRequest.Add("sourceName", "garbage");
  157.         String requestResponse = CPH.ObsSendRaw("GetSceneItemList", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  158.         SceneItemList responseJson = JsonConvert.DeserializeObject<SceneItemList>(requestResponse);
  159.         return responseJson;
  160.     }
  161.  
  162.     SceneItem getSceneItemNamed(String name)
  163.     {
  164.         SceneItemList list = getSceneItemList();
  165.         foreach (SceneItem i in list.sceneItems)
  166.         {
  167.             if (i.sourceName.Equals(name))
  168.             {
  169.                 return i;
  170.             }
  171.         }
  172.  
  173.         return null;
  174.     }
  175.  
  176.     SceneItem getSceneItemById(int id)
  177.     {
  178.         SceneItemList list = getSceneItemList();
  179.         foreach (SceneItem i in list.sceneItems)
  180.         {
  181.             if (i.itemId == id)
  182.             {
  183.                 return i;
  184.             }
  185.         }
  186.  
  187.         return null;
  188.     }
  189.  
  190.     void deleteSource(String name, int id = -1)
  191.     {
  192.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  193.         Dictionary<String, object> itemRequest = new Dictionary<String, object>();
  194.         if (!name.Equals(""))
  195.             itemRequest.Add("name", name);
  196.         if (id != -1)
  197.             itemRequest.Add("id", id);
  198.         updateRequest.Add("item", itemRequest);
  199.         CPH.ObsSendRaw("DeleteSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  200.     }
  201.  
  202.     int copySource(String name, int x = 0, int y = 0, int rotation = 0)
  203.     {
  204.         SceneItem item = getSceneItemNamed(name);
  205.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  206.         Dictionary<String, object> itemRequest = new Dictionary<String, object>();
  207.         itemRequest.Add("id", item.itemId);
  208.         updateRequest.Add("item", itemRequest);
  209.         String response = CPH.ObsSendRaw("DuplicateSceneItem", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  210.         DuplicatedSceneItemResponse newItemResponse = JsonConvert.DeserializeObject<DuplicatedSceneItemResponse>(response);
  211.         Transform t = getTransform("", item.itemId);
  212.         t.item = new Item();
  213.         t.item.id = newItemResponse.item.id;
  214.         t.item.name = newItemResponse.item.name;
  215.         setTransform(t, "", newItemResponse.item.id);
  216.         setPosition(x, y, "", newItemResponse.item.id);
  217.         setRotation(rotation, "", newItemResponse.item.id);
  218.         return newItemResponse.item.id;
  219.     }
  220.  
  221.     void setPosition(int x, int y, String item, int id = -1)
  222.     {
  223.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  224.         if (id == -1)
  225.             updateRequest.Add("item", item);
  226.         else
  227.         {
  228.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  229.             itemDict.Add("id", id);
  230.             updateRequest.Add("item", itemDict);
  231.         }
  232.  
  233.         Dictionary<String, object> position = new Dictionary<String, object>();
  234.         position.Add("x", x);
  235.         position.Add("y", y);
  236.         updateRequest.Add("position", position);
  237.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  238.     }
  239.  
  240.     void setRotation(int angle, String item, int id = -1)
  241.     {
  242.         Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  243.         if (id == -1)
  244.             updateRequest.Add("item", item);
  245.         else
  246.         {
  247.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  248.             itemDict.Add("id", id);
  249.             updateRequest.Add("item", itemDict);
  250.         }
  251.  
  252.         updateRequest.Add("rotation", angle);
  253.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  254.     }
  255.  
  256.     Transform getTransform(String item, int id = -1)
  257.     {
  258.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  259.         if (id == -1)
  260.             getRequest.Add("item", item);
  261.         else
  262.         {
  263.             Dictionary<String, object> itemDict = new Dictionary<String, object>();
  264.             itemDict.Add("id", id);
  265.             getRequest.Add("item", itemDict);
  266.         }
  267.  
  268.         String requestResponse = CPH.ObsSendRaw("GetSceneItemProperties", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  269.         Transform t = JsonConvert.DeserializeObject<Transform>(requestResponse);
  270.         return t;
  271.     }
  272.  
  273.     void setTransform(Transform t, String itemName, int id = -1)
  274.     {
  275.         SceneItem item;
  276.         if (id == -1)
  277.             item = getSceneItemNamed(itemName);
  278.         else
  279.             item = getSceneItemById(id);
  280.         if (item == null)
  281.             return;
  282.         CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(t, Formatting.Indented));
  283.     }
  284.  
  285.     void TweenThread(double x, double y, int frames, String item, double rotation = 0, int id = -1)
  286.     {
  287.         Transform t;
  288.         if (id == -1)
  289.             t = getTransform(item);
  290.         else
  291.             t = getTransform("", id);
  292.         double sX = t.position.x;
  293.         double sY = t.position.y;
  294.         double sR = t.rotation;
  295.         for (int i = 1; i - 1 < frames; i++)
  296.         {
  297.             Thread.Sleep((int)(1.0 / 60.0 * 1000.0));
  298.             Dictionary<String, object> updateRequest = new Dictionary<String, object>();
  299.             if (id == -1)
  300.                 updateRequest.Add("item", item);
  301.             else
  302.             {
  303.                 Dictionary<String, object> itemDict = new Dictionary<String, object>();
  304.                 itemDict.Add("id", id);
  305.                 updateRequest.Add("item", itemDict);
  306.             }
  307.  
  308.             Dictionary<String, object> position = new Dictionary<String, object>();
  309.             position.Add("x", sX + (x * ((double)i / (double)frames)));
  310.             position.Add("y", sY + (y * ((double)i / (double)frames)));
  311.             updateRequest.Add("position", position);
  312.             updateRequest.Add("rotation", sR + (rotation * ((double)i / (double)frames)));
  313.             CPH.ObsSendRaw("SetSceneItemProperties", JsonConvert.SerializeObject(updateRequest, Formatting.Indented));
  314.         }
  315.  
  316.         tweenCount--;
  317.     }
  318.  
  319.     public Task tween(double x, double y, int frames, String item, double rotation = 0, int id = -1)
  320.     {
  321.         tweenCount++;
  322.         Task t = new Task(() => TweenThread(x, y, frames, item, rotation, id));
  323.         t.Start();
  324.         return t;
  325.     }
  326.  
  327.     VideoInfo getVideoInfo()
  328.     {
  329.         Dictionary<String, object> getRequest = new Dictionary<String, object>();
  330.         String requestResponse = CPH.ObsSendRaw("GetVideoInfo", JsonConvert.SerializeObject(getRequest, Formatting.Indented));
  331.         VideoInfo responseJson = JsonConvert.DeserializeObject<VideoInfo>(requestResponse);
  332.         return responseJson;
  333.     }
  334. }
Add Comment
Please, Sign In to add comment