Skip to content

Commit 0ace9e2

Browse files
committed
归档
0 parents  commit 0ace9e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2256
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__/
2+
.vscode/settings.json
3+
读我.txt
4+
temp.py

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## 說明
2+
3+
這裏的程式碼被用於一個 `OBS Python 腳本` 的教程,你可以訪問 [OBS PYTHON 腳本教程說明](https://learnscript.net/zh-hant/obs-python-scripting/),檢視 `OBS Python 腳本` 相關的文章。
4+
5+
每個 `.py` 程式碼檔案都含有其對應文章的資訊。
6+
7+
## 關於
8+
9+
這些 `OBS Python 腳本` 的文章可能還會不斷補充,您可以訂閱 [學腳本](https://learnscript.net/zh-hant/)
10+
11+
12+
## 说明
13+
14+
这里的代码被用于一个 `OBS Python 脚本` 的教程,你可以访问 [OBS PYTHON 脚本教程说明](https://learnscript.net/zh/obs-python-scripting/),查看 `OBS Python 脚本` 相关的文章。
15+
16+
每个 `.py` 代码文件都含有其对应文章的信息。
17+
18+
## 关于
19+
20+
这些 `OBS Python 脚本` 的文章可能还会不断补充,您可以关注 [学脚本](https://learnscript.net/zh/)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/apply-settings-to-property-collection/ 如何套用設定至屬性集
4+
'''
5+
6+
# 匯入模組 obspython,random
7+
import obspython as obs
8+
import random
9+
10+
# 用於儲存腳本設定
11+
data = None
12+
# 一組可供選擇的訊息
13+
messages = ('天氣不錯!', '吃了嗎?', '下雨啦!')
14+
15+
16+
def script_load(settings):
17+
# 在腳本載入時,將腳本設定儲存在模組變數 data 中
18+
global data
19+
data = settings
20+
21+
22+
def script_properties():
23+
props = obs.obs_properties_create()
24+
25+
# 添加一個文字方塊和按鈕
26+
obs.obs_properties_add_text(props, 'message', '訊息:', obs.OBS_TEXT_DEFAULT)
27+
obs.obs_properties_add_button(props, 'random_message', '隨機訊息', random_message_clicked)
28+
29+
return props
30+
31+
32+
def random_message_clicked(props, prop):
33+
# 隨機的取得一個訊息
34+
message = messages[random.randint(0, 2)]
35+
36+
# 將訊息寫入腳本設定項 message
37+
obs.obs_data_set_string(data, 'message', message)
38+
# 將腳本設定套用到腳本屬性集
39+
obs.obs_properties_apply_settings(props, data)
40+
return True

src/zh-hant/junior/button.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/button/ 如何使用按鈕
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
def script_properties():
10+
props = obs.obs_properties_create()
11+
12+
# 添加一個按鈕
13+
prop = obs.obs_properties_add_button(props, 'output', '輸出', output_clicked)
14+
# 設定按鈕的類型和 URL,回呼函式 output_clicked 將失效
15+
obs.obs_property_button_set_type(prop, obs.OBS_BUTTON_URL)
16+
obs.obs_property_button_set_url(prop, 'http://www.bing.com/')
17+
18+
return props
19+
20+
def output_clicked(props, prop):
21+
# 取得按鈕的 URL,並輸出到指令稿記錄視窗中,但這不會發生
22+
url = obs.obs_property_button_url(prop)
23+
obs.script_log(obs.LOG_INFO, f'按鈕 URL:{url}')

src/zh-hant/junior/check_box.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/check-box/ 如何使用核取方塊
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
import datetime
9+
10+
11+
def script_properties():
12+
props = obs.obs_properties_create()
13+
14+
# 添加一個核取方塊,用於確定是否啟用計時器
15+
obs.obs_properties_add_bool(props, 'enable_timer', '啟用計時器?')
16+
17+
return props
18+
19+
20+
def script_update(settings):
21+
# 當腳本更新時,取得核取方塊的選取狀態
22+
display = obs.obs_data_get_bool(settings, 'enable_timer')
23+
24+
# 根據核取方塊的選取狀態,添加或移除計時器
25+
if display:
26+
# 添加顯示時間的計時器
27+
obs.timer_add(show_time, 1000)
28+
else:
29+
# 移除顯示時間的計時器
30+
obs.timer_remove(show_time)
31+
32+
33+
def show_time():
34+
# 回呼函式 show_time,用於在指令稿記錄中顯示時間
35+
time = datetime.datetime.now().ctime()
36+
obs.script_log(obs.LOG_INFO, f'當前時間:{time}')
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/data/clear-data-items-and-erase-data-item/ 如何清除所有資料項,抹除資料項
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
# 用於儲存腳本設定
10+
data = None
11+
12+
def script_load(settings):
13+
# 在腳本載入時,將腳本設定儲存在模組變數 data 中
14+
global data
15+
data = settings
16+
17+
def script_properties():
18+
props = obs.obs_properties_create()
19+
20+
# 添加用於填寫玩家和遊戲資訊的文字方塊
21+
obs.obs_properties_add_text(props, 'player', '玩家:', obs.OBS_TEXT_DEFAULT)
22+
obs.obs_properties_add_text(props, 'game', '遊戲:', obs.OBS_TEXT_DEFAULT)
23+
# 添加顯示祝賀資訊的文字方塊
24+
obs.obs_properties_add_text(props, 'done', '祝賀,你已經完成所有步驟!', obs.OBS_TEXT_INFO)
25+
# 添加下一步和重置按鈕
26+
obs.obs_properties_add_button(props, 'next', '下一步', next_clicked)
27+
obs.obs_properties_add_button(props, 'reset', '重置', reset_clicked)
28+
29+
# 刷新介面,用於顯示或隱藏文字方塊和按鈕
30+
refresh_ui(props)
31+
32+
return props
33+
34+
def next_clicked(props, prop):
35+
# 當下一步按鈕被點選時,將腳本設定項 step 加 1,表示進入下一步
36+
step = obs.obs_data_get_int(data, 'step')
37+
obs.obs_data_set_int(data, 'step', step + 1)
38+
39+
# 刷新介面,切換顯示下一步所用的控製項
40+
refresh_ui(props)
41+
return True
42+
43+
def reset_clicked(props, prop):
44+
# 清空所有的腳本設定項
45+
obs.obs_data_clear(data)
46+
# 需要將腳本設定應用到屬性,否則文字方塊中的已有內容不會被清空
47+
obs.obs_properties_apply_settings(props, data)
48+
49+
# 刷新介面,恢復到第一步
50+
refresh_ui(props)
51+
return True
52+
53+
def refresh_ui(props):
54+
# 腳本設定項 step 表示當前是第幾步
55+
step = obs.obs_data_get_int(data, 'step')
56+
57+
# 根據 step 來決定哪些文字方塊和按鈕需要顯示
58+
prop = obs.obs_properties_get(props, 'player')
59+
obs.obs_property_set_visible(prop, step == 0)
60+
61+
prop = obs.obs_properties_get(props, 'game')
62+
obs.obs_property_set_visible(prop, step == 1)
63+
64+
prop = obs.obs_properties_get(props, 'done')
65+
obs.obs_property_set_visible(prop, step == 2)
66+
67+
prop = obs.obs_properties_get(props, 'next')
68+
obs.obs_property_set_visible(prop, step != 2)
69+
70+
prop = obs.obs_properties_get(props, 'reset')
71+
obs.obs_property_set_visible(prop, step == 2)

src/zh-hant/junior/color_dialog.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/color-dialog/ 如何使用色彩對話方塊
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
def script_properties():
10+
props = obs.obs_properties_create()
11+
12+
# 添加一個色彩對話方塊
13+
prop = obs.obs_properties_add_color(props, 'text_color', '色彩:')
14+
obs.obs_property_set_modified_callback(prop, color_modified)
15+
16+
return props
17+
18+
def color_modified(props, prop, settings):
19+
# 取得色彩對話方塊所確定的色彩
20+
text_color = obs.obs_data_get_int(settings, 'text_color')
21+
22+
# 取得場景中的文字來源 Welcome
23+
source = obs.obs_get_source_by_name('Welcome')
24+
25+
# 將色彩對話方塊的色彩應用到文字來源
26+
data = obs.obs_data_create()
27+
obs.obs_data_set_int(data, 'color', text_color)
28+
obs.obs_source_update(source, data)
29+
30+
# 釋放資料物件和來源物件
31+
obs.obs_data_release(data)
32+
obs.obs_source_release(source)

src/zh-hant/junior/combo_box.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/combo-box/ 如何使用下拉式方塊
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
def script_properties():
10+
props = obs.obs_properties_create()
11+
12+
# 添加一個表示串流平臺的下拉式方塊
13+
prop = obs.obs_properties_add_list(props, 'platform', '串流平臺:', obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
14+
15+
# 為下拉式方塊添加項
16+
obs.obs_property_list_add_string(prop, 'YouTube', 'yt')
17+
obs.obs_property_list_add_string(prop, 'Twitch', 't')
18+
# 在下拉式方塊的第二個位置插入項,並使該項不可用
19+
obs.obs_property_list_insert_string(prop, 1, 'Unkown', 'u')
20+
obs.obs_property_list_item_disable(prop, 1, True)
21+
22+
# 增加一個開始串流的按鈕
23+
obs.obs_properties_add_button(props, 'start', '開始串流', start_clicked)
24+
25+
return props
26+
27+
# 用於儲存腳本設定
28+
data = None
29+
30+
def script_load(settings):
31+
# 在腳本載入時,將腳本設定儲存在模組變數 data 中
32+
global data
33+
data = settings
34+
35+
def start_clicked(props, prop):
36+
# 取得下拉式方塊中選取的項,並顯示在指令稿記錄視窗中
37+
platform = obs.obs_data_get_string(data, 'platform')
38+
39+
obs.script_log(obs.LOG_INFO, f'開始在 {platform} 串流')

src/zh-hant/junior/controls.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/general-control-functions/ 通用控製項函式有哪些
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
def script_properties():
10+
props = obs.obs_properties_create()
11+
12+
# 添加一個按鈕,用於確定一些串流的設定
13+
obs.obs_properties_add_button(props, 'ok', '確定', ok_clicked)
14+
15+
# 添加一個核取方塊,表示串流是否顯示歡迎訊息
16+
prop = obs.obs_properties_add_bool(props, 'display_welcome', '是否顯示歡迎訊息?')
17+
obs.obs_property_set_modified_callback(prop, display_welcome_modified)
18+
19+
# 添加一個文字方塊,表示歡迎訊息
20+
prop = obs.obs_properties_add_text(props, 'welcome', '歡迎訊息:', obs.OBS_TEXT_DEFAULT)
21+
# 設定文字方塊不顯示
22+
obs.obs_property_set_visible(prop, False)
23+
24+
return props
25+
26+
def ok_clicked(props, prop):
27+
# 確定按鈕不能被再一次的點選
28+
obs.obs_property_set_enabled(prop, False)
29+
30+
# 如果文字方塊可見,則認為顯示歡迎訊息
31+
prop_welcome = obs.obs_properties_get(props, 'welcome')
32+
obs.script_log(obs.LOG_INFO, f'顯示歡迎訊息?{obs.obs_property_visible(prop_welcome)}')
33+
return True
34+
35+
def display_welcome_modified(props, prop, settings):
36+
# 點選核取方塊時,切換文字方塊的可見狀態
37+
# 取得文字方塊
38+
prop_welcome = obs.obs_properties_get(props, 'welcome')
39+
40+
# 取得並反轉文字方塊的可見狀態
41+
visible = not obs.obs_property_visible(prop_welcome)
42+
obs.obs_property_set_visible(prop_welcome, visible)
43+
return True
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/script-properties/create-and-destroy-property-collections/ 如何建立和終結屬性集
4+
'''
5+
6+
# 匯入模組 obspython,datetime
7+
import obspython as obs
8+
import datetime
9+
10+
# 開始時間
11+
start = None
12+
13+
def script_properties():
14+
# script_properties 傳回一個屬性集物件
15+
# 呼叫 obs_properties_create 函式建立屬性集
16+
props = obs.obs_properties_create()
17+
18+
# 添加一個判斷是否可以結束串流的按鈕
19+
obs.obs_properties_add_button(props, 'over', '結束了?', over_clicked)
20+
21+
# 將當前時間記錄為開始時間
22+
global start
23+
start = datetime.datetime.now()
24+
25+
return props
26+
27+
def over_clicked(props, prop):
28+
# 當按鈕被點選時,判斷是否可以結束串流
29+
30+
# 計算從開始時間到現在經歷的秒數
31+
end = datetime.datetime.now()
32+
seconds = (end - start).total_seconds()
33+
34+
# 已經過了 1 分鐘,則顯示可以結束
35+
if seconds > 60:
36+
obs.script_log(obs.LOG_INFO, '可以結束了')
37+
else:
38+
obs.script_log(obs.LOG_INFO, '串流還不到 1 分鐘')

0 commit comments

Comments
 (0)