Smiley Blender
Smiley sur Blender


import bpy import json import math
def create_smiley(config): """ Create a smiley in Blender based on a JSON configuration.
:param config: JSON configuration string.
"""
# Parse JSON data
smiley_data = json.loads(config)["smiley"]
# Create the base (face)
base = smiley_data["base"]
bpy.ops.mesh.primitive_uv_sphere_add(radius=base["radius"], location=base["position"])
smiley_base = bpy.context.object
mat_base = bpy.data.materials.new(name="SmileyBaseMaterial")
mat_base.use_nodes = True
bsdf = mat_base.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Base Color"].default_value = (*base["color"], 1.0)
smiley_base.data.materials.append(mat_base)
# Add the eyes
for eye in smiley_data["eyes"]:
if eye["type"] == "sphere":
# Create spherical eyes
bpy.ops.mesh.primitive_uv_sphere_add(radius=eye["radius"], location=eye["position"])
eye_obj = bpy.context.object
mat_eye = bpy.data.materials.new(name="EyeMaterial")
mat_eye.use_nodes = True
bsdf_eye = mat_eye.node_tree.nodes["Principled BSDF"]
bsdf_eye.inputs["Base Color"].default_value = (*eye["color"], 1.0)
eye_obj.data.materials.append(mat_eye)
elif eye["type"] == "curve":
# Create curved eyes
curve_data = bpy.data.curves.new(name="EyeCurve", type='CURVE')
curve_data.dimensions = '3D'
curve_data.resolution_u = 64
polyline = curve_data.splines.new('POLY')
polyline.points.add(len(eye["points"]) - 1)
for i, point in enumerate(eye["points"]):
x, y, z = point
polyline.points[i].co = (x, y, z, 1) # Add weight (w)
curve_obj = bpy.data.objects.new("EyeCurve", curve_data)
bpy.context.collection.objects.link(curve_obj)
curve_obj.data.bevel_depth = eye["thickness"]
curve_obj.data.bevel_resolution = 8
mat_eye = bpy.data.materials.new(name="EyeCurveMaterial")
mat_eye.use_nodes = True
bsdf_eye = mat_eye.node_tree.nodes["Principled BSDF"]
bsdf_eye.inputs["Base Color"].default_value = (*eye["color"], 1.0)
curve_obj.data.materials.append(mat_eye)
# Add the mouth
mouth = smiley_data["mouth"]
curve_data = bpy.data.curves.new(name="MouthCurve", type='CURVE')
curve_data.dimensions = '3D'
curve_data.resolution_u = 64
polyline = curve_data.splines.new('POLY')
polyline.points.add(len(mouth["points"]) - 1)
for i, point in enumerate(mouth["points"]):
x, y, z = point
polyline.points[i].co = (x, y, z, 1) # Add weight (w)
curve_obj = bpy.data.objects.new("Mouth", curve_data)
bpy.context.collection.objects.link(curve_obj)
curve_obj.data.bevel_depth = mouth["thickness"]
curve_obj.data.bevel_resolution = 8
mat_mouth = bpy.data.materials.new(name="MouthMaterial")
mat_mouth.use_nodes = True
bsdf_mouth = mat_mouth.node_tree.nodes["Principled BSDF"]
bsdf_mouth.inputs["Base Color"].default_value = (*mouth["color"], 1.0)
curve_obj.data.materials.append(mat_mouth)
Example usage with a combined configuration
template = """ { "smiley": { "base": { "type": "sphere", "radius": 1.0, "position": [0, 0, 0], "color": [1.0, 1.0, 0.0] }, "eyes": [ { "type": "curve", "points": [ [-0.3, 0.5, 0.9], [-0.2, 0.6, 0.9], [-0.1, 0.5, 0.9] ], "thickness": 0.02, "color": [0.0, 0.0, 0.0] }, { "type": "sphere", "radius": 0.1, "position": [0.3, 0.5, 0.8], "color": [0.0, 0.0, 0.0] } ], "mouth": { "type": "curve", "points": [ [-0.5, -0.15, 0.9], [-0.25, -0.25, 0.95], [0.0, -0.3, 1.0], [0.25, -0.25, 0.95], [0.5, -0.15, 0.9] ], "thickness": 0.05, "color": [0, 0, 0] } } } """
create_smiley(template)