質問編集履歴
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -105,3 +105,151 @@
|
|
105
105
|
### 求めている解答
|
106
106
|
|
107
107
|
このようなJSONファイルをPythonで作る場合, 辞書以外に有効な方法はありますでしょうか? もしくはできればPythonで解決したいですが他に楽, 現実的に解決できそうな方法等ございますでしょうか?
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
###追記
|
112
|
+
|
113
|
+
実際に使用しているコードはこちらです
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
※フォルダ構成は
|
118
|
+
|
119
|
+
Live2D
|
120
|
+
|
121
|
+
┣ sample(必要なファイルはすべてこの中)
|
122
|
+
|
123
|
+
となっています.
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
```Python
|
128
|
+
|
129
|
+
import os
|
130
|
+
|
131
|
+
import glob
|
132
|
+
|
133
|
+
import json
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
|
138
|
+
|
139
|
+
files = os.listdir("./live2D")
|
140
|
+
|
141
|
+
files_dir = [f for f in files if os.path.isdir(os.path.join("./live2D", f))]
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
for dir in files_dir :
|
146
|
+
|
147
|
+
dic = {"name":"Live2D","model":"moc/Live2D.moc","textures":["Texture2D/texture_00.png"],"motions":{"idle":[{"name":0,"file":0}],"tap":[{"name":0,"file":0}]},"expressions":[{"name":0,"file":0}],"physics":"physics/physics.json","lip_sync":"true"}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
mocfiles = glob.glob('./live2D/'+str(dir)+'/**/*.moc')
|
152
|
+
|
153
|
+
dic["model"] = str(mocfiles)[len(dir)+13:-2].replace('\','/')
|
154
|
+
|
155
|
+
phyfiles = glob.glob('./live2D/'+str(dir)+'/**/*physics.json')
|
156
|
+
|
157
|
+
dic["physics"] = str(phyfiles)[len(dir)+13:-2].replace('\','/')
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
mtnfiles = glob.glob('./live2D/'+str(dir)+'/**/*.mtn')
|
162
|
+
|
163
|
+
exdic = {"idle":[{"name":0,"file":0}],"tap":[{"name":0,"file":0}]}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
for mtn in mtnfiles:
|
168
|
+
|
169
|
+
exdic.update({"file":str(mtn)[len(dir)+10:].replace('\','/')})
|
170
|
+
|
171
|
+
dic ["motions"].update(exdic)
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
with open (r"./Live2D/"+dir+"/model.json","w") as f:
|
176
|
+
|
177
|
+
f.write(json.dumps(dic, ensure_ascii=False, indent=4, sort_keys=False, separators=(',', ': ')))
|
178
|
+
|
179
|
+
print(json.dumps(dic, ensure_ascii=False, indent=4, sort_keys=False, separators=(',', ': ')))
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
実行結果
|
188
|
+
|
189
|
+
```ここに言語を入力
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
"name": "Live2D",
|
194
|
+
|
195
|
+
"model": "moc/Live2D.moc",
|
196
|
+
|
197
|
+
"textures": [
|
198
|
+
|
199
|
+
"Texture2D/texture_00.png"
|
200
|
+
|
201
|
+
],
|
202
|
+
|
203
|
+
"motions": {
|
204
|
+
|
205
|
+
"idle": [
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
"name": 0,
|
210
|
+
|
211
|
+
"file": 0
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
],
|
216
|
+
|
217
|
+
"tap": [
|
218
|
+
|
219
|
+
{
|
220
|
+
|
221
|
+
"name": 0,
|
222
|
+
|
223
|
+
"file": 0
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
],
|
228
|
+
|
229
|
+
"file": "motion/rokka_surprised02.mtn"
|
230
|
+
|
231
|
+
},
|
232
|
+
|
233
|
+
"expressions": [
|
234
|
+
|
235
|
+
{
|
236
|
+
|
237
|
+
"name": 0,
|
238
|
+
|
239
|
+
"file": 0
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
],
|
244
|
+
|
245
|
+
"physics": "physics/physics.json",
|
246
|
+
|
247
|
+
"lip_sync": "true"
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
このように"file": "motion/rokka_surprised02.mtn"しか出力されません. (これは名前順で最後のファイル)
|
1
誤字の訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -104,4 +104,4 @@
|
|
104
104
|
|
105
105
|
### 求めている解答
|
106
106
|
|
107
|
-
このようなJSONファイルをPythonで作る場合
|
107
|
+
このようなJSONファイルをPythonで作る場合, 辞書以外に有効な方法はありますでしょうか? もしくはできればPythonで解決したいですが他に楽, 現実的に解決できそうな方法等ございますでしょうか?
|