Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #Default JSON variables
  5. server_ip = "localhost"
  6. server_port = 4444
  7. server_password = "secret"
  8. ConfigName = "SOS"
  9. StudioMode = False
  10. DefaultTransition = "Cut"
  11. DefaultTransitionDuration = 300
  12.  
  13. from obswebsocket import obsws, requests
  14. import pygame.midi as midi
  15. import time
  16. import json
  17.  
  18.  
  19.  
  20. def Setup():
  21.     global setup_json
  22.     try:
  23.         setup_json = json.loads(''.join(open("setup.json", "r")))
  24.         if "config_general" in setup_json:
  25.             config_general = setup_json["config_general"]
  26.             if "ConfigName" in config_general:
  27.                 global ConfigName
  28.                 ConfigName = config_general["ConfigName"]
  29.             else:
  30.                 print("ERROR 404:  ConfigName argument was not found")
  31.             if "StudioMode" in config_general:
  32.                 global StudioMode
  33.                 StudioMode = config_general["StudioMode"]
  34.             else:
  35.                 print("ERROR 404:  StudioMode argument was not found")
  36.             if "DefaultTransition" in config_general:
  37.                 global DefaultTransition
  38.                 DefaultTransition = config_general["DefaultTransition"]
  39.             else:
  40.                 print("ERROR 404:  DefaultTransition argument was not found")
  41.             if "DefaultTransitionDuration" in config_general:
  42.                 global DefaultTransitionDuration
  43.                 DefaultTransitionDuration = config_general["DefaultTransitionDuration"]
  44.             else:
  45.                 print("ERROR 404:  DefaultTransitionDuration argument was not found")
  46.             if "server_ip" in config_general:
  47.                 global server_ip
  48.                 server_ip = config_general["server_ip"]
  49.             else:
  50.                 print("ERROR 404:  server_ip argument was not found")
  51.             if "server_port" in config_general:
  52.                 global server_port
  53.                 server_port = config_general["server_port"]
  54.             else:
  55.                 print("ERROR 404:  server_port argument as not found")
  56.             if "server_password" in config_general:
  57.                 global server_password
  58.                 server_password = config_general["server_password"]
  59.             else:
  60.                 print("ERROR 404  server_password argument was not found")
  61.         else:
  62.             print("No general config in config file")
  63.  
  64.     except FileNotFoundError:
  65.         print("Config file not found")
  66.  
  67.  
  68.     global ws
  69.     try:
  70.         ws = obsws(server_ip, server_port, server_password)
  71.         ws.connect()
  72.     except:
  73.         print("Connection to OBS Websocket is impossible")
  74.         print("Exiting program")
  75.         exit()
  76.  
  77.  
  78.     global midi_in
  79.     try:
  80.         midi.init()
  81.         midi_in = midi.Input(1, 1024)
  82.     except:
  83.         print("Chaque année, des millions de programmeurs disent qu'ils vont régler ce problème, et ils ne le font jamais. Si vous voyez ce message, c'et que nous faisons parti de ces programmeurs.")
  84.         print("Exiting program")
  85.         exit()
  86.  
  87.  
  88.  
  89. def CheckStudioState():
  90.     StudioState = False
  91.     try:
  92.         StudioState = ws.call(requests.GetStudioModeStatus())
  93.         StudioState = StudioState.getStudioMode()
  94.         print("Studiostate:" + str(StudioState))
  95.     except:
  96.         print("Couldn't get StudioModeStatus")
  97.  
  98.  
  99.  
  100.  
  101. def press_button(Button, Event):
  102.     Action = None
  103.     if "config_pad" in setup_json:
  104.         if "Buttons" in setup_json["config_pad"]:
  105.             if Button in setup_json["config_pad"]["Buttons"]:
  106.                 BUTTONCONF = setup_json["config_pad"]["Buttons"][Button]
  107.                 if "Event" in BUTTONCONF:
  108.                     if (BUTTONCONF["Event"] == "OnRelease" and Event == 138) or (BUTTONCONF["Event"] == "OnPress" and Event == 154):
  109.                         if "Action" in BUTTONCONF:
  110.                             Action = BUTTONCONF["Action"]
  111.                         else:
  112.                             print("No Action argument in Button config")
  113.                 else:
  114.                     print("No Event argument in Button config")
  115.             else:
  116.                 print("Button not found")
  117.         else:
  118.             print("No Button argument in config_pad argument")
  119.     else:
  120.         print("No config_pad argument in config")
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.     if Action == "SwitchScene":
  129.         StudioState = False
  130.         try:
  131.             StudioState = ws.call(requests.GetStudioModeStatus())
  132.             StudioState = StudioState.getStudioMode()
  133.             print("Studiostate:" + str(StudioState))
  134.         except:
  135.             print("Couldn't get StudioModeStatus")
  136.         if StudioState == True:
  137.             try:
  138.                 ws.call(requests.SetPreviewScene(BUTTONCONF["DestinationScene"]))
  139.                 print("Set Target to preview")
  140.             except:
  141.                 print(f"Couldn't set Target to preview")
  142.         else:
  143.             try:
  144.                 ws.call(requests.SetCurrentTransition(BUTTONCONF["Transition"]))
  145.                 ws.call(requests.SetCurrentScene(BUTTONCONF["DestinationScene"]))
  146.                 print("Set Target to program")
  147.             except:
  148.                 print(f"Couldn't set Target to program")
  149.     if Action == "TransitionToProgram":
  150.         StudioState = False
  151.         try:
  152.             StudioState = ws.call(requests.GetStudioModeStatus())
  153.             StudioState = StudioState.getStudioMode()
  154.             print("Studiostate:" + str(StudioState))
  155.         except:
  156.             print("Couldn't get StudioModeStatus")
  157.         if StudioState == True:
  158.             try:
  159.                 CurrentsceneObject = ws.call(requests.GetPreviewScene())
  160.                 Currentscene = CurrentsceneObject.getName()
  161. #               ws.call(requests.SetCurrentTransition("Fade"))
  162.                 ws.call(requests.SetCurrentScene(Currentscene))
  163.                 print("Sent preview to program")
  164.             except:
  165.                 print("Couldn't send preview to program")
  166.         else:
  167.             print("StudioMode not enabled")
  168.  
  169.  
  170.  
  171. Setup()
  172. while True:
  173.     while midi_in.poll():
  174.         midi_read = midi_in.read(1)
  175. #       print(midi_read)
  176.         if midi_read[0][0][0] == 154:
  177.             press_button(str(midi_read[0][0][1]), 154)
  178.     time.sleep(0.01)
  179.  
  180.  
  181. -----------------------------------------------------------------------------------------------------------------------------------
  182.  
  183.  
  184. {
  185.     "config_general" : {
  186.         "ConfigName": "Cafe Kawa",
  187.         "StudioMode": "True",
  188.         "DefaultTransition" : "Cut",
  189.         "DefaultTransitionDuration" : 300,
  190.         "server_ip" : "localhost",
  191.         "server_port" : 4444,
  192.         "server_password" : "banana"
  193.  
  194.  
  195.     },
  196.     "config_pad" : {
  197.         "Buttons" : {
  198.             "8":{
  199.                 "Event" : "OnPress",
  200.                 "Action" : "SwitchScene",
  201.                 "DestinationScene" : "Scene 1",
  202.                 "Transition" : "Fade",
  203.                 "TransitionDuration" : 300
  204.             },
  205.             "16":{
  206.                 "Action" : "SetTransition",
  207.                 "Transition" : "Fade",
  208.                 "TransitionDuration": 300
  209.             },
  210.             "15":{
  211.                 "Action" : "TransitionToProgram",
  212.                 "Transition" : "Cut"
  213.             }
  214.         },
  215.         "Fader" : {
  216.             "1":{
  217.                 "Action" : "SetTransitionDuration",
  218.                 "MaxValue" : 500,
  219.                 "MinValue": 300
  220.             }
  221.         }
  222.  
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement