This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from muesli/obs-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sceneitems.go
135 lines (118 loc) · 2.88 KB
/
sceneitems.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"errors"
"fmt"
obsws "github.com/muesli/go-obs-websocket"
"github.com/spf13/cobra"
)
var (
listSceneItemsCmd = &cobra.Command{
Use: "list-sceneitems",
Short: "Lists all scene-items of a source",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("list-sceneitems requires a scene")
}
return listSceneItems(args[0])
},
}
toggleSceneItemCmd = &cobra.Command{
Use: "toggle-sceneitem",
Short: "Toggles visibility of a scene-item",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("toggle-sceneitem requires a scene and scene-item")
}
return toggleSceneItem(args[0], args[1:]...)
},
}
showSceneItemCmd = &cobra.Command{
Use: "show-sceneitem",
Short: "Makes a scene-item visible",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("show-sceneitem requires a scene and scene-item(s)")
}
return setSceneItemVisible(true, args[0], args[1:]...)
},
}
hideSceneItemCmd = &cobra.Command{
Use: "hide-sceneitem",
Short: "Hides a scene-item",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return errors.New("hide-sceneitem requires a scene and scene-item(s)")
}
return setSceneItemVisible(false, args[0], args[1:]...)
},
}
)
func listSceneItems(scene string) error {
req := obsws.NewGetSceneListRequest()
resp, err := req.SendReceive(*client)
if err != nil {
return err
}
for _, v := range resp.Scenes {
if v["name"] != scene {
continue
}
for _, s := range v["sources"].([]interface{}) {
src := s.(map[string]interface{})
fmt.Println(src["name"])
}
}
return nil
}
func setSceneItemVisible(visible bool, scene string, items ...string) error {
for _, item := range items {
req := obsws.NewGetSceneItemPropertiesRequest(scene, item)
resp, err := req.SendReceive(*client)
if err != nil {
return err
}
chreq := obsws.NewSetSceneItemPropertiesRequest(
scene,
item,
resp.PositionX,
resp.PositionY,
resp.PositionAlignment,
resp.Rotation,
resp.ScaleX,
resp.ScaleY,
resp.CropTop,
resp.CropBottom,
resp.CropLeft,
resp.CropRight,
visible,
resp.BoundsType,
resp.BoundsAlignment,
resp.BoundsX,
resp.BoundsY)
err = chreq.Send(*client)
if err != nil {
return err
}
}
return nil
}
func toggleSceneItem(scene string, items ...string) error {
for _, item := range items {
req := obsws.NewGetSceneItemPropertiesRequest(scene, item)
resp, err := req.SendReceive(*client)
if err != nil {
return err
}
err = setSceneItemVisible(!resp.Visible, scene, item)
if err != nil {
return err
}
}
return nil
}
func init() {
rootCmd.AddCommand(listSceneItemsCmd)
rootCmd.AddCommand(toggleSceneItemCmd)
rootCmd.AddCommand(showSceneItemCmd)
rootCmd.AddCommand(hideSceneItemCmd)
}