Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,46 @@ import base64
|
|
| 3 |
from PIL import ImageColor
|
| 4 |
from pathlib import Path
|
| 5 |
import bpy
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 13 |
-
|
| 14 |
|
| 15 |
light_position_normed = light_position / 20
|
| 16 |
# Delete all mesh objects from the scene
|
| 17 |
-
bpy.ops.object.select_all(action=
|
| 18 |
-
bpy.ops.object.select_by_type(type=
|
| 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 |
)
|
|
@@ -44,22 +63,22 @@ def generate(color1, color2, light_position):
|
|
| 44 |
|
| 45 |
# Add a Gradient Texture and set it to a color ramp of a rainbow
|
| 46 |
gradient = nodes.new(type="ShaderNodeTexGradient")
|
| 47 |
-
gradient.gradient_type =
|
| 48 |
-
gradient.location = (0,0)
|
| 49 |
|
| 50 |
ramp = nodes.new(type="ShaderNodeValToRGB")
|
| 51 |
-
ramp.color_ramp.interpolation =
|
| 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")
|
| 59 |
-
bsdf.location = (400,0)
|
| 60 |
|
| 61 |
output = nodes.new(type="ShaderNodeOutputMaterial")
|
| 62 |
-
output.location = (600,0)
|
| 63 |
|
| 64 |
# Connect the nodes
|
| 65 |
material.node_tree.links.new
|
|
@@ -83,34 +102,48 @@ def generate(color1, color2, light_position):
|
|
| 83 |
|
| 84 |
# Render
|
| 85 |
path = "test.png"
|
| 86 |
-
bpy.context.scene.render.
|
| 87 |
-
bpy.context.scene.render.
|
| 88 |
bpy.context.scene.render.image_settings.file_format = "PNG"
|
| 89 |
bpy.context.scene.render.filepath = path
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
return path
|
| 101 |
|
|
|
|
| 102 |
# generate("#ffffff", "#aaa", 1)
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column():
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
with gr.Column(scale=3):
|
| 110 |
-
|
| 111 |
|
| 112 |
-
slider.change(generate, inputs=[color1, color2, slider], outputs=[image]
|
| 113 |
|
| 114 |
|
| 115 |
# bpy.utils.register_class(generate)
|
| 116 |
-
demo.
|
|
|
|
|
|
| 3 |
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 |
+
# 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 |
|
| 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 |
|
| 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("INVOKE_DEFAULT", 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)
|