radames commited on
Commit
f08d344
·
1 Parent(s): c45097a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -32
app.py CHANGED
@@ -4,13 +4,14 @@ from PIL import ImageColor
4
  from pathlib import Path
5
  import bpy
6
 
7
- blender_context = bpy.context
 
 
 
 
 
 
8
 
9
- async def generate(color1, color2, light_position):
10
- rbg1 = ImageColor.getcolor(color1, "RGB")
11
- rgb2 = ImageColor.getcolor(color2, "RGB")
12
- print(rbg1, rgb2 , light_position)
13
-
14
  light_position_normed = light_position / 20
15
  # Delete all mesh objects from the scene
16
  bpy.ops.object.select_all(action='DESELECT')
@@ -18,16 +19,19 @@ async def generate(color1, color2, light_position):
18
  bpy.ops.object.delete()
19
 
20
  # Add a torus
21
- print(bpy.ops.mesh.primitive_torus_add(
22
- major_radius=1.5,
23
- minor_radius=0.75,
24
- major_segments=48*4,
25
- minor_segments=12*4,
26
- align="WORLD",
27
  location=(0, 1, 1),
28
- ))
29
- torus = blender_context.active_object
30
 
 
 
 
 
31
  # Create a new material and assign it to the torus
32
  material = bpy.data.materials.new(name="RainbowGradient")
33
  torus.data.materials.append(material)
@@ -48,7 +52,7 @@ async def generate(color1, color2, light_position):
48
  ramp.location = (200,0)
49
 
50
  ramp.color_ramp.elements[0].color = rgb1
51
- ramp.color_ramp.elements[1].color = rbg2
52
 
53
  # Add Shader nodes
54
  bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
@@ -68,35 +72,36 @@ async def generate(color1, color2, light_position):
68
 
69
  # Light
70
  light = bpy.data.objects["Light"]
71
- light.location = (light_position_normed, 0, 2) # Position the light
72
 
73
  # Camera
74
  camera = bpy.data.objects["Camera"]
75
  camera.location = (5, -3, 4)
76
  camera.data.dof.use_dof = True
77
  camera.data.dof.focus_distance = 5
78
- camera.data.dof.aperture_fstop = 4
79
 
80
  # Render
81
  path = "test.png"
82
- blender_context.scene.render.resolution_x = 1000
83
- blender_context.scene.render.resolution_y = 400
84
- blender_context.scene.render.image_settings.file_format = "PNG"
85
- blender_context.scene.render.filepath = path
86
  bpy.ops.render.render(write_still=True)
87
- bpy.data.images["Render Result"].save_render(filepath=blender_context.scene.render.filepath)
88
-
89
- # display(Image("test_sphere.png"))
90
 
91
- # Read the saved image into memory and encode it to base64
92
- temp_filepath = Path(blender_context.scene.render.filepath)
93
- with temp_filepath.open("rb") as f:
94
- my_img = base64.b64encode(f.read()).decode("utf-8")
95
 
96
- return temp_filepath
 
 
 
 
 
97
 
 
98
  with gr.Blocks() as demo:
99
- with gr.Row():
100
  with gr.Column():
101
  color1 = gr.ColorPicker()
102
  color2 = gr.ColorPicker()
@@ -107,5 +112,5 @@ with gr.Blocks() as demo:
107
  slider.change(generate, inputs=[color1, color2, slider], outputs=[image], show_progress=False)
108
 
109
 
110
-
111
- demo.launch(debug=True)
 
4
  from pathlib import Path
5
  import bpy
6
 
7
+ def generate(color1, color2, light_position):
8
+ rgb1 = ImageColor.getcolor(color1, "RGBA")
9
+ rgb1 = tuple(v/255.0 for v in rgb1)
10
+ rgb2 = ImageColor.getcolor(color2, "RGBA")
11
+ rgb2 = tuple(v/255.0 for v in rgb2)
12
+ print(rgb1, rgb2 , light_position)
13
+
14
 
 
 
 
 
 
15
  light_position_normed = light_position / 20
16
  # Delete all mesh objects from the scene
17
  bpy.ops.object.select_all(action='DESELECT')
 
19
  bpy.ops.object.delete()
20
 
21
  # Add a torus
22
+ bpy.ops.mesh.primitive_torus_add(
23
+ major_radius=1.5,
24
+ minor_radius=0.75,
25
+ major_segments=48*4,
26
+ minor_segments=12*4,
27
+ align="WORLD",
28
  location=(0, 1, 1),
29
+ )
 
30
 
31
+ # Assigning the torus to a variable
32
+ # torus = bpy.context.active_object
33
+ torus = bpy.context.view_layer.objects.active
34
+ # print(torus)
35
  # Create a new material and assign it to the torus
36
  material = bpy.data.materials.new(name="RainbowGradient")
37
  torus.data.materials.append(material)
 
52
  ramp.location = (200,0)
53
 
54
  ramp.color_ramp.elements[0].color = rgb1
55
+ ramp.color_ramp.elements[1].color = rgb2
56
 
57
  # Add Shader nodes
58
  bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
 
72
 
73
  # Light
74
  light = bpy.data.objects["Light"]
75
+ light.location = (light_position_normed, 0, 2) # Position the light
76
 
77
  # Camera
78
  camera = bpy.data.objects["Camera"]
79
  camera.location = (5, -3, 4)
80
  camera.data.dof.use_dof = True
81
  camera.data.dof.focus_distance = 5
82
+ camera.data.dof.aperture_fstop = 4
83
 
84
  # Render
85
  path = "test.png"
86
+ bpy.context.scene.render.resolution_x = 200
87
+ bpy.context.scene.render.resolution_y = 100
88
+ bpy.context.scene.render.image_settings.file_format = "PNG"
89
+ bpy.context.scene.render.filepath = path
90
  bpy.ops.render.render(write_still=True)
91
+ bpy.data.images["Render Result"].save_render(filepath=bpy.context.scene.render.filepath)
 
 
92
 
93
+ # # display(Image("test_sphere.png"))
 
 
 
94
 
95
+ # # Read the saved image into memory and encode it to base64
96
+ temp_filepath = Path(bpy.context.scene.render.filepath)
97
+ # with temp_filepath.open("rb") as f:
98
+ # my_img = base64.b64encode(f.read()).decode("utf-8")
99
+ # print(temp_filepath)
100
+ return path
101
 
102
+ # generate("#ffffff", "#aaa", 1)
103
  with gr.Blocks() as demo:
104
+ with gr.Row():
105
  with gr.Column():
106
  color1 = gr.ColorPicker()
107
  color2 = gr.ColorPicker()
 
112
  slider.change(generate, inputs=[color1, color2, slider], outputs=[image], show_progress=False)
113
 
114
 
115
+ # bpy.utils.register_class(generate)
116
+ demo.launch(inline=True ,debug=True)