Skip to content

Commit 5c2bf32

Browse files
committed
应用数据对象
1 parent 810a2f4 commit 5c2bf32

File tree

12 files changed

+230
-10
lines changed

12 files changed

+230
-10
lines changed

src/zh-hant/junior/apply_data.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/data/apply-data-to-target/ 如何應用資料至目標
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, 'style', '合併樣式', style_clicked)
14+
15+
return props
16+
17+
def style_clicked(props, prop):
18+
data = obs.obs_data_create()
19+
# 從檔案 white.json 載入白色樣式
20+
data_white = obs.obs_data_create_from_json_file('white.json')
21+
22+
# 將包含白色樣式的資料物件合併至 data
23+
obs.obs_data_apply(data, data_white)
24+
# 輸出 data 對應的 JSON 字串
25+
json_string = obs.obs_data_get_json(data)
26+
obs.script_log(obs.LOG_INFO, json_string)
27+
28+
# 從檔案 black.json 載入黑色樣式
29+
data_black = obs.obs_data_create_from_json_file('black.json')
30+
31+
# 將包含黑色樣式的資料物件合併至 data
32+
obs.obs_data_apply(data, data_black)
33+
# 輸出 data 對應的 JSON 字串
34+
json_string = obs.obs_data_get_json(data)
35+
obs.script_log(obs.LOG_INFO, json_string)
36+
37+
# 取得場景中的文字來源 Welcome
38+
source = obs.obs_get_source_by_name('Welcome')
39+
# 將樣式應用到文字來源
40+
obs.obs_source_update(source, data)
41+
42+
# 釋放資料物件和來源物件
43+
obs.obs_data_release(data_black)
44+
obs.obs_data_release(data_white)
45+
obs.obs_data_release(data)
46+
obs.obs_source_release(source)

src/zh-hant/junior/black.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"color": 4278914060,
3+
"bk_color": 4294442494,
4+
"bk_opacity": 100,
5+
"font": {
6+
"size": 150
7+
}
8+
}

src/zh-hant/junior/create_data_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
本節文章
3-
https://learnscript.net/zh-hant/obs-python-scripting/junior/data/get-json-from-data/ 如何從資料取得 JSON
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/data/create-data-from-json/ 如何從 JSON 建立資料
44
'''
55

66
# 匯入模組 obspython,json

src/zh-hant/junior/get_json.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def script_properties():
2828

2929
def json_clicked(props, prop):
3030
# 取得腳本設定對應的 JSON 字串
31-
json = obs.obs_data_get_json(data)
32-
obs.script_log(obs.LOG_INFO, f'設定的 JSON:{json}')
31+
json_string = obs.obs_data_get_json(data)
32+
obs.script_log(obs.LOG_INFO, f'設定的 JSON:{json_string}')
3333

3434
# 修改設定項 message
3535
obs.obs_data_set_string(data, 'message', '再見!')
3636

3737

3838
def last_json_clicked(props, prop):
3939
# 取得上一次腳本設定產生的 JSON 字串
40-
json = obs.obs_data_get_last_json(data)
41-
obs.script_log(obs.LOG_INFO, f'最後的 JSON:{json}')
40+
json_string = obs.obs_data_get_last_json(data)
41+
obs.script_log(obs.LOG_INFO, f'最後的 JSON:{json_string}')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
本節文章
3+
https://learnscript.net/zh-hant/obs-python-scripting/junior/data/load-and-save-json/ 如何載入和儲存 JSON
4+
'''
5+
6+
# 匯入模組 obspython
7+
import obspython as obs
8+
9+
def script_properties():
10+
props = obs.obs_properties_create()
11+
12+
# 添加儲存和載入 JSON 檔案的按鈕
13+
obs.obs_properties_add_button(props, 'save', '儲存', save_clicked)
14+
obs.obs_properties_add_button(props, 'load', '載入', load_clicked)
15+
16+
return props
17+
18+
# 玩家經驗值
19+
exp = 0
20+
# 玩家等級
21+
level = 0
22+
23+
def save_clicked(props, prop):
24+
# 建立資料物件,並寫入玩家資訊
25+
data = obs.obs_data_create()
26+
obs.obs_data_set_int(data, 'exp', exp)
27+
obs.obs_data_set_int(data, 'level', level)
28+
29+
# 將資料物件儲存至檔案 player.json,如果檔案存在,則先將其備份為 player.backup.json
30+
obs.obs_data_save_json_safe(data, 'player.json', '.temp', '.backup')
31+
# 釋放資料物件
32+
obs.obs_data_release(data)
33+
34+
def load_clicked(props, prop):
35+
global exp, level
36+
37+
# 從檔案 player.json 載入玩家資訊
38+
data = obs.obs_data_create_from_json_file_safe('player.json', '.backup')
39+
40+
# 讀取玩家的經驗值和等級
41+
exp = obs.obs_data_get_int(data, 'exp')
42+
level = obs.obs_data_get_int(data, 'level')
43+
# 釋放資料物件
44+
obs.obs_data_release(data)
45+
46+
obs.script_log(obs.LOG_INFO, f'經驗值:{exp},等級:{level}')

