Skip to content

Commit 45e0602

Browse files
committed
归档
1 parent 1927d07 commit 45e0602

File tree

6 files changed

+398
-12
lines changed

6 files changed

+398
-12
lines changed

src/zh-hant/hello_world.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@
88

99
# 在指令稿記錄視窗顯示 Python 模組搜尋路徑
1010
import sys
11-
obs.script_log(obs.LOG_INFO, str(sys.path))
12-
13-
def script_description():
14-
# 函式 script_description 用於提供腳本說明
15-
return ['12323sdfsdf','12323']
11+
obs.script_log(obs.LOG_INFO, str(sys.path))

src/zh-hant/scripts/exports.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def script_load(settings):
2424
if closed_time:
2525
obs.script_log(obs.LOG_INFO, f'上次腳本停止的時間為 {closed_time}')
2626

27+
obs.script_log(obs.LOG_INFO, script_path())
28+
2729
# def script_unload():
2830
# # 將當前時間寫入腳本設定項 closed_time,作為腳本的停止時間
2931
# from datetime import datetime
@@ -44,4 +46,14 @@ def script_defaults(settings):
4446
obs.obs_data_set_default_int(settings, 'hours', 3)
4547

4648
# def script_tick(seconds):
47-
# obs.script_log(obs.LOG_INFO, f'{seconds} OBS 就要無法回應了!!!')
49+
# obs.script_log(obs.LOG_INFO, f'{seconds} OBS 就要無法回應了!!!')
50+
51+
# 腳本計時器的回呼函式 welcome
52+
def welcome():
53+
obs.script_log(obs.LOG_INFO, f'{type(obs.timer_add)}')
54+
obs.script_log(obs.LOG_INFO, '這是只被呼叫一次的回呼函式')
55+
# 移除 welcome 對應的腳本計時器
56+
obs.remove_current_callback()
57+
58+
# 加入腳本計時器,觸發時間間隔為 3 秒
59+
obs.timer_add(welcome, 3000)

