-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
205 lines (186 loc) · 10.8 KB
/
app.py
File metadata and controls
205 lines (186 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import streamlit as st
from pathlib import Path
from widgets import Widgets,STYLE
from structure import Structure
from function import parsestr2list,get_unique_items
from make_script_for_app import make_script,make_combine_script,make_animation_script
def _make_animation_kwargs(structure):
unique_symbols = structure.get_unique_symbols()
property_dict = {}
property_dict["scale"] = st.session_state["scale"]
property_dict["sizes"] = {symb:st.session_state[f"size_{symb}"] for symb in unique_symbols}
property_dict["colors"] = {symb:st.session_state[f"color_{symb}"] for symb in unique_symbols}
return property_dict
def make_animation_kwargs(structure,index_list=None,view_index=False,multi_structure=True):
property_dict = _make_animation_kwargs(structure)
return {"properties":property_dict,"index_list":index_list,"multi_structure":multi_structure,"view_index":view_index}
st.set_page_config(
page_title="Blender用Pythonスクリプト作成",
page_icon="./logo.png",
menu_items={'About': 'https://shimi-lab.github.io/GRRMPY_document/src/blender_gallery.html'})
widgets = Widgets()
widgets.structure_file_upload_widgets()
if st.session_state["upload_file"] is not None:
widgets.structure_style_widgets()
widgets.color_scale_widgets()
file = st.session_state["upload_file"]
format = None if st.session_state["format"]=="自動検出" else st.session_state["format"]
if st.session_state["color_scale_selectbox"] == "import element.ini ...":
widgets.user_color_scale_widgets()
if st.session_state["style_radio"] == STYLE[5]: #"Animation":
multi_structure = True
structure = Structure(file,format,multi_structure=True)
widgets.atoms_property_widgets(structure.atoms[0],space_filling=True)
widgets.animations_property_widgets()
# widgets.make_buttun_widgets(structure.atoms,True)
widgets.animation_widgets()
elif st.session_state["style_radio"] == STYLE[6]: # "Combine"
widgets.combine_widgets()
structure = Structure(file,format,multi_structure=False) # とりあえずmulti_structure=Falseで作成
for style in st.session_state["multi_style_box"]:
widgets.combine_property_widgets(style,structure.atoms,value=f"0-{structure.natoms()-1}")
widgets.label_widgets()
if "Animation" in st.session_state["multi_style_box"]:
widgets.animation_widgets()
radio_disabled = True
else:
radio_disabled = False
# widgets.make_buttun_widgets(structure.atoms,radio_disabled)
else:
radio_disabled = False
structure = Structure(file,format,multi_structure=False)
if st.session_state["style_radio"] == STYLE[0]: # "Ball and Stick"
widgets.atoms_property_widgets(structure.atoms)
widgets.bond_propert_widgets(structure.atoms,bicolor=False)
elif st.session_state["style_radio"] == STYLE[1]: # "Ball and Stick (bicolor)"
widgets.atoms_property_widgets(structure.atoms)
widgets.bond_propert_widgets(structure.atoms,bicolor=True)
elif st.session_state["style_radio"] == STYLE[2]: # "Space Filling"
widgets.atoms_property_widgets(structure.atoms,space_filling=True)
elif st.session_state["style_radio"] == STYLE[3]: # "Stick"
widgets.bond_propert_widgets(structure.atoms,bicolor=False)
elif st.session_state["style_radio"] == STYLE[4]: # "Stick (bicilor)"
widgets.bond_propert_widgets(structure.atoms,bicolor=True,stick_color=True)
# widgets.make_buttun_widgets(structure.atoms,radio_disabled)
widgets.render_widgets()
###### 構造の可視化 #######
style = st.session_state["style_radio"]
if style == "Animation":
if st.session_state["one_structure"] == "1枚目の構造のみ表示":
kwargs = make_animation_kwargs(structure,view_index=False,multi_structure=False)
else:
kwargs = make_animation_kwargs(structure,view_index=False,multi_structure=True)
structure.view(style,**kwargs)
elif style == "Combine":
apply_style_list = st.session_state["multi_style_box"]
index_list = [list(set(parsestr2list(st.session_state[f"index_{style}"]))) for style in apply_style_list]
index_list = get_unique_items(index_list)
if "Animation" in apply_style_list and st.session_state["one_structure"] != "1枚目の構造のみ表示":
# partial_atoms_dictの中身は{style:[images,index]}
structure = Structure(file,format,multi_structure=True) #multi_structure=Trueに更新
partial_atoms_dict = {
style: [[atoms[idx] for atoms in structure.atoms] ,idx]
if style == "Animation" else
[[structure.atoms[0][idx] for _ in structure.atoms],idx]
for style,idx in zip(apply_style_list,index_list) if not idx==[]}
else:
# partial_atoms_dictの中身は{style:[atoms,index]}
partial_atoms_dict = {style:[structure.atoms[idx],idx] for style,idx in zip(apply_style_list,index_list) if not idx==[]}
property_list = []
for style,(atoms,index) in partial_atoms_dict.items():
if "Animation" in apply_style_list and st.session_state["one_structure"] != "1枚目の構造のみ表示":
unique_symbols = list(set(atoms[0].get_chemical_symbols()))
animation = True
else:
unique_symbols = list(set(atoms.get_chemical_symbols()))
animation = False
property_dict = {}
property_dict["style"] = style
property_dict["index"] = index
if style in ["Ball and Stick","Ball and Stick (bicolor)","Space Filling","Animation"]:
property_dict["scale"] = st.session_state[f"scale_{style}"]
property_dict["sizes"] = {symb:st.session_state[f"size_{style}_{symb}"] for symb in unique_symbols}
if style in ["Ball and Stick","Ball and Stick (bicolor)","Space Filling","Stick (bicolor)","Animation"]:
property_dict["colors"] = {symb:st.session_state[f"color_{style}_{symb}"] for symb in unique_symbols}
if style in ["Ball and Stick","Stick"]:
property_dict["bond_color"] = st.session_state[f"bond_color_{style}"]
if style in ["Ball and Stick","Ball and Stick (bicolor)","Stick","Stick (bicolor)"]:
property_dict["radius"] = st.session_state[f"radius_{style}"]
property_list.append(property_dict)
partial_atoms_list = [atoms for atoms,_ in partial_atoms_dict.values()]
show_index = True if st.session_state["show_index"] else None
structure.view_combine(partial_atoms_list,property_list,show_index=show_index,animation=animation)
else:
unique_symbols = structure.get_unique_symbols()
property_dict = {}
if style in ["Ball and Stick","Ball and Stick (bicolor)","Space Filling"]:
property_dict["scale"] = st.session_state["scale"]
property_dict["sizes"] = {symb:st.session_state[f"size_{symb}"] for symb in unique_symbols}
if style in ["Ball and Stick","Ball and Stick (bicolor)","Space Filling","Stick (bicolor)"]:
property_dict["colors"] = {symb:st.session_state[f"color_{symb}"] for symb in unique_symbols}
if style in ["Ball and Stick","Stick"]:
property_dict["bond_color"] = st.session_state["bond_color"]
if style in ["Ball and Stick","Ball and Stick (bicolor)","Stick","Stick (bicolor)"]:
property_dict["radius"] = st.session_state["radius"]
structure.view(style,property_dict)
###################################################
if st.session_state["style_radio"] == STYLE[5]: #"Animation":
file_name = str(Path(st.session_state["upload_file"].name).with_suffix(".zip"))
zipbite = make_animation_script(structure)
widgets.download_widgets(zipbite,file_name=file_name)
elif st.session_state["style_radio"] == STYLE[6]: # "Combine":
if "Animation" in st.session_state["multi_style_box"]:
structure = Structure(file,format,multi_structure=True) #multi_structure=Trueに更新
partial_atoms_list = [[atoms[idx] for atoms in structure.atoms]
if style == "Animation" else
structure.atoms[0][idx]
for style,idx in zip(apply_style_list,index_list) if not idx==[]]
zipbite = make_combine_script(partial_atoms_list,property_list)
file_name = str(Path(st.session_state["upload_file"].name).with_suffix(".zip"))
widgets.download_widgets(zipbite,file_name=file_name)
else:
make_combine_script(partial_atoms_list,property_list)
widgets.script_widgets()
else:
make_script(structure.atoms)
widgets.script_widgets()
else:
st.title("Blender用Pythonスクリプト作成")
st.markdown(
"""
### はじめに
左のサイドバーに構造ファイルをドラック&ドロップ
Belnder2.93.4で動作確認済み
### ギャラリー
https://shimi-lab.github.io/GRRMPY_document/src/blender_gallery.html
### 使い方
"""
)
st.write("構造ファイルをドラッグ&ドロップ")
st.image("img/drag_and_drop.png")
st.write("Pythonスクリプトをコピー")
st.image("img/copy.png")
st.write("Blenderを開き,テキストエディターを開く")
st.image("img/text_editor.png")
st.write("新規をクリックし,コピーしたスクリプトを貼り付け,実行する")
st.image("img/run.png")
st.write("3Dビューポートに戻る.シェダーに切り替えると色がついていることを確認できる")
st.image("img/finish.png")
st.write("カメラとライトを設定し,レンダリングする")
st.markdown(
"""
### Renderについて
Cartoonを適用すると,漫画風にレンダリングすることができる.
Subdivistionを適用すると,球(原子)が表面がより滑らかになる
**どちらの項目もこのアプリ上での変化は起きない** """)
st.image("img/Render.png")
st.markdown(
"""
#### Cartoon
Cartoonにチェックを入れた場合,原子の色はShadingから変更することができる.
(マテリアルプロパティーから変更することができない)
""")
st.image("img/Shading.png")
st.write("ミックスの色1で原子の色,色2で枠線の色を変えることができる.")
st.write("IOR値を変えることで枠線の太さを変えることができる")
st.image("img/Node.png")