Skip to content

Commit 1f73903

Browse files
committed
Change library to pynput, refactor class
1 parent 73561e1 commit 1f73903

File tree

2 files changed

+24
-85
lines changed

2 files changed

+24
-85
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ Selected source will follow mouse pointer.
33
Using [`obs_sceneitem_set_pos`](https://obsproject.com/docs/reference-scenes.html#c.obs_sceneitem_set_pos)
44
# Installation
55
- Make sure your OBS Studio supports [scripting](https://obsproject.com/docs/scripting.html)
6-
- Download and extract source code from [here](https://github.com/upgradeQ/OBS-Studio-Cursor-skin/releases/tag/0.2.1)
7-
- You will need to install mouse package from [pypi](https://pypi.org/project/mouse/):
8-
`python -m pip install mouse`
6+
- You will need to install pynput package from [pypi](hhttps://pypi.org/project/pynput/):
7+
`python -m pip install pynput`
98
# Limitations
109
- Multilpe monitors setup will not work .
10+
- If used in fullscreen apps, offset will appear.
1111
# Usage
1212
- Create _source_ with desired cursor(e.g Image source or Media source).
1313
- In scripts select _that_ source name.
1414
- Test it: press Start, press Stop, tweak refresh rate.
1515
# Crop auto update
16-
For zoom or higlight.
16+
Zoom or higlight.
1717
- Create 2 display captures.
1818
- Create crop filter with this name: `cropXY`.
1919
- Check relative.

mouse_skin_obs.py

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import obspython as obs
2-
from mouse import get_position # python -m pip install mouse
2+
from pynput.mouse import Controller # python -m pip install pynput
33

4-
__version__ = "0.3.0-alpha"
5-
REFRESH_RATE = 15
6-
FLAG = True
4+
c = Controller()
5+
get_position = lambda: c.position
6+
7+
__version__ = "1.0.0"
78

89

910
def apply_scale(x, y, width, height):
@@ -13,16 +14,12 @@ def apply_scale(x, y, width, height):
1314

1415

1516
class CursorAsSource:
16-
def __init__(self, source_name=None):
17-
self.source_name = source_name
18-
self.lock = True
19-
self.update_xy = False
20-
self.update_gs = False # green screen
21-
self.update_cr = False
17+
source_name = None
18+
lock = True
19+
flag = True
20+
refresh_rate = 15
2221

2322
def update_cursor(self):
24-
""" in script settings check update cursor"""
25-
2623
source = obs.obs_get_source_by_name(self.source_name)
2724
settings = obs.obs_data_create()
2825
if source is not None:
@@ -48,58 +45,14 @@ def update_cursor(self):
4845
obs.obs_scene_release(scene)
4946
obs.obs_source_release(source)
5047

51-
def update_green(self):
52-
"""
53-
create new scene with name:__spotlight__
54-
add color source , add opacity(color correction) to that source
55-
add green circle
56-
add that scene as source to ur current scene
57-
apply chromakey filter to that source
58-
in script settings check update green
59-
"""
60-
source = obs.obs_get_source_by_name(self.source_name)
61-
settings = obs.obs_data_create()
62-
if source is not None:
63-
scene_width = obs.obs_source_get_width(source)
64-
scene_height = obs.obs_source_get_height(source)
65-
# get all scenes
66-
scenes = obs.obs_frontend_get_scenes()
67-
for sc in scenes:
68-
# select the one with chromakey cursor(green)
69-
name = obs.obs_source_get_name(sc)
70-
if name == "__spotlight__":
71-
# assign it
72-
_item = sc
73-
else:
74-
obs.obs_source_release(sc)
75-
76-
scene = obs.obs_scene_from_source(_item)
77-
scene_item = obs.obs_scene_find_source(scene, self.source_name)
78-
if scene_item:
79-
scale = obs.vec2()
80-
obs.obs_sceneitem_get_scale(scene_item, scale)
81-
scene_width, scene_height = apply_scale(
82-
scale.x, scale.y, scene_width, scene_height
83-
)
84-
next_pos = obs.vec2()
85-
next_pos.x, next_pos.y = get_position()
86-
next_pos.x -= scene_width / 2
87-
next_pos.y -= scene_height / 2
88-
# set position to center of source where cursor is
89-
obs.obs_sceneitem_set_pos(scene_item, next_pos)
90-
91-
obs.obs_data_release(settings)
92-
obs.obs_scene_release(scene)
93-
obs.obs_source_release(source)
94-
9548
def update_crop(self):
9649
"""
9750
Create 2 display captures.
9851
Create crop filter with this name: cropXY.
9952
Check relative.
10053
Set Width and Height to relatively small numbers e.g : 64x64 .
10154
Image mask blend + color correction might be an option too.
102-
Run script,select this source as cursor source , check Update crop and updated cursor, click start.
55+
Run script,select this source as cursor source , check Update crop, click start.
10356
"""
10457
source = obs.obs_get_source_by_name(self.source_name)
10558
crop = obs.obs_source_get_filter_by_name(source, "cropXY")
@@ -125,54 +78,42 @@ def ticker(self):
12578
if self.update_xy:
12679
self.update_crop()
12780
self.update_cursor()
128-
if self.update_gs:
129-
self.update_green()
130-
if self.update_cr:
81+
else:
13182
self.update_cursor()
13283

13384
if not self.lock:
13485
obs.remove_current_callback()
13586

13687

137-
py_cursor = CursorAsSource() # class created ,obs part starts
88+
py_cursor = CursorAsSource()
13889

13990

14091
def stop_pressed(props, prop):
141-
global FLAG
142-
FLAG = True
92+
py_cursor.flag = True
14393
py_cursor.lock = False
14494

14595

14696
def start_pressed(props, prop):
147-
global FLAG # to keep only one timer callback
148-
if py_cursor.source_name != "" and FLAG:
149-
obs.timer_add(py_cursor.ticker, REFRESH_RATE)
97+
if py_cursor.source_name != "" and py_cursor.flag:
98+
obs.timer_add(py_cursor.ticker, py_cursor.refresh_rate)
15099
py_cursor.lock = True
151-
FLAG = False
152-
153-
154-
def script_description():
155-
return "USE SOURCE AS CURSOR"
100+
py_cursor.flag = False # to keep only one timer callback
156101

157102

158103
def script_defaults(settings):
159-
global REFRESH_RATE
160-
obs.obs_data_set_default_int(settings, "refresh_rate", REFRESH_RATE)
104+
obs.obs_data_set_default_int(settings, "_refresh_rate", py_cursor.refresh_rate)
161105

162106

163107
def script_update(settings):
164-
global REFRESH_RATE
165108
py_cursor.update_xy = obs.obs_data_get_bool(settings, "bool_yn")
166-
py_cursor.update_xy = obs.obs_data_get_bool(settings, "bool_yn_cursor")
167-
py_cursor.update_gs = obs.obs_data_get_bool(settings, "bool_yn_green")
168109
py_cursor.source_name = obs.obs_data_get_string(settings, "source")
169-
REFRESH_RATE = obs.obs_data_get_int(settings, "refresh_rate")
110+
py_cursor.refresh_rate = obs.obs_data_get_int(settings, "_refresh_rate")
170111

171112

172-
def script_properties(): # ui
113+
def script_properties():
173114
props = obs.obs_properties_create()
174115
number = obs.obs_properties_add_int(
175-
props, "refresh_rate", "Refresh rate (ms)", 15, 300, 5
116+
props, "_refresh_rate", "Refresh rate (ms)", 15, 300, 5
176117
)
177118
p = obs.obs_properties_add_list(
178119
props,
@@ -191,6 +132,4 @@ def script_properties(): # ui
191132
obs.obs_properties_add_button(props, "button", "Stop", stop_pressed)
192133
obs.obs_properties_add_button(props, "button2", "Start", start_pressed)
193134
obs.obs_properties_add_bool(props, "bool_yn", "Update crop")
194-
obs.obs_properties_add_bool(props, "bool_yn_green", "Update green circle")
195-
obs.obs_properties_add_bool(props, "bool_yn_cursor", "Update cursro ")
196135
return props

0 commit comments

Comments
 (0)