Files
workinf_Blender_Wasm/tools/web/generate-attribute-fixture.py
2026-08-12 04:47:48 -04:00

93 lines
3.1 KiB
Python

import sys
import bpy
def main(output_path: str) -> None:
bpy.ops.wm.read_factory_settings(use_empty=True)
vertices = [
(-1.0, -1.0, 0.0),
(1.0, -1.0, 0.0),
(1.0, 1.0, 0.0),
(-1.0, 1.0, 0.0),
(0.0, 0.0, 1.0),
]
faces = [(0, 1, 2, 3), (0, 3, 4)]
mesh = bpy.data.meshes.new("AttributeMesh")
mesh.from_pydata(vertices, [], faces)
mesh.update()
uv_layer = mesh.uv_layers.new(name="UVMap")
uv_values = [
(0.0, 0.0),
(1.0, 0.0),
(1.0, 1.0),
(0.0, 1.0),
(0.0, 1.0),
(0.5, 0.0),
(1.0, 1.0),
]
for loop, uv in zip(uv_layer.data, uv_values):
loop.uv = uv
color_layer = mesh.color_attributes.new(name="CornerColor", type="FLOAT_COLOR", domain="CORNER")
colors = [
(1.0, 0.0, 0.0, 1.0),
(0.0, 1.0, 0.0, 1.0),
(0.0, 0.0, 1.0, 1.0),
(1.0, 1.0, 0.0, 1.0),
(1.0, 0.0, 1.0, 1.0),
(0.0, 1.0, 1.0, 1.0),
(1.0, 1.0, 1.0, 1.0),
]
for color, value in zip(color_layer.data, colors):
color.color = value
first = bpy.data.materials.new("AttributeRed")
first.diffuse_color = (1.0, 0.1, 0.1, 1.0)
first.use_nodes = True
principled = first.node_tree.nodes.get("Principled BSDF")
if principled:
principled.inputs["Base Color"].default_value = (0.8, 0.05, 0.02, 1.0)
principled.inputs["Metallic"].default_value = 0.25
principled.inputs["Roughness"].default_value = 0.35
if principled.inputs.get("IOR"):
principled.inputs["IOR"].default_value = 1.33
if principled.inputs.get("Alpha"):
principled.inputs["Alpha"].default_value = 0.8
if principled.inputs.get("Emission Color"):
principled.inputs["Emission Color"].default_value = (0.02, 0.01, 0.0, 1.0)
elif principled.inputs.get("Emission"):
principled.inputs["Emission"].default_value = (0.02, 0.01, 0.0, 1.0)
image = bpy.data.images.new("AttributeTexture", width=2, height=2)
image.source = "FILE"
image.filepath = "textures/attribute.png"
texture = first.node_tree.nodes.new("ShaderNodeTexImage")
texture.image = image
first.node_tree.links.new(texture.outputs["Color"], principled.inputs["Base Color"])
second = bpy.data.materials.new("AttributeBlue")
second.diffuse_color = (0.1, 0.1, 1.0, 1.0)
mesh.materials.append(first)
mesh.materials.append(second)
mesh.polygons[0].material_index = 0
mesh.polygons[1].material_index = 1
obj = bpy.data.objects.new("AttributeMeshObject", mesh)
bpy.context.collection.objects.link(obj)
obj.scale = (-1.0, 1.0, 1.0)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
scene = bpy.context.scene
scene.frame_start = 1
scene.frame_end = 24
scene.render.fps = 24
bpy.ops.wm.save_as_mainfile(filepath=output_path, compress=True)
if __name__ == "__main__":
if "--" not in sys.argv or len(sys.argv) <= sys.argv.index("--") + 1:
raise SystemExit("usage: blender -b --python generate-attribute-fixture.py -- output.blend")
main(sys.argv[sys.argv.index("--") + 1])