src/zh-hant/junior/white.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"color": 4294442494,
3+
"bk_color": 4278914060,
4+
"font": {
5+
"face": "Consolas",
6+
"style": "Regular",
7+
"size": 256,
8+
"flags": 0
9+
}
10+
}

src/zh/junior/apply_data.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
本节文章
3+
https://learnscript.net/zh/obs-python-scripting/junior/data/apply-data-to-target/ 如何应用数据至目标
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, 'style', '合并样式', style_clicked)
14+
15+
return props
16+
17+
def style_clicked(props, prop):
18+
data = obs.obs_data_create()
19+
# 从文件 white.json 载入白色样式
20+
data_white = obs.obs_data_create_from_json_file('white.json')
21+
22+
# 将包含白色样式的数据对象合并至 data
23+
obs.obs_data_apply(data, data_white)
24+
# 输出 data 对应的 JSON 字符串
25+
json_string = obs.obs_data_get_json(data)
26+
obs.script_log(obs.LOG_INFO, json_string)
27+
28+
# 从文件 black.json 载入黑色样式
29+
data_black = obs.obs_data_create_from_json_file('black.json')
30+
31+
# 将包含黑色样式的数据对象合并至 data
32+
obs.obs_data_apply(data, data_black)
33+
# 输出 data 对应的 JSON 字符串
34+
json_string = obs.obs_data_get_json(data)
35+
obs.script_log(obs.LOG_INFO, json_string)
36+
37+
# 获取场景中的文本源 Welcome
38+
source = obs.obs_get_source_by_name('Welcome')
39+
# 将样式应用到文本源
40+
obs.obs_source_update(source, data)
41+
42+
# 释放数据对象和源对象
43+
obs.obs_data_release(data_black)
44+
obs.obs_data_release(data_white)
45+
obs.obs_data_release(data)
46+
obs.obs_source_release(source)

src/zh/junior/black.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"color": 4278914060,
3+
"bk_color": 4294442494,
4+
"bk_opacity": 100,
5+
"font": {
6+
"size": 150
7+
}
8+
}

src/zh/junior/create_data_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
本节文章
3-
https://learnscript.net/zh/obs-python-scripting/junior/data/get-json-from-data/ 如何从数据获取 JSON
3+
https://learnscript.net/zh/obs-python-scripting/junior/data/create-data-from-json/ 如何从 JSON 创建数据
44
'''
55

66
# 导入模块 obspython,json

src/zh/junior/get_json.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def script_properties():
2828

2929
def json_clicked(props, prop):
3030
# 获取脚本设置对应的 JSON 字符串
31-
json = obs.obs_data_get_json(data)
32-
obs.script_log(obs.LOG_INFO, f'设置的 JSON:{json}')
31+
json_string = obs.obs_data_get_json(data)
32+
obs.script_log(obs.LOG_INFO, f'设置的 JSON:{json_string}')
3333

3434
# 修改设置项 message
3535
obs.obs_data_set_string(data, 'message', '再见!')
3636

3737

3838
def last_json_clicked(props, prop):
3939
# 获取上一次脚本设置生成的 JSON 字符串
40-
json = obs.obs_data_get_last_json(data)
41-
obs.script_log(obs.LOG_INFO, f'最后的 JSON:{json}')
40+
json_string = obs.obs_data_get_last_json(data)
41+
obs.script_log(obs.LOG_INFO, f'最后的 JSON:{json_string}')

0 commit comments

Comments
 (0)