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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
bpy.context.scene.render.resolution_x = 500
|
| 7 |
-
bpy.context.scene.render.resolution_y = 200
|
| 8 |
-
|
| 9 |
-
# Render the image and save it to a file
|
| 10 |
-
path = "test.png"
|
| 11 |
-
bpy.ops.render.render()
|
| 12 |
-
bpy.data.images["Render Result"].save_render(filepath=path)
|
| 13 |
-
|
| 14 |
-
# Function to show the rendered image
|
| 15 |
-
def show_image():
|
| 16 |
-
return path
|
| 17 |
-
|
| 18 |
-
# Create a Gradio interface to display the image
|
| 19 |
-
demo = gr.Interface(
|
| 20 |
-
fn=show_image,
|
| 21 |
-
inputs=None,
|
| 22 |
-
outputs=gr.Image()
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
# Launch the Gradio interface
|
| 26 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import base64
|
| 3 |
+
from PIL import ImageColor
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import bpy
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from math import pi
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def enable_GPUS():
|
| 12 |
+
bpy.data.scenes[0].render.engine = "CYCLES" #"CYCLES"
|
| 13 |
+
# Set the device_type
|
| 14 |
+
bpy.context.preferences.addons[
|
| 15 |
+
"cycles"
|
| 16 |
+
].preferences.compute_device_type = "METAL" # or "OPENCL"
|
| 17 |
+
|
| 18 |
+
# Set the device and feature set
|
| 19 |
+
bpy.context.scene.cycles.device = "GPU"
|
| 20 |
+
|
| 21 |
+
for scene in bpy.data.scenes:
|
| 22 |
+
scene.cycles.device = "GPU"
|
| 23 |
+
|
| 24 |
+
bpy.context.preferences.addons["cycles"].preferences.get_devices()
|
| 25 |
+
print(bpy.context.preferences.addons["cycles"].preferences.compute_device_type)
|
| 26 |
+
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
|
| 27 |
+
d["use"] = True # Using all devices, include GPU and CPU
|
| 28 |
+
print(d["name"])
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
enable_GPUS()
|
| 32 |
+
|
| 33 |
+
# bpy.ops.wm.read_factory_settings(use_empty=True)
|
| 34 |
+
|
| 35 |
+
def generate(
|
| 36 |
+
color1,
|
| 37 |
+
color2,
|
| 38 |
+
camera_X,
|
| 39 |
+
camera_Y,
|
| 40 |
+
camera_Z,
|
| 41 |
+
fov,
|
| 42 |
+
torus_X,
|
| 43 |
+
torus_Y,
|
| 44 |
+
torus_Z,
|
| 45 |
+
progress=gr.Progress(track_tqdm=True),
|
| 46 |
+
):
|
| 47 |
+
rgb1 = ImageColor.getcolor(color1, "RGBA")
|
| 48 |
+
rgb1 = tuple(v / 255.0 for v in rgb1)
|
| 49 |
+
rgb2 = ImageColor.getcolor(color2, "RGBA")
|
| 50 |
+
rgb2 = tuple(v / 255.0 for v in rgb2)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Delete all mesh objects from the scene
|
| 54 |
+
for obj in bpy.context.scene.objects:
|
| 55 |
+
# If the object is of MESH type
|
| 56 |
+
if obj.type == 'MESH':
|
| 57 |
+
# Delete the object
|
| 58 |
+
bpy.data.objects.remove(obj, do_unlink=True)
|
| 59 |
+
# Add a torus
|
| 60 |
+
bpy.ops.mesh.primitive_torus_add(
|
| 61 |
+
major_radius=1.5,
|
| 62 |
+
minor_radius=0.75,
|
| 63 |
+
major_segments=12*4,
|
| 64 |
+
minor_segments=12*4,
|
| 65 |
+
align="WORLD",
|
| 66 |
+
location=(0, 1, 1),
|
| 67 |
+
rotation=(torus_X,torus_Y,torus_Z)
|
| 68 |
+
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Assigning the torus to a variable
|
| 72 |
+
torus = bpy.context.view_layer.objects.active
|
| 73 |
+
|
| 74 |
+
# Create a new material and assign it to the torus
|
| 75 |
+
material = bpy.data.materials.new(name="RainbowGradient")
|
| 76 |
+
torus.data.materials.append(material)
|
| 77 |
+
material.use_nodes = True
|
| 78 |
+
nodes = material.node_tree.nodes
|
| 79 |
+
|
| 80 |
+
# Clear default nodes
|
| 81 |
+
for node in nodes:
|
| 82 |
+
nodes.remove(node)
|
| 83 |
+
|
| 84 |
+
# Add a Gradient Texture and set it to a color ramp of a rainbow
|
| 85 |
+
gradient = nodes.new(type="ShaderNodeTexGradient")
|
| 86 |
+
gradient.gradient_type = "LINEAR"
|
| 87 |
+
gradient.location = (0, 0)
|
| 88 |
+
|
| 89 |
+
ramp = nodes.new(type="ShaderNodeValToRGB")
|
| 90 |
+
ramp.color_ramp.interpolation = "LINEAR"
|
| 91 |
+
ramp.location = (200, 0)
|
| 92 |
+
|
| 93 |
+
ramp.color_ramp.elements[0].color = rgb1
|
| 94 |
+
ramp.color_ramp.elements[1].color = rgb2
|
| 95 |
+
|
| 96 |
+
# Add Shader nodes
|
| 97 |
+
bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
|
| 98 |
+
bsdf.location = (400, 0)
|
| 99 |
+
|
| 100 |
+
output = nodes.new(type="ShaderNodeOutputMaterial")
|
| 101 |
+
output.location = (600, 0)
|
| 102 |
+
|
| 103 |
+
# Connect the nodes
|
| 104 |
+
material.node_tree.links.new
|
| 105 |
+
material.node_tree.links.new(gradient.outputs["Color"], ramp.inputs[0])
|
| 106 |
+
material.node_tree.links.new(ramp.outputs["Color"], bsdf.inputs["Base Color"])
|
| 107 |
+
material.node_tree.links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
|
| 108 |
+
|
| 109 |
+
# Rotate the gradient to apply it from left to right
|
| 110 |
+
torus = bpy.context.view_layer.objects.active
|
| 111 |
+
# torus.rotation_euler =
|
| 112 |
+
|
| 113 |
+
# Light
|
| 114 |
+
light = bpy.data.objects["Light"]
|
| 115 |
+
light.location = (0.1, 0, 2) # Position the light
|
| 116 |
+
|
| 117 |
+
# Camera
|
| 118 |
+
camera = bpy.data.objects["Camera"]
|
| 119 |
+
camera.location = (camera_X, camera_Y, camera_Z)
|
| 120 |
+
camera.data.dof.use_dof = True
|
| 121 |
+
camera.data.dof.focus_distance = 5
|
| 122 |
+
camera.data.dof.aperture_fstop = 4
|
| 123 |
+
camera.data.angle = fov
|
| 124 |
+
camera.data.type = 'PERSP'
|
| 125 |
+
|
| 126 |
+
# Render
|
| 127 |
+
with tempfile.NamedTemporaryFile(suffix=".JPEG", delete=False) as f:
|
| 128 |
+
|
| 129 |
+
bpy.context.scene.render.resolution_y = 288
|
| 130 |
+
bpy.context.scene.render.resolution_x = 512
|
| 131 |
+
bpy.context.scene.render.image_settings.file_format = "JPEG"
|
| 132 |
+
bpy.context.scene.render.filepath = f.name
|
| 133 |
+
|
| 134 |
+
with tqdm() as pbar:
|
| 135 |
+
|
| 136 |
+
def elapsed(dummy):
|
| 137 |
+
pbar.update()
|
| 138 |
+
|
| 139 |
+
bpy.app.handlers.render_stats.append(elapsed)
|
| 140 |
+
bpy.context.scene.frame_set(1)
|
| 141 |
+
bpy.context.scene.frame_current = 1
|
| 142 |
+
|
| 143 |
+
# bpy.ops.render.render(animation=False, write_still=True)
|
| 144 |
+
# bpy.ops.render.render(animation=False, write_still=True)
|
| 145 |
+
bpy.ops.render.render(animation=False, write_still=True)
|
| 146 |
+
|
| 147 |
+
bpy.data.images["Render Result"].save_render(
|
| 148 |
+
filepath=bpy.context.scene.render.filepath
|
| 149 |
+
)
|
| 150 |
+
bpy.app.handlers.render_stats.clear()
|
| 151 |
+
return f.name
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# generate("#ffffff", "#aaa", 1)
|
| 155 |
+
with gr.Blocks() as demo:
|
| 156 |
+
gr.Markdown("""# Gradio with Blender bpy
|
| 157 |
+
based on [kolibril13](https://github.com/kolibril13/ipyblender-experimental)
|
| 158 |
+
""")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column():
|
| 161 |
+
color1 = gr.ColorPicker(value="#59C173")
|
| 162 |
+
color2 = gr.ColorPicker(value="#5D26C1")
|
| 163 |
+
torus_X = gr.Slider(minimum=-pi, maximum=pi, value=0, label="Torus φ")
|
| 164 |
+
torus_Y = gr.Slider(minimum=-pi, maximum=pi, value=-3, label="Torus θ")
|
| 165 |
+
torus_Z = gr.Slider(minimum=-pi, maximum=pi, value=1.5, label="Torus ψ")
|
| 166 |
+
fov = gr.Slider(minimum=0.0, maximum=pi, value=pi/3, label="FOV")
|
| 167 |
+
camera_X = gr.Slider(minimum=-100, maximum=100, value=5, label="Camera X")
|
| 168 |
+
camera_Y = gr.Slider(minimum=-100, maximum=100, value=-3, label="Camera Y")
|
| 169 |
+
camera_Z = gr.Slider(minimum=-100, maximum=100, value=4, label="Camera Z")
|
| 170 |
+
|
| 171 |
+
render_btn = gr.Button("Render")
|
| 172 |
+
with gr.Column(scale=3):
|
| 173 |
+
image = gr.Image(type="filepath")
|
| 174 |
+
|
| 175 |
+
render_btn.click(
|
| 176 |
+
generate,
|
| 177 |
+
inputs=[
|
| 178 |
+
color1,
|
| 179 |
+
color2,
|
| 180 |
+
camera_X,
|
| 181 |
+
camera_Y,
|
| 182 |
+
camera_Z,
|
| 183 |
+
fov,
|
| 184 |
+
torus_X,
|
| 185 |
+
torus_Y,
|
| 186 |
+
torus_Z,
|
| 187 |
+
],
|
| 188 |
+
outputs=[image],
|
| 189 |
+
)
|
| 190 |
|
| 191 |
+
demo.queue(concurrency_count=1)
|
| 192 |
+
demo.launch(debug=True, inline=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|