radames commited on
Commit
c010459
·
1 Parent(s): c3c2457

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -47
app.py CHANGED
@@ -9,40 +9,41 @@ bpy.data.scenes[0].render.engine = "CYCLES"
9
  # Set the device_type
10
  bpy.context.preferences.addons[
11
  "cycles"
12
- ].preferences.compute_device_type = "CUDA" # or "OPENCL"
13
 
14
  # Set the device and feature set
15
  bpy.context.scene.cycles.device = "GPU"
16
 
17
  for scene in bpy.data.scenes:
18
- scene.cycles.device = "GPU"
19
 
20
  bpy.context.preferences.addons["cycles"].preferences.get_devices()
21
  print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
22
  for d in bpy.context.preferences.addons["cycles"].preferences.devices:
23
- d["use"] = True # Using all devices, include GPU and CPU
24
  print(d["name"])
25
 
26
 
27
  def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=True)):
28
  rgb1 = ImageColor.getcolor(color1, "RGBA")
29
- rgb1 = tuple(v / 255.0 for v in rgb1)
30
  rgb2 = ImageColor.getcolor(color2, "RGBA")
31
- rgb2 = tuple(v / 255.0 for v in rgb2)
32
- print(rgb1, rgb2, light_position)
 
33
 
34
  light_position_normed = light_position / 20
35
  # Delete all mesh objects from the scene
36
- bpy.ops.object.select_all(action="DESELECT")
37
- bpy.ops.object.select_by_type(type="MESH")
38
  bpy.ops.object.delete()
39
 
40
  # Add a torus
41
  bpy.ops.mesh.primitive_torus_add(
42
  major_radius=1.5,
43
  minor_radius=0.75,
44
- major_segments=48 * 4,
45
- minor_segments=12 * 4,
46
  align="WORLD",
47
  location=(0, 1, 1),
48
  )
@@ -63,22 +64,22 @@ def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=Tru
63
 
64
  # Add a Gradient Texture and set it to a color ramp of a rainbow
65
  gradient = nodes.new(type="ShaderNodeTexGradient")
66
- gradient.gradient_type = "LINEAR"
67
- gradient.location = (0, 0)
68
 
69
  ramp = nodes.new(type="ShaderNodeValToRGB")
70
- ramp.color_ramp.interpolation = "LINEAR"
71
- ramp.location = (200, 0)
72
 
73
  ramp.color_ramp.elements[0].color = rgb1
74
  ramp.color_ramp.elements[1].color = rgb2
75
 
76
  # Add Shader nodes
77
  bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
78
- bsdf.location = (400, 0)
79
 
80
  output = nodes.new(type="ShaderNodeOutputMaterial")
81
- output.location = (600, 0)
82
 
83
  # Connect the nodes
84
  material.node_tree.links.new
@@ -102,48 +103,36 @@ def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=Tru
102
 
103
  # Render
104
  path = "test.png"
105
- bpy.context.scene.render.resolution_y = 256
106
- bpy.context.scene.render.resolution_x = 256
107
  bpy.context.scene.render.image_settings.file_format = "PNG"
108
  bpy.context.scene.render.filepath = path
109
 
110
- pbar = tqdm()
111
-
112
- def elapsed(dummy):
113
- pbar.update()
114
-
115
- # bpy.app.handlers.render_pre.append(start_timer)
116
- bpy.app.handlers.render_stats.append(elapsed)
117
- bpy.ops.render.render(animation=False, write_still=True)
118
- pbar.close()
119
- # bpy.ops.render.render(write_still=True)
120
- bpy.data.images["Render Result"].save_render(
121
- filepath=bpy.context.scene.render.filepath
122
- )
123
-
124
- # # display(Image("test_sphere.png"))
125
-
126
- # # Read the saved image into memory and encode it to base64
127
- temp_filepath = Path(bpy.context.scene.render.filepath)
128
-
129
- # bpy.app.handlers.render_pre.clear()
130
- bpy.app.handlers.render_stats.clear()
131
- return path
132
-
133
 
134
  # generate("#ffffff", "#aaa", 1)
135
  with gr.Blocks() as demo:
136
  with gr.Row():
137
  with gr.Column():
138
- color1 = gr.ColorPicker()
139
- color2 = gr.ColorPicker()
140
- slider = gr.Slider(minimum=0, maximum=100, value=1)
 
141
  with gr.Column(scale=3):
