Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,46 +4,60 @@ from PIL import ImageColor
|
|
4 |
from pathlib import Path
|
5 |
import bpy
|
6 |
from tqdm import tqdm
|
|
|
7 |
|
8 |
-
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 |
-
|
15 |
-
bpy.
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
scene.cycles.device =
|
19 |
|
20 |
-
bpy.
|
21 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
38 |
-
bpy.ops.object.select_by_type(type=
|
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=
|
46 |
-
minor_segments=
|
47 |
align="WORLD",
|
48 |
location=(0, 1, 1),
|
49 |
)
|
@@ -64,22 +78,22 @@ def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=Tru
|
|
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 =
|
68 |
-
gradient.location = (0,0)
|
69 |
|
70 |
ramp = nodes.new(type="ShaderNodeValToRGB")
|
71 |
-
ramp.color_ramp.interpolation =
|
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
|
@@ -88,7 +102,11 @@ def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=Tru
|
|
88 |
material.node_tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
|
89 |
|
90 |
# Rotate the gradient to apply it from left to right
|
91 |
-
torus.rotation_euler = (
|
|
|
|
|
|
|
|
|
92 |
|
93 |
# Light
|
94 |
light = bpy.data.objects["Light"]
|
@@ -96,43 +114,72 @@ def generate(color1, color2, light_position, progress=gr.Progress(track_tqdm=Tru
|
|
96 |
|
97 |
# Camera
|
98 |
camera = bpy.data.objects["Camera"]
|
99 |
-
camera.location = (
|
100 |
camera.data.dof.use_dof = True
|
101 |
camera.data.dof.focus_distance = 5
|
102 |
camera.data.dof.aperture_fstop = 4
|
103 |
|
104 |
# Render
|
105 |
path = "test.png"
|
106 |
-
bpy.context.scene.render.resolution_y =
|
107 |
-
bpy.context.scene.render.resolution_x =
|
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 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# generate("#ffffff", "#aaa", 1)
|
123 |
with gr.Blocks() as demo:
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
with gr.Column(scale=3):
|
131 |
-
|
132 |
-
|
133 |
-
render_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
|
136 |
# bpy.utils.register_class(generate)
|
137 |
demo.queue()
|
138 |
-
demo.launch(debug=True, inline=True)
|
|
|
4 |
from pathlib import Path
|
5 |
import bpy
|
6 |
from tqdm import tqdm
|
7 |
+
from math import pi
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def enable_GPUS():
|
11 |
+
bpy.data.scenes[0].render.engine = "CYCLES"
|
12 |
+
# Set the device_type
|
13 |
+
bpy.context.preferences.addons[
|
14 |
+
"cycles"
|
15 |
+
].preferences.compute_device_type = "CUDA" # or "OPENCL"
|
16 |
|
17 |
+
# Set the device and feature set
|
18 |
+
bpy.context.scene.cycles.device = "GPU"
|
19 |
|
20 |
+
for scene in bpy.data.scenes:
|
21 |
+
scene.cycles.device = "GPU"
|
|
|
|
|
|
|
22 |
|
23 |
+
bpy.context.preferences.addons["cycles"].preferences.get_devices()
|
24 |
+
print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
|
25 |
+
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
|
26 |
+
d["use"] = True # Using all devices, include GPU and CPU
|
27 |
+
print(d["name"])
|
28 |
|
29 |
+
|
30 |
+
enable_GPUS()
|
31 |
+
|
32 |
+
|
33 |
+
def generate(
|
34 |
+
color1,
|
35 |
+
color2,
|
36 |
+
camera_X,
|
37 |
+
camera_Y,
|
38 |
+
camera_Z,
|
39 |
+
torus_X,
|
40 |
+
torus_Y,
|
41 |
+
torus_Z,
|
42 |
+
progress=gr.Progress(track_tqdm=True),
|
43 |
+
):
|
44 |
rgb1 = ImageColor.getcolor(color1, "RGBA")
|
45 |
+
rgb1 = tuple(v / 255.0 for v in rgb1)
|
46 |
rgb2 = ImageColor.getcolor(color2, "RGBA")
|
47 |
+
rgb2 = tuple(v / 255.0 for v in rgb2)
|
|
|
|
|
48 |
|
49 |
light_position_normed = light_position / 20
|
50 |
# Delete all mesh objects from the scene
|
51 |
+
bpy.ops.object.select_all(action="DESELECT")
|
52 |
+
bpy.ops.object.select_by_type(type="MESH")
|
53 |
bpy.ops.object.delete()
|
54 |
|
55 |
# Add a torus
|
56 |
bpy.ops.mesh.primitive_torus_add(
|
57 |
major_radius=1.5,
|
58 |
minor_radius=0.75,
|
59 |
+
major_segments=2**7,
|
60 |
+
minor_segments=2**5,
|
61 |
align="WORLD",
|
62 |
location=(0, 1, 1),
|
63 |
)
|
|
|
78 |
|
79 |
# Add a Gradient Texture and set it to a color ramp of a rainbow
|
80 |
gradient = nodes.new(type="ShaderNodeTexGradient")
|
81 |
+
gradient.gradient_type = "LINEAR"
|
82 |
+
gradient.location = (0, 0)
|
83 |
|
84 |
ramp = nodes.new(type="ShaderNodeValToRGB")
|
85 |
+
ramp.color_ramp.interpolation = "LINEAR"
|
86 |
+
ramp.location = (200, 0)
|
87 |
|
88 |
ramp.color_ramp.elements[0].color = rgb1
|
89 |
ramp.color_ramp.elements[1].color = rgb2
|
90 |
|
91 |
# Add Shader nodes
|
92 |
bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
|
93 |
+
bsdf.location = (400, 0)
|
94 |
|
95 |
output = nodes.new(type="ShaderNodeOutputMaterial")
|
96 |
+
output.location = (600, 0)
|
97 |
|
98 |
# Connect the nodes
|
99 |
material.node_tree.links.new
|
|
|
102 |
material.node_tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
|
103 |
|
104 |
# Rotate the gradient to apply it from left to right
|
105 |
+
torus.rotation_euler = (
|
106 |
+
torus_X,
|
107 |
+
torus_Y,
|
108 |
+
torus_Z,
|
109 |
+
) # Rotate 90 degrees on the Z axis
|
110 |
|
111 |
# Light
|
112 |
light = bpy.data.objects["Light"]
|
|
|
114 |
|
115 |
# Camera
|
116 |
camera = bpy.data.objects["Camera"]
|
117 |
+
camera.location = (camera_X, camera_Y, camera_Z)
|
118 |
camera.data.dof.use_dof = True
|
119 |
camera.data.dof.focus_distance = 5
|
120 |
camera.data.dof.aperture_fstop = 4
|
121 |
|
122 |
# Render
|
123 |
path = "test.png"
|
124 |
+
bpy.context.scene.render.resolution_y = 256
|
125 |
+
bpy.context.scene.render.resolution_x = 256
|
126 |
bpy.context.scene.render.image_settings.file_format = "PNG"
|
127 |
bpy.context.scene.render.filepath = path
|
128 |
|
129 |
+
with tqdm(total=bpy.context.scene.frame_end) as pbar:
|
130 |
+
|
131 |
+
def elapsed(dummy):
|
132 |
+
pbar.update()
|
133 |
+
|
134 |
+
bpy.app.handlers.render_stats.append(elapsed)
|
135 |
+
bpy.ops.render.render(animation=False, write_still=True)
|
136 |
+
bpy.data.images["Render Result"].save_render(
|
137 |
+
filepath=bpy.context.scene.render.filepath
|
138 |
+
)
|
139 |
+
temp_filepath = Path(bpy.context.scene.render.filepath)
|
140 |
+
bpy.app.handlers.render_stats.clear()
|
141 |
+
return path
|
142 |
+
|
143 |
|
144 |
# generate("#ffffff", "#aaa", 1)
|
145 |
with gr.Blocks() as demo:
|
146 |
with gr.Row():
|
147 |
with gr.Column():
|
148 |
+
color1 = gr.ColorPicker(value="#59C173")
|
149 |
+
color2 = gr.ColorPicker(value="#5D26C1")
|
150 |
+
camera_X = gr.Slider(minimum=-100, maximum=100, value=5, label="Camera X")
|
151 |
+
camera_Y = gr.Slider(minimum=-100, maximum=100, value=-3, label="Camera X")
|
152 |
+
camera_Z = gr.Slider(minimum=-100, maximum=100, value=4, label="Camera X")
|
153 |
+
torus_X = gr.Slider(
|
154 |
+
minimum=-2 * pi, maximum=2 * pi, value=0, label="Torus φ"
|
155 |
+
)
|
156 |
+
torus_Y = gr.Slider(
|
157 |
+
minimum=-2 * pi, maximum=2 * pi, value=0, label="Torus θ"
|
158 |
+
)
|
159 |
+
torus_Z = gr.Slider(
|
160 |
+
minimum=-2 * pi, maximum=2 * pi, value=pi / 2, label="Torus ψ"
|
161 |
+
)
|
162 |
+
|
163 |
+
render_btn = gr.Button("Render")
|
164 |
with gr.Column(scale=3):
|
165 |
+
image = gr.Image(type="filepath")
|
166 |
+
|
167 |
+
render_btn.click(
|
168 |
+
generate,
|
169 |
+
inputs=[
|
170 |
+
color1,
|
171 |
+
color2,
|
172 |
+
camera_X,
|
173 |
+
camera_Y,
|
174 |
+
camera_Z,
|
175 |
+
torus_X,
|
176 |
+
torus_Y,
|
177 |
+
torus_Z,
|
178 |
+
],
|
179 |
+
outputs=[image],
|
180 |
+
)
|
181 |
|
182 |
|
183 |
# bpy.utils.register_class(generate)
|
184 |
demo.queue()
|
185 |
+
demo.launch(debug=True, inline=True)
|