kolibril13 commited on
Commit
05f01a3
1 Parent(s): 52ab6e8

re-add molecular

Browse files
Files changed (2) hide show
  1. app.py +72 -15
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  import bpy
 
 
3
  import tempfile
 
 
4
 
5
  def enable_GPUS():
6
  bpy.data.scenes[0].render.engine = "CYCLES" #"CYCLES"
@@ -22,21 +26,75 @@ def enable_GPUS():
22
  print(d["name"])
23
 
24
 
 
25
 
 
 
26
 
27
- def generate():
28
- with tempfile.NamedTemporaryFile(suffix=".JPEG", delete=False) as f:
29
- bpy.context.scene.render.resolution_y = 200
30
- bpy.context.scene.render.resolution_x = 400
31
- bpy.context.scene.render.image_settings.file_format = "JPEG"
32
- bpy.context.scene.render.filepath = f.name
33
- enable_GPUS()
34
- bpy.ops.render.render(animation=False, write_still=True)
35
- bpy.data.images["Render Result"].save_render(
36
- filepath=bpy.context.scene.render.filepath
37
- )
38
- bpy.app.handlers.render_stats.clear()
39
- return f.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  with gr.Blocks() as demo:
42
  with gr.Row():
@@ -51,5 +109,4 @@ with gr.Blocks() as demo:
51
  )
52
 
53
  demo.queue(concurrency_count=1)
54
- demo.launch(debug=True, inline=True)
55
- w
 
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"
 
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 = 400
79
+ bpy.context.scene.render.resolution_x = 600
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
  )
110
 
111
  demo.queue(concurrency_count=1)
112
+ demo.launch(debug=True, inline=True)
 
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- bpy
 
 
1
+ bpy
2
+ molecularnodes