142
- image = gr.Image(type="filepath")
143
 
144
- slider.change(generate, inputs=[color1, color2, slider], outputs=[image])
145
 
146
 
147
  # bpy.utils.register_class(generate)
148
  demo.queue()
149
- demo.launch(debug=True, inline=True)
 
9
  # Set the device_type
10
  bpy.context.preferences.addons[
11
  "cycles"
12
+ ].preferences.compute_device_type = "CUDA" # or "OPENCL"
13
 
14
  # Set the device and feature set
15
  bpy.context.scene.cycles.device = "GPU"
16
 
17
  for scene in bpy.data.scenes:
18
+ scene.cycles.device = 'GPU'
19
 
20
  bpy.context.preferences.addons["cycles"].preferences.get_devices()
21
  print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
22
  for d in bpy.context.preferences.addons["cycles"].preferences.devices:
23
+ d["use"] = True # Using all devices, include GPU and CPU
24
  print(d["name"])
25
 
26
 
27
  def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=True)):
28
  rgb1 = ImageColor.getcolor(color1, "RGBA")
29
+ rgb1 = tuple(v/255.0 for v in rgb1)
30
  rgb2 = ImageColor.getcolor(color2, "RGBA")
31
+ rgb2 = tuple(v/255.0 for v in rgb2)
32
+ print(rgb1, rgb2 , light_position)
33
+
34
 
35
  light_position_normed = light_position / 20
36
  # Delete all mesh objects from the scene
37
+ bpy.ops.object.select_all(action='DESELECT')
38
+ bpy.ops.object.select_by_type(type='MESH')
39
  bpy.ops.object.delete()
40
 
41
  # Add a torus
42
  bpy.ops.mesh.primitive_torus_add(
43
  major_radius=1.5,
44
  minor_radius=0.75,
45
+ major_segments=48*4,
46
+ minor_segments=12*4,
47
  align="WORLD",
48
  location=(0, 1, 1),
49
  )
 
64
 
65
  # Add a Gradient Texture and set it to a color ramp of a rainbow
66
  gradient = nodes.new(type="ShaderNodeTexGradient")
67
+ gradient.gradient_type = 'LINEAR'
68
+ gradient.location = (0,0)
69
 
70
  ramp = nodes.new(type="ShaderNodeValToRGB")
71
+ ramp.color_ramp.interpolation = 'LINEAR'
72
+ ramp.location = (200,0)
73
 
74
  ramp.color_ramp.elements[0].color = rgb1
75
  ramp.color_ramp.elements[1].color = rgb2
76
 
77
  # Add Shader nodes
78
  bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
79
+ bsdf.location = (400,0)
80
 
81
  output = nodes.new(type="ShaderNodeOutputMaterial")
82
+ output.location = (600,0)
83
 
84
  # Connect the nodes
85
  material.node_tree.links.new
 
103
 
104
  # Render
105
  path = "test.png"
106
+ bpy.context.scene.render.resolution_y = 128
107
+ bpy.context.scene.render.resolution_x = 128
108
  bpy.context.scene.render.image_settings.file_format = "PNG"
109
  bpy.context.scene.render.filepath = path
110
 
111
+ with tqdm(total=bpy.context.scene.frame_end) as pbar:
112
+ def elapsed(dummy):
113
+ pbar.update()
114
+
115
+ bpy.app.handlers.render_stats.append(elapsed)
116
+ bpy.ops.render.render(animation=False, write_still=True)
117
+ bpy.data.images["Render Result"].save_render(filepath=bpy.context.scene.render.filepath)
118
+ temp_filepath = Path(bpy.context.scene.render.filepath)
119
+ bpy.app.handlers.render_stats.clear()
120
+ return path
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  # generate("#ffffff", "#aaa", 1)
123
  with gr.Blocks() as demo:
124
  with gr.Row():
125
  with gr.Column():
126
+ color1 = gr.ColorPicker()
127
+ color2 = gr.ColorPicker()
128
+ slider = gr.Slider(minimum=0, maximum=100, value=1)
129
+ render_btn = gr.Button("Render")
130
  with gr.Column(scale=3):
131
+ image = gr.Image(type="filepath")
132
 
133
+ render_btn.click(generate, inputs=[color1, color2, slider], outputs=[image])
134
 
135
 
136
  # bpy.utils.register_class(generate)
137
  demo.queue()
138
+ demo.launch(debug=True, inline=True)