src/zh-hant/scripts/properties.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# 匯入模組 obspython
2+
import obspython as obs
3+
4+
5+
# 函式 load_message 將讀取文字檔案中的內容,並將其寫入文字方塊
6+
def load_message(props, prop, settings):
7+
# 取得檔案對話方塊中的檔案路徑
8+
path = obs.obs_data_get_string(settings, 'more')
9+
10+
if path:
11+
# 從檔案讀取內容
12+
file = open(path, 'r', encoding='utf-8')
13+
content = file.read()
14+
file.close()
15+
16+
# 將內容設定到文字方塊中,如果內容與文字方塊不一致
17+
if content != obs.obs_data_get_string(settings, 'message'):
18+
obs.obs_data_set_string(settings, 'message', content)
19+
# 立即將腳本設定更新至控製項,這將導致 load_message 再次被呼叫
20+
obs.obs_properties_apply_settings(props, settings)
21+
# 這裏需要傳回特定值 True
22+
return True
23+
24+
# 函式 set_source_color 將色彩對話方塊指定的色彩,設定到文字來源
25+
def set_source_color(settings):
26+
# 取得色彩對話方塊所確定的色彩
27+
color = obs.obs_data_get_int(settings, 'color')
28+
29+
# 為場景中的文字來源 Welcome 設定色彩
30+
source = obs.obs_get_source_by_name('Welcome')
31+
data = obs.obs_data_create()
32+
obs.obs_data_set_int(data, 'color', color)
33+
obs.obs_source_update(source, data)
34+
obs.obs_data_release(data)
35+
obs.obs_source_release(source)
36+
37+
# 函式 set_source_font 將字型對話方塊指定的字型,設定到文字來源
38+
def set_source_font(settings):
39+
# 取得字型對話方塊所確定的字型
40+
font = obs.obs_data_get_obj(settings, 'font')
41+
# obs.script_log(obs.LOG_INFO, f"色彩:{font}")
42+
43+
# 為場景中的文字來源 Welcome 設定字型
44+
source = obs.obs_get_source_by_name('Welcome')
45+
data = obs.obs_data_create()
46+
obs.obs_data_set_obj(data, 'font', font)
47+
obs.obs_source_update(source, data)
48+
obs.obs_data_release(font)
49+
obs.obs_data_release(data)
50+
obs.obs_source_release(source)
51+
52+
# 函式 set_source_text 將隨機選擇清單方塊中的項,然後將其設定為文字來源的文字
53+
def set_source_text(settings):
54+
# 取得清單方塊中的所有項
55+
my_texts = obs.obs_data_get_array(settings, 'my_texts')
56+
57+
# 隨機選擇一個項,並將其內容設定為文字來源的文字
58+
count = obs.obs_data_array_count(my_texts)
59+
if count > 0:
60+
import random
61+
index = random.randint(0, count - 1)
62+
item = obs.obs_data_array_item(my_texts, index)
63+
64+
# 設定場景中的文字來源 Welcome 的文字
65+
source = obs.obs_get_source_by_name('Welcome')
66+
data = obs.obs_data_create()
67+
obs.obs_data_set_string(data, 'text', obs.obs_data_get_string(item, 'value'))
68+
obs.obs_source_update(source, data)
69+
obs.obs_data_release(data)
70+
obs.obs_source_release(source)
71+
72+
# 用於登入的函式 login
73+
def login(props, prop):
74+
# 點選登入按鈕後,將隱藏群組方塊
75+
group_login = obs.obs_properties_get(props, 'group_login')
76+
obs.obs_property_set_visible(group_login, False)
77+
# 這裏需要傳回特定值 True,否則無法看到隱藏效果
78+
return True
79+
80+
# 讓訊息文字方塊顯示隨機的文字
81+
def set_random_text(props, prop):
82+
# 隨機的取得一個文字
83+
import random
84+
messages = ('天氣不錯!', '吃了嗎?', '下雨啦!')
85+
text = messages[random.randint(0, 2)]
86+
87+
# 將隨機文字寫入腳本設定項 message
88+
obs.obs_data_set_string(current_settings, 'message', text)
89+
# 將腳本設定應用到屬性集物件
90+
obs.obs_properties_apply_settings(props, current_settings)
91+
# 這裏需要傳回特定值 True
92+
return True
93+
94+
def script_properties():
95+
# 建立一個屬性集物件
96+
props = obs.obs_properties_create()
97+
98+
# 新增一個核取方塊,用於控製是否在腳本載入後執行任務
99+
obs.obs_properties_add_bool(props, 'auto', '在啟動時執行任務?')
100+
# 新增一個小數滑桿,用於表示延遲執行任務的時間
101+
obs.obs_properties_add_float_slider(props, 'seconds', '延遲秒數', 1, 10, 0.5)
102+
# 新增一個文字方塊,用於表示需要傳送的訊息
103+
obs.obs_properties_add_text(props, 'message', '訊息:', obs.OBS_TEXT_MULTILINE)
104+
# 新增一個按鈕,用於再次執行 task 函式
105+
obs.obs_properties_add_button(props, 'again', '再次執行', lambda ps, p: task())
106+
107+
# 新增一個下拉式方塊,用於選擇串流平臺
108+
platform = obs.obs_properties_add_list(props, 'platform', '串流平臺:', obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
109+
# 為下拉式方塊新增項,需要給出項的名稱和值
110+
obs.obs_property_list_add_string(platform, 'YouTube', 'yt')
111+
obs.obs_property_list_add_string(platform, 'Twitch', 't')
112+
# 在下拉式方塊的第二個位置插入項,並使其不可用
113+
obs.obs_property_list_insert_string(platform, 1, 'Unkown', 'u')
114+
obs.obs_property_list_item_disable(platform, 1, True)
115+
116+
# 新增一個檔案對話方塊,用於載入更多的訊息
117+
more = obs.obs_properties_add_path(props, 'more', '更多訊息:', obs.OBS_PATH_FILE, '文字檔案(*.txt)', None)
118+
# 當使用者選擇了一個檔案後,函式 load_message 將被呼叫
119+
obs.obs_property_set_modified_callback(more, load_message)
120+
121+
# 新增一個色彩對話方塊,用於設定文字來源的色彩
122+
obs.obs_properties_add_color(props, 'color', '色彩:')
123+
# 新增一個字型對話方塊,用於設定文字來源的字型
124+
obs.obs_properties_add_font(props, 'font', '字型:')
125+
126+
# 新增一個可編輯清單方塊,用於設定文字來源的文字
127+
obs.obs_properties_add_editable_list(props, 'my_texts', '文字來源文字:', obs.OBS_EDITABLE_LIST_TYPE_STRINGS, None, None)
128+
129+
# 建立群組方塊對應的屬性集
130+
login_props = obs.obs_properties_create()
131+
# 新增一個群組方塊,他包含了用於登入的子控製項
132+
obs.obs_properties_add_group(props, 'group_login', '使用者登入', obs.OBS_GROUP_NORMAL, login_props)
133+
obs.obs_properties_add_text(login_props, 'username', '使用者名稱:', obs.OBS_TEXT_DEFAULT)
134+
obs.obs_properties_add_text(login_props, 'password', '密碼:', obs.OBS_TEXT_PASSWORD)
135+
# 新增一個登入按鈕
136+
obs.obs_properties_add_button(login_props, 'login', '登入', login)
137+
138+
# 新增一個按鈕,用於為訊息文字方塊選擇隨機文字
139+
obs.obs_properties_add_button(props, 'random_text', '隨機訊息', set_random_text)
140+
return props
141+
142+
143+
def script_update(settings):
144+
# 使用者編輯控製項後,將最新的腳本設定應用至場景
145+
set_source_color(settings)
146+
set_source_font(settings)
147+
148+
149+
def script_defaults(settings):
150+
# 設定控製項的預設值
151+
obs.obs_data_set_default_bool(settings, 'auto', True)
152+
obs.obs_data_set_default_double(settings, 'seconds', 1.5)
153+
154+
155+
# 變數 current_settings 用於表示腳本設定
156+
current_settings = None
157+
158+
# 執行任務的函式 task
159+
def task():
160+
obs.script_log(obs.LOG_INFO, '任務開始了')
161+
obs.remove_current_callback()
162+
163+
# 顯示文字方塊中的訊息
164+
message = obs.obs_data_get_string(current_settings, 'message')
165+
obs.script_log(obs.LOG_INFO, f'訊息為 {message if message else "[空]"}')
166+
# 顯示選擇的串流平臺
167+
platform = obs.obs_data_get_string(current_settings, 'platform')
168+
obs.script_log(obs.LOG_INFO, f'串流平臺為 {platform}')
169+
170+
# 隨機選擇項作為文字來源的文字
171+
set_source_text(current_settings)
172+
173+
174+
def script_load(settings):
175+
global current_settings
176+
current_settings = settings
177+
178+
# 是否在腳本載入後執行任務?
179+
if obs.obs_data_get_bool(settings, 'auto'):
180+
obs.script_log(obs.LOG_INFO, f'在啟動時執行任務(是的)')
181+
182+
# 新增執行任務的計時器,時間間隔為滑桿表示的秒數
183+
seconds = obs.obs_data_get_double(settings, 'seconds')
184+
obs.script_log(obs.LOG_INFO, f'請等待大約 {seconds} 秒')
185+
obs.timer_add(task, int(seconds * 1000))

src/zh/hello_world.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@
88

99
# 在脚本日志窗口显示 Python 模块搜索路径
1010
import sys
11-
obs.script_log(obs.LOG_INFO, str(sys.path))
12-
13-
def script_description():
14-
# 函数 script_description 用于提供脚本说明
15-
return ['12323sdfsdf','12323']
11+
obs.script_log(obs.LOG_INFO, str(sys.path))

src/zh/scripts/exports.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def script_load(settings):
2424
if closed_time:
2525
obs.script_log(obs.LOG_INFO, f'上次脚本停止的时间为 {closed_time}')
2626

27+
obs.script_log(obs.LOG_INFO, script_path())
28+
2729
# def script_unload():
2830
# # 将当前时间写入脚本设置项 closed_time,作为脚本的停止时间
2931
# from datetime import datetime
@@ -44,4 +46,14 @@ def script_defaults(settings):
4446
obs.obs_data_set_default_int(settings, 'hours', 3)
4547

4648
# def script_tick(seconds):
47-
# obs.script_log(obs.LOG_INFO, f'{seconds} OBS 就要无法响应了!!!')
49+
# obs.script_log(obs.LOG_INFO, f'{seconds} OBS 就要无法响应了!!!')
50+
51+
# 脚本计时器的回调函数 welcome
52+
def welcome():
53+
obs.script_log(obs.LOG_INFO, f'{type(obs.timer_add)}')
54+
obs.script_log(obs.LOG_INFO, '这是只被调用一次的回调函数')
55+
# 移除 welcome 对应的脚本计时器
56+
obs.remove_current_callback()
57+
58+
# 添加脚本计时器,触发时间间隔为 3 秒
59+
obs.timer_add(welcome, 3000)

0 commit comments

Comments
 (0)