Spaces:
Sleeping
Sleeping
kolibril13
commited on
Commit
·
4a674ae
1
Parent(s):
2283d25
make this a minimal example
Browse files- app.py +13 -92
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,100 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
import bpy
|
3 |
-
from tqdm import tqdm
|
4 |
-
from math import pi
|
5 |
import tempfile
|
6 |
-
import molecularnodes as mn
|
7 |
-
import os
|
8 |
|
9 |
-
def enable_GPUS():
|
10 |
-
bpy.data.scenes[0].render.engine = "CYCLES" #"CYCLES"
|
11 |
-
# Set the device_type
|
12 |
-
bpy.context.preferences.addons[
|
13 |
-
"cycles"
|
14 |
-
].preferences.compute_device_type = "CUDA" # or "OPENCL"
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
for scene in bpy.data.scenes:
|
20 |
-
scene.cycles.device = "GPU"
|
21 |
-
|
22 |
-
bpy.context.preferences.addons["cycles"].preferences.get_devices()
|
23 |
-
print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
|
24 |
-
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
|
25 |
-
d["use"] = True # Using all devices, include GPU and CPU
|
26 |
-
print(d["name"])
|
27 |
-
|
28 |
-
|
29 |
-
enable_GPUS()
|
30 |
-
|
31 |
-
window = bpy.context.window
|
32 |
-
screen = window.screen
|
33 |
-
|
34 |
-
style = 'cartoon'
|
35 |
-
|
36 |
-
nodes_to_append = ["MN_color_set",
|
37 |
-
"MN_color_common",
|
38 |
-
"MN_color_attribute_random",
|
39 |
-
mn.nodes.styles_mapping[style]]
|
40 |
-
|
41 |
-
def get_areas(type):
|
42 |
-
return [area for area in screen.areas if area.type == type]
|
43 |
-
|
44 |
-
def get_regions(areas):
|
45 |
-
return [region for region in areas[0].regions if region.type == 'WINDOW']
|
46 |
-
|
47 |
-
for node in nodes_to_append:
|
48 |
-
bpy.ops.wm.append(
|
49 |
-
'INVOKE_DEFAULT',
|
50 |
-
directory = os.path.join(mn.nodes.mn_data_file, 'NodeTree'),
|
51 |
-
filename = node,
|
52 |
-
link = False
|
53 |
-
)
|
54 |
-
|
55 |
-
def generate(progress=gr.Progress(track_tqdm=True)):
|
56 |
-
area_type = 'VIEW_3D'
|
57 |
-
areas = get_areas(area_type)
|
58 |
-
with bpy.context.temp_override(window=window, area=areas[0], region=get_regions(areas)[0], screen=screen):
|
59 |
-
|
60 |
-
|
61 |
-
for obj in bpy.context.scene.objects:
|
62 |
-
if obj.type == 'MESH':
|
63 |
-
bpy.data.objects.remove(obj, do_unlink=True)
|
64 |
-
|
65 |
-
molecule = mn.load.molecule_rcsb("7TYG", starting_style=style, center_molecule=True)
|
66 |
-
molecule.select_set(True)
|
67 |
-
bpy.context.view_layer.objects.active = molecule
|
68 |
-
|
69 |
-
bpy.ops.view3d.camera_to_view_selected()
|
70 |
-
camera = bpy.data.objects["Camera"]
|
71 |
-
camera.data.dof.use_dof = True
|
72 |
-
camera.data.dof.focus_distance = 5
|
73 |
-
camera.data.dof.aperture_fstop = 4
|
74 |
-
camera.data.angle = pi / 3
|
75 |
-
camera.data.type = "PERSP"
|
76 |
-
|
77 |
-
with tempfile.NamedTemporaryFile(suffix=".JPEG", delete=False) as f:
|
78 |
-
bpy.context.scene.render.resolution_y = 1000
|
79 |
-
bpy.context.scene.render.resolution_x = 1000
|
80 |
-
bpy.context.scene.render.image_settings.file_format = "JPEG"
|
81 |
-
bpy.context.scene.render.filepath = f.name
|
82 |
-
|
83 |
-
with tqdm() as pbar:
|
84 |
-
|
85 |
-
def elapsed(dummy):
|
86 |
-
pbar.update()
|
87 |
-
|
88 |
-
bpy.app.handlers.render_stats.append(elapsed)
|
89 |
-
bpy.context.scene.frame_set(1)
|
90 |
-
bpy.context.scene.frame_current = 1
|
91 |
-
bpy.ops.render.render(animation=False, write_still=True)
|
92 |
-
|
93 |
-
bpy.data.images["Render Result"].save_render(
|
94 |
-
filepath=bpy.context.scene.render.filepath
|
95 |
-
)
|
96 |
-
bpy.app.handlers.render_stats.clear()
|
97 |
-
return f.name
|
98 |
|
99 |
with gr.Blocks() as demo:
|
100 |
with gr.Row():
|
@@ -109,4 +30,4 @@ with gr.Blocks() as demo:
|
|
109 |
)
|
110 |
|
111 |
demo.queue(concurrency_count=1)
|
112 |
-
demo.launch(debug=True, inline=True)
|
|
|
1 |
import gradio as gr
|
2 |
import bpy
|
|
|
|
|
3 |
import tempfile
|
|
|
|
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def generate():
|
7 |
+
with tempfile.NamedTemporaryFile(suffix=".JPEG", delete=False) as f:
|
8 |
+
bpy.context.scene.render.resolution_y = 200
|
9 |
+
bpy.context.scene.render.resolution_x = 400
|
10 |
+
bpy.context.scene.render.image_settings.file_format = "JPEG"
|
11 |
+
bpy.context.scene.render.filepath = f.name
|
12 |
+
bpy.ops.render.render(animation=False, write_still=True)
|
13 |
+
bpy.data.images["Render Result"].save_render(
|
14 |
+
filepath=bpy.context.scene.render.filepath
|
15 |
+
)
|
16 |
+
bpy.app.handlers.render_stats.clear()
|
17 |
+
return f.name
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
with gr.Blocks() as demo:
|
21 |
with gr.Row():
|
|
|
30 |
)
|
31 |
|
32 |
demo.queue(concurrency_count=1)
|
33 |
+
demo.launch(debug=True, inline=True)
|
requirements.txt
CHANGED
@@ -1,2 +1 @@
|
|
1 |
-
bpy
|
2 |
-
molecularnodes
|
|
|
1 |
+
bpy
|
|