radames commited on
Commit
429630e
1 Parent(s): 400ec44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import base64
3
+ from PIL import ImageColor
4
+ from pathlib import Path
5
+ import bpy
6
+
7
+ def generate(color1, color2, light_position):
8
+ rbg1 = ImageColor.getcolor(color1, "RGB")
9
+ rgb2 = ImageColor.getcolor(color2, "RGB")
10
+ print(rbg1, rgb2 , light_position)
11
+
12
+ light_position_normed = light_position / 20
13
+ # Delete all mesh objects from the scene
14
+ bpy.ops.object.select_all(action='DESELECT')
15
+ bpy.ops.object.select_by_type(type='MESH')
16
+ bpy.ops.object.delete()
17
+ print(bpy.ops.object)
18
+ # Add a torus
19
+ bpy.ops.mesh.primitive_torus_add(
20
+ major_radius=1.5,
21
+ minor_radius=0.75,
22
+ major_segments=48*4,
23
+ minor_segments=12*4,
24
+ align="WORLD",
25
+ location=(0, 1, 1),
26
+ )
27
+
28
+ # Assigning the torus to a variable
29
+ torus = bpy.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)
34
+ material.use_nodes = True
35
+ nodes = material.node_tree.nodes
36
+
37
+ # Clear default nodes
38
+ for node in nodes:
39
+ nodes.remove(node)
40
+
41
+ # Add a Gradient Texture and set it to a color ramp of a rainbow
42
+ gradient = nodes.new(type="ShaderNodeTexGradient")
43
+ gradient.gradient_type = 'LINEAR'
44
+ gradient.location = (0,0)
45
+
46
+ ramp = nodes.new(type="ShaderNodeValToRGB")
47
+ ramp.color_ramp.interpolation = 'LINEAR'
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")
55
+ bsdf.location = (400,0)
56
+
57
+ output = nodes.new(type="ShaderNodeOutputMaterial")
58
+ output.location = (600,0)
59
+
60
+ # Connect the nodes
61
+ material.node_tree.links.new
62
+ material.node_tree.links.new(gradient.outputs["Color"], ramp.inputs[0])
63
+ material.node_tree.links.new(ramp.outputs["Color"], bsdf.inputs["Base Color"])
64
+ material.node_tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
65
+
66
+ # Rotate the gradient to apply it from left to right
67
+ torus.rotation_euler = (0, 0, 1.5708) # Rotate 90 degrees on the Z axis
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
+ bpy.context.scene.render.resolution_x = 1000
83
+ bpy.context.scene.render.resolution_y = 400
84
+ bpy.context.scene.render.image_settings.file_format = "PNG"
85
+ bpy.context.scene.render.filepath = path
86
+ bpy.ops.render.render(write_still=True)
87
+ bpy.data.images["Render Result"].save_render(filepath=bpy.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(bpy.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()
103
+ slider = gr.Slider(minimum=0, maximum=100, value=1)
104
+ with gr.Column(scale=3):
105
+ image = gr.Image(type="filepath")
106
+
107
+ slider.change(generate, inputs=[color1, color2, slider], outputs=[image], show_progress=False)
108
+
109
+
110
+
111
+ demo.launch(debug=True)