Skip to content

Latest commit

 

History

History
5231 lines (4325 loc) · 170 KB

File metadata and controls

5231 lines (4325 loc) · 170 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 4 column 1
---
show: step
version: 1.0 
enable_checker: true
本教程同步发布在: 

     个人网站: `https://oeasy.org` 
     蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
     GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
     Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

  • oeasy Python 0706
  • 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。

角色_人物_动物

开始

  • 上次将 静态渲染 一条龙
    1. 建模
    2. 材质
    3. 灯光
    4. 渲染
    5. 封装

图片描述

  • 用 blender 还能做点啥好玩的?🤔

机器人

图片描述

import bpy
from math import pi
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
collection = bpy.data.collections["Collection"]
robot = bpy.data.objects.new("robot", None)
collection.objects.link(robot)
bpy.ops.mesh.primitive_cube_add(size=1.5, location=(0, 0, 4))
bpy.context.object.parent = robot
bpy.context.object.name = "head"
yellow_material = bpy.data.materials.new('YellowMaterial')
color = (1, 1, 0, 1)
yellow_material.use_nodes = True
yellow_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(yellow_material)
bpy.context.object.data.materials.append(yellow_material)
bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 2.5))
bpy.context.object.parent = robot
bpy.context.object.name = "body"
green_material = bpy.data.materials.new('GreenMaterial')
color = (0, 1, 0, 1)  
green_material.use_nodes = True
green_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(green_material)
bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=1.5, location=(-1.75, 0, 2.5))
bpy.context.object.rotation_euler[1] = pi / 2
bpy.context.object.parent = robot
bpy.context.object.name = "left_arm"
red_material = bpy.data.materials.new('RedMaterial')
color = (1, 0, 0, 1) 
red_material.use_nodes = True
red_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(red_material)
bpy.context.object.data.materials.append(red_material)
bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=1.5, location=(1.75, 0, 2.5))
bpy.context.object.rotation_euler[1] = pi / 2
bpy.context.object.parent = robot
bpy.context.object.name = "right_arm"
red_material = bpy.data.materials.new('RedMaterial')
bpy.context.object.data.materials.append(red_material)
bpy.ops.mesh.primitive_cylinder_add(radius=0.4, depth=2, location=(-0.5, 0, 0.5))
bpy.context.object.data.materials.append(red_material)
bpy.context.object.parent = robot
bpy.context.object.name = "left_leg"
bpy.context.object.data.materials.append(yellow_material)
bpy.ops.mesh.primitive_cylinder_add(radius=0.4, depth=2, location=(0.5, 0, 0.5))
bpy.context.object.parent = robot
bpy.context.object.name = "right_leg"
bpy.context.object.data.materials.append(yellow_material)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, location=(-0.5, 0.75, 4.25))
bpy.context.object.parent = robot
bpy.context.object.name = "left_eye"
black_material = bpy.data.materials.new('BlackMaterial')
black_material.use_nodes = True
color = (0, 0, 0, 1)
black_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(black_material)
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, location=(0.5, 0.75, 4.25))
bpy.context.object.parent = robot
bpy.context.object.name = "right_eye"
color = (0, 0, 0, 1) 
black_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(black_material)
camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera_obj.location = (-12.36, 13.48, 9.76)
camera_obj.rotation_euler = (69 * pi / 180, 0, -133 * pi / 180)
bpy.ops.mesh.primitive_plane_add(size=50, location=(0, 0, 0))
bpy.context.scene.camera = camera_obj
bpy.context.scene.render.filepath = '/tmp/rendered_robot.png'
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.ops.object.light_add(type='SPOT', radius=10, location=(-16.02, 10, 10))
spot_light = bpy.context.object
spot_light.data.energy = 8000
spot_light.data.spot_size = pi / 4 
spot_light.data.spot_blend = 0.5  
spot_light.rotation_euler = (-5*pi / 180, -58*pi / 180, -27*pi / 180)
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50
bpy.ops.render.render(write_still=True)

甜甜圈

图片描述

import bpy
import math
from mathutils import Color


bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()


bpy.ops.mesh.primitive_torus_add(
    align='WORLD', 
    location=(0, 0, 0), 
    rotation=(2, 0, 0), 
    major_radius=2, 
    minor_radius=1, 
    abso_major_rad=1.25,
    abso_minor_rad=0.75
)
torus = bpy.context.object
torus.name = "身体"
torus.rotation_euler = (math.pi / 2, 0, 0)

bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.3, 
    location=(-0.8, -1, 1.5)  
)
sphere = bpy.context.object
sphere.name = "左眼"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(-1.3, -1, 1.8))  
bpy.ops.object.mode_set(mode='OBJECT')

bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.3,  
    location=(0.8, -1, 1.5)  
)

sphere = bpy.context.object
sphere.name = "右眼"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(1.3, -1, 1.8))  
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.15,  
    location=(-0.8, -1.2, 1.5)  
)


sphere = bpy.context.object
sphere.name = "左眼珠"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(-1.3, -1.2, 1.8)) 
bpy.ops.object.mode_set(mode='OBJECT')


bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.15, 
    location=(0.8, -1.2, 1.5) 
)
sphere = bpy.context.object
sphere.name = "右眼珠"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(1.3, -1.2, 1.8)) 
bpy.ops.object.mode_set(mode='OBJECT')


bpy.ops.mesh.primitive_torus_add(
    align='WORLD', 
    location=(0, -1, -1.5), 
    rotation=(0, 0, 0), 
    major_radius=0.5, 
    minor_radius=0.25, 
    abso_major_rad=1.25,
    abso_minor_rad=0.75
)
torus = bpy.context.object
torus.name = "嘴"
torus.rotation_euler = (math.pi / 2, 0, 0)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(1, -1, 1.2)) 
bpy.ops.object.mode_set(mode='OBJECT')


bpy.ops.mesh.primitive_torus_add(
    align='WORLD', 
    location=(-3, -0.3, 0), 
    rotation=(0, 0, 0), 
    major_radius=0.8, 
    minor_radius=0.3, 
    abso_major_rad=1.25,
    abso_minor_rad=0.75
)
torus = bpy.context.object
torus.name = "左胳膊"
torus.rotation_euler = (math.pi / 2, 0, 0)


bpy.ops.mesh.primitive_torus_add(
    align='WORLD', 
    location=(3, -0.3, 0), 
    rotation=(0, 0, 0), 
    major_radius=0.8, 
    minor_radius=0.3, 
    abso_major_rad=1.25,
    abso_minor_rad=0.75
)
torus = bpy.context.object
torus.name = "右胳膊"
torus.rotation_euler = (math.pi / 2, 0, 0)


bpy.ops.mesh.primitive_cylinder_add(
    radius=0.2, 
    depth=2, 
    enter_editmode=False, 
    align='WORLD', 
    location=(-1, -0.3, -3), 
    scale=(1, 1, 1)
)
torus = bpy.context.object
torus.name = "左腿"


bpy.ops.mesh.primitive_cylinder_add(
    radius=0.2, 
    depth=2, 
    enter_editmode=False, 
    align='WORLD', 
    location=(1, -0.3, -3), 
    scale=(1, 1, 1)
)
torus = bpy.context.object
torus.name = "右腿"


bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.4,  
    location=(-1, -0.15, -4)  
)
sphere = bpy.context.object
sphere.name = "左脚"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(-3, -1, -1)) 
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.transform.translate(value=(-0.4, -0.15, 0))
bpy.ops.transform.resize(value=(0.7, 1, 1))


bpy.ops.mesh.primitive_uv_sphere_add(
    radius=0.4, 
    location=(1, -0.15, -4) 
)
sphere = bpy.context.object
sphere.name = "右脚"
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(3, -1, -1))
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.transform.translate(value=(0.4, -0.15, 0))
bpy.ops.transform.resize(value=(0.7, 1, 1))  

bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.shade_smooth()

bpy.data.objects['身体'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['身体']
mat = bpy.data.materials.new('mat_eye')
mat.use_nodes = True
color = (0.013,0.718,0.290,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['左眼珠'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['左眼珠']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['右眼珠'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['右眼珠']
bpy.context.object.data.materials.append(mat)

bpy.data.objects['嘴'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['嘴']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (0.801,0.034,0.060,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['左胳膊'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['左胳膊']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (1,0.894,0,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['右胳膊'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['右胳膊']
bpy.context.object.data.materials.append(mat)

bpy.data.objects['左腿'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['左腿']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (1,0.912,0.522,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['右腿'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['右腿']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (1,0.912,0.522,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['左脚'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['左脚']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (0,0.045,0.707,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.data.objects['右脚'].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['右脚']
mat = bpy.data.materials.new('mat_eye')
mat.use_node = True
color = (0,0.045,0.707,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 50  
camera.sensor_width = 36  
camera.sensor_height = 24  
camera_obj.location = (1, -25, 7.5)  
camera_obj.rotation_euler = (-5, 0, 0)


bpy.ops.object.light_add(type='SPOT', radius=1)
bpy.context.object.data.energy = 10000
bpy.context.object.location = (18, -15, 10)
bpy.context.object.rotation_euler = (1.172,0,0.907)
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50
#渲染?
bpy.context.scene.camera = camera_obj

# Set the render engine (e.g., CYCLES, BLENDER_EEVEE)
bpy.context.scene.render.engine = 'CYCLES'
# Set the output file path
bpy.context.scene.render.filepath = '/tmp/render2.png'
# Render the current view
bpy.ops.render.render(write_still=True)

性格塑造

图片描述

import bpy

def clear_scene():
    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete()

def create_man():
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    bpy.context.object.name  = "head"
    mat_head = bpy.data.materials.new('mat_head')
    mat_head.use_nodes = True
    color_head = (0.8, 0.6, 0.4, 1)
    mat_head.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color_head
    bpy.context.object.data.materials.append(mat_head)


    r_eye = bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "r_eye"
    bpy.context.object.location = (0.8, 0.5, 0)
    bpy.context.object.scale = (0.1, 0.3, 0.3)
    mat = bpy.data.materials.new('mat_eye')
    mat.use_nodes = True
    color = (1, 1, 1, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = head

    bpy.ops.mesh.primitive_uv_sphere_add()
    r_pupil = bpy.context.object
    r_pupil.name = "r_pupil"
    r_pupil.location = (0.9, 0.6, 0)
    r_pupil.scale = (0.1, 0.2, 0.15)  
    mat_pupil = bpy.data.materials.new('mat_pupil')
    pupil_color = (0, 0, 0, 1) 
    mat_pupil.diffuse_color = pupil_color
    r_pupil.data.materials.append(mat_pupil)
    r_pupil.parent = head


    l_eye = bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "l_eye"
    bpy.context.object.location = (0.8, -0.5, 0.1)
    bpy.context.object.scale = (0.1, 0.3, 0.3)
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = head

    bpy.ops.mesh.primitive_uv_sphere_add()
    l_pupil = bpy.context.object
    l_pupil.name = "l_pupil"
    l_pupil.location = (0.9, -0.6, 0.1)
    l_pupil.scale = (0.1, 0.2, 0.15)
    l_pupil.data.materials.append(mat_pupil) 
    l_pupil.parent = head


    bpy.ops.mesh.primitive_cube_add()
    body = bpy.context.object
    body.name  = "body"
    body.location = (0,0,-2.5)
    body.scale = (0.5, 0.7, 1.3)
    mat_body = bpy.data.materials.new('mat_body')
    mat_body.use_nodes = True
    color_body = (0, 0, 0, 1)
    mat_body.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color_body
    bpy.context.object.data.materials.append(mat_body)
    bpy.context.object.parent = head


    bpy.ops.mesh.primitive_cylinder_add()
    neck = bpy.context.object
    neck.name  = "neck"
    neck.location = (0,0,-1)
    neck.scale = (0.3, 0.3, 1)
    mat_neck = bpy.data.materials.new('mat_neck')
    mat_neck.use_nodes = True
    color_neck = (0.8, 0.6, 0.4, 1)
    mat_neck.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color_neck
    bpy.context.object.data.materials.append(mat_neck)
    bpy.context.object.parent = head


    bpy.ops.mesh.primitive_cylinder_add()
    right_arm = bpy.context.object
    right_arm.name = "right_arm"
    right_arm.location = (0.5, 1.2, 0.1)
    right_arm.scale = (0.3, 0.3, 0.9)
    mat_arm = bpy.data.materials.new('mat_arm')
    arm_color = (0.8, 0.6, 0.4, 1) 
    mat_arm.diffuse_color = arm_color
    right_arm.data.materials.append(mat_arm)
    right_arm.parent = body


    bpy.ops.mesh.primitive_cylinder_add()
    left_arm = bpy.context.object
    left_arm.name = "left_arm"
    left_arm.location = (-0.5, -1.2, 0.1)
    left_arm.scale = (0.3, 0.3, 0.9)
    left_arm.data.materials.append(mat_arm)
    left_arm.parent = body


    bpy.ops.mesh.primitive_cylinder_add()
    right_leg = bpy.context.object
    right_leg.name = "right_leg"
    right_leg.location = (0.2, 0.5, -1.5)
    right_leg.scale = (0.3, 0.3, 1.6)
    right_leg.data.materials.append(mat_arm)
    right_leg.parent = body


    bpy.ops.mesh.primitive_cylinder_add()
    left_leg = bpy.context.object
    left_leg.name = "left_leg"
    left_leg.location = (-0.2, -0.5, -1.5)
    left_leg.scale = (0.3, 0.3, 1.6)
    left_leg.data.materials.append(mat_arm)
    left_leg.parent = body


    bpy.ops.mesh.primitive_cube_add()
    right_eyebrow = bpy.context.object
    right_eyebrow.name = "right_eyebrow"
    right_eyebrow.location = (0.6, 0.5, 0.6) 
    right_eyebrow.scale = (0.1, 0.2, 0.1)  
    mat_eyebrow = bpy.data.materials.new('mat_eyebrow')
    eyebrow_color = (0.0, 0.0, 0.0, 0.9)  
    mat_eyebrow.diffuse_color = eyebrow_color
    right_eyebrow.data.materials.append(mat_eyebrow)  
    right_eyebrow.parent = head


    bpy.ops.mesh.primitive_cube_add()
    left_eyebrow = bpy.context.object
    left_eyebrow.name = "left_eyebrow"
    left_eyebrow.location = (0.6, -0.5, 0.5) 
    left_eyebrow.scale = (0.1, 0.2, 0.1)  
    left_eyebrow.data.materials.append(mat_eyebrow) 
    left_eyebrow.parent = head


    bpy.ops.mesh.primitive_cube_add()
    mouth = bpy.context.object
    mouth.name = "mouth"
    mouth.location = (0.8, 0, -0.3)  
    mouth.scale = (0.2, 0.3, 0.1) 
    mat_mouth = bpy.data.materials.new('mat_mouth')
    mouth_color = (0.8, 0.0, 0.0, 1) 
    mat_mouth.diffuse_color = mouth_color
    mouth.data.materials.append(mat_mouth)
    mouth.parent = head  


    bpy.ops.mesh.primitive_cube_add()
    saliva = bpy.context.object
    saliva.name = "saliva"
    saliva.location = (0.9, 0.2, -0.8)  
    saliva.scale = (0.02, 0.06, 0.5) 
    mat_saliva = bpy.data.materials.new('mat_saliva')
    saliva_color = (0.0, 0.23, 0.6, 0.6) 
    mat_saliva.diffuse_color = saliva_color
    saliva.data.materials.append(mat_saliva)
    saliva.parent = head

    bpy.ops.mesh.primitive_cube_add()
    mohawk = bpy.context.object
    mohawk.name = "mohawk"
    mohawk.location = (-0.2, 0, 0.8)
    mohawk.scale = (0.6, 0.3, 0.6)  
    mat_hair = bpy.data.materials.new('mat_hair')
    hair_color = (0.3, 0.2, 0.1, 1) 
    mat_hair.diffuse_color = hair_color
    mohawk.data.materials.append(mat_hair)
    mohawk.parent = head    


    bpy.ops.mesh.primitive_cube_add()
    right_shoe = bpy.context.object
    right_shoe.name = "right_shoe"
    right_shoe.location = (1, 0.2, -1)  
    right_shoe.scale = (3, 1, 0.07)  
    mat_shoe = bpy.data.materials.new('mat_shoe')
    shoe_color = (0.8, 5, 0.01, 1)  
    mat_shoe.diffuse_color = shoe_color
    right_shoe.data.materials.append(mat_shoe)
    right_shoe.parent = right_leg  


    bpy.ops.mesh.primitive_cube_add()
    left_shoe = bpy.context.object
    left_shoe.name = "left_shoe"
    left_shoe.location = (1, 0.2, -1)  
    left_shoe.scale = (3, 1, 0.07)
    left_shoe.data.materials.append(mat_shoe) 
    left_shoe.parent = left_leg  

    bpy.ops.mesh.primitive_cube_add()
    collar = bpy.context.object
    collar.name = "collar"
    collar.location = (0.001, 0.001, 0.8) 
    collar.scale = (1, 0.6, 0.2)
    mat_collar = bpy.data.materials.new('mat_collar')
    collar_color = (0.8, 0.6, 0.4, 1)  
    mat_collar.diffuse_color = collar_color
    collar.data.materials.append(mat_collar)
    collar.parent = body

    bpy.ops.mesh.primitive_cube_add()
    shorts = bpy.context.object
    shorts.name = "shorts"
    shorts.location = (0, 0, -1) 
    shorts.scale = (0.8, 0.9, 0.5)  
    mat_shorts = bpy.data.materials.new('mat_shorts')
    shorts_color = (0.5, 0.0, 0.0, 1)  
    mat_shorts.diffuse_color = shorts_color
    shorts.data.materials.append(mat_shorts)
    shorts.parent = body  

    character = bpy.data.objects.new("character", None)
    bpy.data.collections["Collection"].objects.link(character)
    head.parent = character
    body.parent = character

clear_scene()
create_man()

随机颜色的小人

图片描述

import bpy
import random

# 清除现有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# 创建 pop_Mart 对象
pop_Mart = bpy.data.objects.new("pop_Mart", None)
bpy.data.collections["Collection"].objects.link(pop_Mart)

# 创建头部
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, location=(0, 0, 1.5))
head = bpy.context.object
head.parent = pop_Mart
head.name = "Head"

# 创建身体
bpy.ops.mesh.primitive_cylinder_add(radius=0.75, depth=1.5, location=(0, 0, 0.5))
body = bpy.context.object
body.parent = pop_Mart
body.name = "Body"

# 创建左眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.15, location=(-0.35, 0.9, 1.75))
left_eye = bpy.context.object
left_eye.parent = pop_Mart
left_eye.name = "left_eye"

# 创建右眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.15, location=(0.35, 0.9, 1.75))
right_eye = bpy.context.object
right_eye.parent = pop_Mart
right_eye.name = "right_eye"

# 创建左臂
bpy.ops.mesh.primitive_cylinder_add(radius=0.15, depth=1.0, location=(-1.0, 0, 0.75))
left_arm = bpy.context.object
left_arm.parent = pop_Mart
left_arm.name = "left_arm"

# 创建右臂
bpy.ops.mesh.primitive_cylinder_add(radius=0.15, depth=1.0, location=(1.0, 0, 0.75))
right_arm = bpy.context.object
right_arm.parent = pop_Mart
right_arm.name = "right_arm"

# 创建左腿
bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=1.0, location=(-0.4, 0, -0.5))
left_leg = bpy.context.object
left_leg.parent = pop_Mart
left_leg.name = "left_leg"

# 创建右腿
bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=1.0, location=(0.4, 0, -0.5))
right_leg = bpy.context.object
right_leg.parent = pop_Mart
right_leg.name = "right_leg"

# 函数:为对象创建材质并应用随机颜色
def create_random_material(obj):
    mat = bpy.data.materials.new(name=obj.name + "_Material")
    red = random.random()
    green = random.random()
    blue = random.random()
    alpha = 1.0
    mat.diffuse_color = (red, green, blue, alpha)
    obj.data.materials.append(mat)

# 应用随机材质和颜色
create_random_material(head)
create_random_material(body)
create_random_material(left_eye)
create_random_material(right_eye)
create_random_material(left_arm)
create_random_material(right_arm)
create_random_material(left_leg)
create_random_material(right_leg)

# 创建和设置摄影机
camera = bpy.data.cameras.new('Camera')
camera_obj = bpy.data.objects.new('Camera', camera)
bpy.data.collections["Collection"].objects.link(camera_obj)
camera.lens = 50  # Focal length in millimeters
camera.sensor_width = 36  # Sensor width in millimeters
camera.sensor_height = 24  # Sensor height in millimeters
camera_obj.location = (0, 10, 3)  # 摄影机放置在模型的正前方
camera_obj.rotation_euler = (1.36, 0, 3.14)  # 调整角度正对模型
bpy.context.scene.camera = camera_obj

# 创建和设置灯光
light_data = bpy.data.lights.new(name="Light", type='POINT')
light = bpy.data.objects.new(name="Light", object_data=light_data)
bpy.context.collection.objects.link(light)
light.location = (5, 5, 5)
light.data.energy = 1000  # 调整灯光强度

# 添加地面
bpy.ops.mesh.primitive_plane_add(size=20)
bpy.context.object.location = (0, 0, -1)

# 设置渲染参数
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.engine = 'CYCLES'  # 使用 Cycles 渲染引擎
bpy.context.scene.render.filepath = "/tmp/r.png"  # 输出文件路径

# 渲染当前视图
bpy.ops.render.render(write_still=True)

盛装方脸

图片描述

import bpy
from math import pi
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
collection = bpy.data.collections["Collection"]
robot = bpy.data.objects.new("robot", None)
collection.objects.link(robot)

bpy.ops.mesh.primitive_cube_add(size=1.5, location=(0, 0, 5))
bpy.context.object.parent = robot
bpy.context.object.name = "head"
skin_material = bpy.data.materials.new('SkinMaterial')
skin_material.use_nodes = True
color = (1, 0.7, 0.7, 1)
skin_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(skin_material)

bpy.ops.mesh.primitive_cone_add(location = (0,0,7.8))
bpy.context.object.scale = (0.8, 0.8, 2)
bpy.context.object.parent = robot
bpy.context.object.name = "hat1"   
hat1_material = bpy.data.materials.new('Hat1Material')    
hat1_material.use_nodes = True
color = (1, 0.7, 0.2, 1)
hat1_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(hat1_material)

bpy.ops.mesh.primitive_torus_add(location=(0,0,6))
bpy.context.object.parent = robot
bpy.context.object.name = "hat2"   

bpy.ops.mesh.primitive_uv_sphere_add(location = (0,0,7.5))
bpy.context.object.scale = (0.5, 0.5, 0.5)
bpy.context.object.parent = robot
bpy.context.object.name = "hat3"
hat3_material = bpy.data.materials.new('Hat3Material')    
hat3_material.use_nodes = True
color = (0.5, 0, 0.2, 1)
hat3_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(hat3_material)

bpy.ops.mesh.primitive_uv_sphere_add(location = (0,0,9.5))
bpy.context.object.scale = (0.5, 0.5, 0.5)
bpy.context.object.parent = robot
bpy.context.object.name = "hat4"
hat4_material = bpy.data.materials.new('Hat4Material')    
hat4_material.use_nodes = True
color = (0.5, 0, 0.2, 1)
hat4_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(hat4_material)

bpy.ops.mesh.primitive_cone_add(location = (0,0,3.5))
bpy.context.object.scale = (2, 2, 2)
bpy.context.object.parent = robot
bpy.context.object.name = "body"
pink_material = bpy.data.materials.new('PinkMaterial')
pink_material.use_nodes = True
color = (1, 0.5, 1, 1)
pink_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(pink_material)

bpy.ops.mesh.primitive_torus_add(location=(0,0,3))
bpy.context.object.parent = robot
bpy.context.object.name = "decorate1"  
dec1_material = bpy.data.materials.new('Dec1Material')
dec1_material.use_nodes = True
color = (0, 0.2, 1, 1)
dec1_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(dec1_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (-1,1,2.5))
bpy.context.object.scale = (0.35, 0.35, 0.35)
bpy.context.object.parent = robot
bpy.context.object.name = "decorate2"
dec2_material = bpy.data.materials.new('Dec2Material')
dec2_material.use_nodes = True
color = (0.2, 0, 0.7, 1)
dec2_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(dec2_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (1,1,2.5))
bpy.context.object.scale = (0.35, 0.35, 0.35)
bpy.context.object.parent = robot
bpy.context.object.name = "decorate3"
dec3_material = bpy.data.materials.new('Dec3Material')
dec3_material.use_nodes = True
color = (0, 1, 0.5, 1)
dec3_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(dec3_material)

bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=1.5, location=(-1.35, 0, 3.5))
bpy.context.object.rotation_euler[1] = pi / 6
bpy.context.object.parent = robot
bpy.context.object.name = "left_arm"
larm_material = bpy.data.materials.new('LarmMaterial')
larm_material.use_nodes = True
color = (1, 0.3, 0.2, 1) 
larm_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(larm_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (-1.35, 0, 3.5))
bpy.context.object.scale = (0.35, 0.35, 0.35)
bpy.context.object.parent = robot

bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=1.5, location=(1.75, 0, 3.75))
bpy.context.object.rotation_euler[1] = pi / 2
bpy.context.object.parent = robot
bpy.context.object.name = "right_arm"
rarm_material = bpy.data.materials.new('RarmMaterial')
rarm_material.use_nodes = True
color = (1, 0.3, 0.2, 1) 
rarm_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(rarm_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (1.75, 0, 3.75))
bpy.context.object.scale = (0.35, 0.35, 0.35)
bpy.context.object.parent = robot

bpy.ops.mesh.primitive_cylinder_add(radius=0.4, depth=2, location=(-0.5, 0, 1.5))
bpy.context.object.parent = robot
bpy.context.object.name = "left_leg"
yellow_material = bpy.data.materials.new('YellowMaterial')
yellow_material.use_nodes = True
color = (1, 0.5, 0.2, 1) 
yellow_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(yellow_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (-0.5, 0, 0.5))
bpy.context.object.scale = (0.45, 0.45, 0.45)
bpy.context.object.parent = robot

bpy.ops.mesh.primitive_cylinder_add(radius=0.4, depth=2, location=(0.5, 0, 1.5))
bpy.context.object.parent = robot
bpy.context.object.name = "right_leg"
yellow_material = bpy.data.materials.new('YellowMaterial')
yellow_material.use_nodes = True
color = (1, 1, 0, 1)
yellow_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(yellow_material)
bpy.ops.mesh.primitive_uv_sphere_add(location = (0.5, 0, 0.5))
bpy.context.object.scale = (0.45, 0.45, 0.45)
bpy.context.object.parent = robot

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, location=(-0.5, 0.75, 5.25))
bpy.context.object.parent = robot
bpy.context.object.name = "left_eye"
black_material = bpy.data.materials.new('BlackMaterial')
black_material.use_nodes = True
color = (0, 0, 0, 1)
black_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(black_material)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, location=(0.5, 0.75, 5.25))
bpy.context.object.parent = robot
bpy.context.object.name = "right_eye"
black_material = bpy.data.materials.new('BlackMaterial')
black_material.use_nodes = True
color = (0, 0, 0, 1) 
black_material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(black_material)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera_obj.location = (-12.36, 15.48, 12.76)
camera_obj.rotation_euler = (69 * pi / 180, 0, -133 * pi / 180)
bpy.ops.mesh.primitive_plane_add(size=50, location=(0, 0, 0))
bpy.context.scene.camera = camera_obj
bpy.context.scene.render.filepath = '/tmp/rendered_robot.png'
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.ops.object.light_add(type='SPOT', radius=10, location=(-16.02, 10, 10))
spot_light = bpy.context.object
spot_light.data.energy = 8000
spot_light.data.spot_size = pi / 4 
spot_light.data.spot_blend = 0.5  
spot_light.rotation_euler = (-5*pi / 180, -58*pi / 180, -27*pi / 180)
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = '/tmp/1.png'
bpy.ops.render.render(write_still=True)

雪人再进化

图片描述

import bpy
from math import pi

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_snowman():
    # Clear scene
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # Create materials
    materials = {
        'eye': create_material('mat_eye', (0, 0, 0, 1)),
        'hat': create_material('pink_hat', (1, 0.4, 0.5, 1)),
        'nose': create_material('orange_nose', (0.9, 0.3, 0, 1))
    }
    
    # Create head
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0, 0, 2))
    head = bpy.context.object
    head.name = "head"
    
    # Create eyes
    eyes_data = [
        ("r_eye", (0.8, 0.5, 0)),
        ("l_eye", (0.8, -0.5, 0))
    ]
    
    for name, loc in eyes_data:
        bpy.ops.mesh.primitive_uv_sphere_add()
        eye = bpy.context.object
        eye.name = name
        eye.location = loc
        eye.scale = (0.1, 0.1, 0.1)
        eye.data.materials.append(materials['eye'])
        eye.parent = head
    
    # Create body
    bpy.ops.mesh.primitive_uv_sphere_add()
    body = bpy.context.object
    body.name = "body"
    body.scale = (1.5, 1.5, 1.5)
    
    # Create hat
    hat_data = [
        ("hat1", (0, 0, 3), (0.7, 0.7, 0.5)),
        ("hat2", (0, 0, 2.8), (1, 1, 0.1))
    ]
    
    for name, loc, scale in hat_data:
        bpy.ops.mesh.primitive_cylinder_add()
        hat = bpy.context.object
        hat.name = name
        hat.location = loc
        hat.scale = scale
        hat.data.materials.append(materials['hat'])
    
    # Create nose
    bpy.ops.mesh.primitive_cone_add()
    nose = bpy.context.object
    nose.name = "nose"
    nose.location = (0.8, 0, -0.1)
    nose.scale = (0.5, 0.5, 0.8)
    nose.rotation_euler[1] = pi/2
    nose.data.materials.append(materials['nose'])
    nose.parent = head
    
    # Create buttons (kozi)
    kozi_data = [
        ("kozi1", (1, 0, 1)),
        ("kozi2", (1.3, 0, 0.6))
    ]
    
    for name, loc in kozi_data:
        bpy.ops.mesh.primitive_uv_sphere_add()
        kozi = bpy.context.object
        kozi.name = name
        kozi.location = loc
        kozi.scale = (0.2, 0.2, 0.2)
        kozi.data.materials.append(materials['hat'])
    
    # Create arms and fingers
    arms_data = [
        ("l_arm", (0, -1.5, 0.6), pi/3),
        ("r_arm", (0, 1.5, 0.6), -pi/3)
    ]
    
    for name, loc, rot in arms_data:
        bpy.ops.mesh.primitive_cylinder_add()
        arm = bpy.context.object
        arm.name = name
        arm.location = loc
        arm.scale = (0.1, 0.1, 1)
        arm.data.materials.append(materials['eye'])
        arm.rotation_euler[0] = rot
    
    # Create fingers
    fingers_data = [
        ((0, 2, 0.6), -pi/2),
        ((0, -2, 0.6), pi/2)
    ]
    
    for loc, rot in fingers_data:
        bpy.ops.mesh.primitive_cylinder_add()
        finger = bpy.context.object
        finger.name = "finger"
        finger.location = loc
        finger.scale = (0.1, 0.1, 0.3)
        finger.data.materials.append(materials['eye'])
        finger.rotation_euler[0] = rot
    
    return head

if __name__ == "__main__":
    create_snowman()

雪人与女友

图片描述

  • 雪人获得了女朋友
import bpy

bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()

def create_snow_man():
    bpy.ops.mesh.primitive_uv_sphere_add()
    head = bpy.context.object
    bpy.context.object.name  = "head"
    mat = bpy.data.materials.new('mat_head')
    mat.use_nodes = True
    color = (1, 0.7, 0.6, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)

    r_eye = bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "r_eye"
    bpy.context.object.location = (0.7, 0.5, -0.3)
    bpy.context.object.scale = (0.3, 0.1, 0.3)
    mat = bpy.data.materials.new('mat_eye')
    color = (0, 0, 0, 1)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = head

    l_eye = bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "l_eye"
    bpy.context.object.location = (0.7, -0.5, -0.3)
    bpy.context.object.scale = (0.3, 0.1, 0.3)
    mat = bpy.data.materials.new('mat_l_eye')
    mat.use_nodes = True
    color = (0, 0, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = head

    bpy.ops.mesh.primitive_cone_add()
    body = bpy.context.object
    body.name  = "body"
    body.location = (0,0,-2.4)
    body.scale = (2,2,2)
    mat = bpy.data.materials.new('mat_body')
    mat.use_nodes = True
    color = (0.005, 0.06, 0.003, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    character = bpy.data.objects.new("character", None)
    bpy.data.collections["Collection"].objects.link(character)
    head.parent = character
    body.parent = character
    bpy.ops.mesh.primitive_plane_add(size=20)
    bpy.context.object.location = (0,0,-7.6)

def create_girl():
    girl = bpy.data.objects.new("girl", None)
    bpy.data.collections["Collection"].objects.link(girl)

    bpy.ops.mesh.primitive_cone_add()
    hat = bpy.context.object
    hat.name  = "hat"
    hat.location = (-1.1,0,2)
    hat.scale = (1,1,2)
    bpy.context.object.rotation_euler[1] = 3.14 / -6
    mat = bpy.data.materials.new('mat_hat')
    mat.use_nodes = True
    color = (1, 0, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_torus_add()
    bpy.context.object.name = "hat2"
    bpy.context.object.location = (-0.2,0,0.4)
    bpy.context.object.scale = (1, 1, 1)
    bpy.context.object.rotation_euler[1] = 3.14 / -6
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "hat3"
    bpy.context.object.location = (-2,0,3.5)
    bpy.context.object.scale = (0.5,0.5,0.5)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_cylinder_add()
    bpy.context.object.name = "l_leg"
    bpy.context.object.location = (0,-0.5,-6)
    bpy.context.object.scale = (0.3,0.3,1.5)
    mat = bpy.data.materials.new('mat_l_leg')
    mat.use_nodes = True
    color = (0.03, 0.01, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_cylinder_add()
    bpy.context.object.name = "r_leg"
    bpy.context.object.location = (0,0.5,-6)
    bpy.context.object.scale = (0.3,0.3,1.5)
    mat = bpy.data.materials.new('mat_r_leg')
    mat.use_nodes = True
    color = (0.03, 0.01, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_torus_add()
    bpy.context.object.name = "scarf"
    bpy.context.object.location = (0,0,-1.2)
    bpy.context.object.scale = (0.7, 0.7, 1)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_torus_add()
    bpy.context.object.name = "body2"
    bpy.context.object.location = (0,0,-4.3)
    bpy.context.object.scale = (1.8, 1.8, 1.5)
    mat = bpy.data.materials.new('mat_body2')
    mat.use_nodes = True
    color = (1, 0, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "body3"
    bpy.context.object.location = (0.4,0,-2)
    bpy.context.object.scale = (0.5,0.5,0.5)
    mat = bpy.data.materials.new('mat_body3')
    mat.use_nodes = True
    color = (1, 0, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl


    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "l ear"
    bpy.context.object.location = (0.2,-1,-0.08)
    bpy.context.object.scale = (0.5,0.5,0.5)
    mat = bpy.data.materials.new('mat_l ear')
    mat.use_nodes = True
    color = (0.005, 0.06, 0.003, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "r ear"
    bpy.context.object.location = (-0.2,1,-0.08)
    bpy.context.object.scale = (0.5,0.5,0.5)
    mat = bpy.data.materials.new('mat_r ear')
    mat.use_nodes = True
    color = (0.005, 0.06, 0.003, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "body4"
    bpy.context.object.location = (0.8,1,-3.2)
    bpy.context.object.scale = (0.4,0.5,0.5)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "body5"
    bpy.context.object.location = (0.8,-1,-3.2)
    bpy.context.object.scale = (0.4,0.5,0.5)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "snow"
    bpy.context.object.location = (-2,5.6,-4.5)
    bpy.context.object.scale = (3,3,3)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "snow2"
    bpy.context.object.location = (-2,5.6,-0.3)
    bpy.context.object.scale = (2,2,2)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "snow3"
    bpy.context.object.location = (-0.9,4.3,-0.36)
    bpy.context.object.scale = (0.5,0.5,0.5)
    mat = bpy.data.materials.new('mat_snow3')
    mat.use_nodes = True
    color = (0.03, 0.01, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

    bpy.ops.mesh.primitive_uv_sphere_add()
    bpy.context.object.name = "snow4"
    bpy.context.object.location = (-0.289,5.5,-0.33)
    bpy.context.object.scale = (0.5,0.5,0.5)
    mat = bpy.data.materials.new('mat_snow4')
    mat.use_nodes = True
    color = (0.03, 0.01, 0, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    bpy.context.object.data.materials.append(mat)
    bpy.context.object.parent = girl

def init_camera():
    camera = bpy.data.cameras.new('Camera')
    camera_obj = bpy.data.objects.new('Camera', camera)
    bpy.data.collections["Collection"].objects.link(camera_obj)
    camera.lens = 50  # Focal length in millimeters
    camera.sensor_width = 36  # Sensor width in millimeters
    camera.sensor_height = 24  # Sensor height in millimeters
    camera_obj.location = (23.3,5.9, 8.1)  # X, Y, Z coordinates
    camera_obj.rotation_euler = (1.172,0,1.8)
    bpy.context.scene.camera = camera_obj

def init_light():
    bpy.ops.object.light_add(type='SPOT', radius=3)
    bpy.context.object.data.energy = 12000
    bpy.context.object.location = (11.77,11.81,24.43)
    bpy.context.object.rotation_euler = (36.3/180*3.14,0,124.3/180*3.14)
    bpy.context.scene.render.resolution_x = 640
    bpy.context.scene.render.resolution_y = 480
    bpy.context.scene.render.resolution_percentage = 50

def render():
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = '/tmp/render2.png'
    bpy.ops.render.render(write_still=True)

create_snow_man()
create_girl()
init_camera()
init_light()
render()

村民

图片描述

  • 巧妙利用对称
import bpy

def clear_scene():
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

def create_material(name, color):
    mat = bpy.data.materials.new(name=name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_cube(name, location, scale, color, rotation=(0,0,0)):
    bpy.ops.mesh.primitive_cube_add(size=1, location=location)
    cube = bpy.context.active_object
    cube.name = name
    cube.scale = scale
    cube.rotation_euler = rotation
    mat = create_material(f"{name}Material", color)
    cube.data.materials.append(mat)
    return cube

def create_character():
    # 定义颜色
    colors = {
        'skin': (0.9, 0.7, 0.6, 1),
        'green': (0.2, 0.8, 0.2, 1),
        'brown': (0.6, 0.3, 0.1, 1),
        'black': (0, 0, 0, 1),
        'white': (1, 1, 1, 1)
    }
    
    # 创建头部和面部特征
    head = create_cube("Head", (0,0,2), (0.8,0.8,0.8), colors['skin'])
    
    # 创建眼睛
    for x in [-0.2, 0.2]:
        eye = create_cube(f"Eye_{x}", (x,0.4,2.1), (1,1,1), colors['white'])
        eye.scale = (0.15,0.15,0.15)
        eye_green = create_cube(f"EyeGreen_{x}", (x*1.1875,0.5,2.1), (0.5,1,1), colors['green'])
        eye_green.scale = (0.075,0.075,0.075)
        eyebrow = create_cube(f"Eyebrow_{x}", (x,0.4,2.3), (0.3,0.1,0.1), colors['black'])
    
    # 创建鼻子
    nose = create_cube("Nose", (0,0.5,2), (0.2,0.2,0.3), colors['brown'])
    
    # 创建身体
    upper_body = create_cube("UpperBody", (0,0,1.5), (0.6,0.4,0.5), colors['green'])
    lower_body = create_cube("LowerBody", (0,0,1), (0.6,0.4,0.5), colors['brown'])
    
    # 创建手臂
    for x in [-0.3, 0.3]:
        arm = create_cube(f"Arm_{x}", (x*0.6,0.1,1.5), (0.2,0.2,0.8), colors['green'],
                         rotation=(0,0,-1.2 if x > 0 else 1.2))
    
    # 创建腿部
    for x in [-0.2, 0.2]:
        leg = create_cube(f"Leg_{x}", (x,0,0.5), (0.2,0.2,0.8), colors['brown'])

if __name__ == "__main__":
    clear_scene()
    create_character()

彻底立方化

图片描述

import bpy
import math

# 定义一个函数来创建立方体并设置其属性
def create_cube(location, rotation, scale, material_name, diffuse_color):
    # 创建一个立方体
    bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=location)
    
    # 获取当前立方体对象
    cube = bpy.context.active_object
    
    # 设置位置
    cube.location = location
    
    # 设置旋转(将角度转换为弧度)
    cube.rotation_euler = (math.radians(rotation[0]), math.radians(rotation[1]), math.radians(rotation[2]))
    
    # 设置缩放
    cube.scale = scale
    
    # 设置变换模式为 XYZ 欧拉
    cube.rotation_mode = 'XYZ'
    
    # 创建一个新材质
    material = bpy.data.materials.new(name=material_name)
    
    # 设置材质的颜色
    material.use_nodes = True
    material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = diffuse_color  # 使用传入的颜色
    
    # 将材质应用到立方体
    cube.data.materials.append(material)

# 定义每个立方体的参数,包括颜色
cubes = [
    {"location": (0, 0, 6.0531), "rotation": (0, 0, 0), "scale": (0.908, 0.633, 1.889), "material_name": "GreyMaterial1", "diffuse_color": (0.5, 0.8, 0.9, 1)},
    {"location": (0, 0, 8.9505), "rotation": (0, 0, 0), "scale": (1.000, 1.000, 1.000), "material_name": "GreyMaterial2", "diffuse_color": (0.8, 0.6, 0.5, 1)},
    {"location": (1.2175, 1.2552, 5.4779), "rotation": (-90, 125.47, -90), "scale": (1.716, 0.375, 0.377), "material_name": "GreyMaterial3", "diffuse_color": (0.8, 0.6, 0.5, 1)},
    {"location": (1.2175, 0.019304, 7.2122), "rotation": (-90, 125.47, -90), "scale": (0.399, 0.375, 0.377), "material_name": "GreyMaterial4", "diffuse_color": (0.5, 0.8, 0.9, 1)},
    {"location": (-1.2735, -1.6849, 5.9744), "rotation": (90, 143.48, 90), "scale": (1.716, 0.375, 0.377), "material_name": "GreyMaterial5", "diffuse_color": (0.8, 0.6, 0.5, 1)},
    {"location": (-1.2735, -0.038982, 7.1932), "rotation": (90, 143.48, 90), "scale": (0.349, 0.375, 0.377), "material_name": "GreyMaterial6", "diffuse_color": (0.5, 0.8, 0.9, 1)},
    {"location": (-1.2459, -2.9336, 5.9344), "rotation": (33.062, 0, 0), "scale": (0.133, -0.126, 1.000), "material_name": "GreyMaterial7", "diffuse_color": (0.8, 0.8, 0.8, 1)},
    {"location": (0.45889, -0.470097, 2.4756), "rotation": (-12.125, 0, 0), "scale": (0.396, 0.407, 1.889), "material_name": "GreyMaterial8", "diffuse_color": (0.5, 0.8, 0.9, 1)},
    {"location": (-0.44446, 0.32416, 2.4645), "rotation": (14.287, 0, 0), "scale": (0.396, 0.407, 1.889), "material_name": "GreyMaterial9", "diffuse_color": (0.5, 0.8, 0.9, 1)},
    {"location": (-0.44446, 0.86122, 0.35558), "rotation": (14.287, 0, 0), "scale": (0.396, 0.407, 0.305), "material_name": "GreyMaterial10", "diffuse_color": (0.647, 0.165, 0.165, 1)},
    {"location": (-1.2459, -2.9336, 5.9344), "rotation": (33.062, 0, 0), "scale": (0.133, -0.126, 1.000), "material_name": "GreyMaterial11", "diffuse_color": (0.647, 0.165, 0.165, 1)},
    {"location": (-1.2459, -3.5799, 6.9272), "rotation": (33.062, 0, 0), "scale": (0.133, -0.126, 0.125), "material_name": "YellowMaterial", "diffuse_color": (1.0, 1.0, 0.0, 1)},
    {"location": (0.45889, -0.9244, 0.36097), "rotation": (-12.125, 0, 0), "scale": (0.396, 0.407, 0.324), "material_name": "BrownMaterial", "diffuse_color": (0.647, 0.165, 0.165, 1)}  # 新增的棕色立方体
]

# 创建每个立方体
for cube_data in cubes:
    create_cube(cube_data["location"], cube_data["rotation"], cube_data["scale"], cube_data["material_name"], cube_data["diffuse_color"])

# 更新场景
bpy.context.view_layer.update()

熊猫头

图片描述

import bpy

man = bpy.data.objects.new("man",None)
bpy.data.collections["Collection"].objects.link(man)
#this is head
bpy.ops.mesh.primitive_uv_sphere_add()
head = bpy.context.object
head.location = (0,0,3.5)
head.scale = (3,3,2.6)
head.parent = man
main_material = bpy.data.materials.new("MainMat")
main_material.diffuse_color = [1.5,2,2,1]
bpy.context.object.active_material =  main_material
#this is ear
bpy.ops.mesh.primitive_uv_sphere_add()
leftear = bpy.context.object
leftear.location = (-1.86,-0.34,5.5)
leftear.scale = (1,0.5,1)
leftear.parent = man
mat = bpy.data.materials.new('mat_ear')
mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_uv_sphere_add()
rightear = bpy.context.object
rightear.location = (1.86,-0.34,5.5)
rightear.scale = (1,0.5,1)
rightear.parent = man
bpy.context.object.data.materials.append(mat)
#this is eye
bpy.ops.mesh.primitive_uv_sphere_add()
lefteye = bpy.context.object
lefteye.location = (-1.6,-2.1,3.3)
lefteye.scale = (1.1,0.5,1)
bpy.context.object.rotation_euler[2] = -0.610865
lefteye.parent = man
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_uv_sphere_add()
righteye = bpy.context.object
righteye.location = (1.6,-2.1,3.3)
righteye.scale = (1.1,0.5,1)
bpy.context.object.rotation_euler[2] = 0.610865
righteye.parent = man
bpy.context.object.data.materials.append(mat)

熊头

图片描述

import bpy
# 创建头部

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0))
bpy.context.object.scale = (2.5, 3, 2.5)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="headear")
obj.data.materials.append(mat)
mat.diffuse_color = (0.800236, 0.535324, 0.328868, 1)
obj.name = "head"

# 创建左耳
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 2, 2))
bpy.context.object.scale = (0.7, 1, 0.7)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="headear")
obj.data.materials.append(mat)
mat.diffuse_color = (0.800236, 0.535324, 0.328868, 1)
obj.name = "ear1"

# 创建右耳
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, -2, 2))
bpy.context.object.scale = (0.7, 1, 0.7)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="headear")
obj.data.materials.append(mat)
mat.diffuse_color = (0.800236, 0.535324, 0.328868, 1)
obj.name = "ear2"

# 创建左眉毛
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(1.9, 1, 1))
bpy.context.object.scale = (0.25, 0.4, 0.3)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="eyebrow")
obj.data.materials.append(mat)
mat.diffuse_color = (0.370, 0.250, 0.156, 1)
obj.name = "eyebrow1"

# 创建右眉毛
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(1.9, -1, 1))
bpy.context.object.scale = (0.25, 0.4, 0.3)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="eyebrow")
obj.data.materials.append(mat)
mat.diffuse_color = (0.370, 0.250, 0.156, 1)
obj.name = "eyebrow2"

# 创建左眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(2.25, -1, 0.6))
bpy.context.object.scale = (0.25, 0.3, 0.35)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="eye")
obj.data.materials.append(mat)
mat.diffuse_color = (0.204, 0.139, 0.088, 0.9)
obj.name = "eye1"

# 创建右眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(2.25, 1, 0.6))
bpy.context.object.scale = (0.25, 0.3, 0.35)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="eye")
obj.data.materials.append(mat)
mat.diffuse_color = (0.204, 0.139, 0.088, 0.9)
obj.name = "eye2"

# 创建鼻子
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(2.4, 0, 0))
bpy.context.object.scale = (0.16, 0.3, 0.2)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="nose")
obj.data.materials.append(mat)
mat.diffuse_color = (0.071, 0.050, 0.033, 0.95)
obj.name = "nose"

# 创建嘴巴
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(2.25, 0, -0.7))
bpy.context.object.scale = (0.16, 0.33, 0.28)
bpy.ops.object.shade_smooth()
obj = bpy.context.object
mat = bpy.data.materials.new(name="mouth")
obj.data.materials.append(mat)
mat.diffuse_color = (0.496, 0.167, 0.110, 0.9)
obj.name = "mouth"

# 设置相机
camera_data = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera_data)
bpy.context.scene.collection.objects.link(camera_obj)
camera_data.lens = 40
camera_obj.location = (10, -9, 5.5)
camera_obj.rotation_euler = (1.21404, -0.10472, 0.919648)
bpy.context.scene.camera = camera_obj

# 添加光源
bpy.ops.object.light_add(type='AREA', location=(5, 2, 3))
light = bpy.context.object
light.data.energy = 100
light.data.shadow_soft_size = 0.05
light.rotation_euler = (0.645, 0.524, 1.85)
bpy.context.view_layer.update()

# 设置渲染
scene = bpy.context.scene
scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = '/tmp/output.png'
bpy.ops.render.render(write_still=True)

仓鼠

图片描述

import bpy
# clear current scene
bpy.ops.object.select_all(action="SELECT") # 选择所有物体
bpy.ops.object.delete() # 删除选定的物体
#脸
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(0.95, 1, 0.95))
head = bpy.context.object
head.name = "Head1"
head.data.materials.append(bpy.data.materials.new(name="HeadMaterial"))
head.data.materials[0].diffuse_color = (1, 0.8, 0.4,1)  

#下半脸
bpy.ops.object.metaball_add(type='CAPSULE', radius=2, enter_editmode=False, align='WORLD', location=(0, 0, -0.39), scale=(0.688, 0.814, 0.672))
bpy.context.object.rotation_euler[2] = 1.5708
bpy.context.object.scale[0] = 0.688
bpy.context.object.scale[1] = 0.814
bpy.context.object.scale[2] = 0.672
head = bpy.context.object
head.name = "Head2"
head.data.materials.append(bpy.data.materials.new(name="HeadMaterial2"))
head.data.materials[0].diffuse_color = (0.97,1 , 0.84,1) 

#ears
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, -0.7, 0.7), scale=(0.13, 0.18, 0.18))
ear = bpy.context.object
ear.name = "earR"
ear.data.materials.append(bpy.data.materials.new(name="EarMaterial"))
ear.data.materials[0].diffuse_color = (1, 0.8, 0.4,1)  

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0.7, 0.7), scale=(0.13, 0.18, 0.18))
ear = bpy.context.object
ear.name = "earL"
ear.data.materials.append(bpy.data.materials.new(name="EarMaterial2"))
ear.data.materials[0].diffuse_color = (1, 0.8, 0.4,1) 

#eyes
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0.89, -0.3, 0.037), scale=(0.06, 0.08, 0.186))
eye = bpy.context.object
eye.name = "eyeR"
eye.data.materials.append(bpy.data.materials.new(name="EyeMaterial"))
eye.data.materials[0].diffuse_color = (0, 0, 0,1) 

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0.89, 0.3, 0.037), scale=(0.06, 0.08, 0.186))
eye = bpy.context.object
eye.name = "eyeL"
eye.data.materials.append(bpy.data.materials.new(name="EyeMaterial2"))
eye.data.materials[0].diffuse_color = (0, 0, 0,1) 
#nose
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0.91, 0, -0.21), scale=(0.063, 0.08, 0.063))
nose = bpy.context.object
nose.name = "nose"
nose.data.materials.append(bpy.data.materials.new(name="NoseMaterial"))
nose.data.materials[0].diffuse_color = (0, 0, 0,1) 
#mouse
bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(0.9, 0, -0.3), rotation=(0, 1.18682, 0))
bpy.context.object.scale[0] = 0.086
bpy.context.object.scale[1] = 0.152
bpy.context.object.scale[2] = 0.104
mouse = bpy.context.object
mouse.name = "mouse"
mouse.data.materials.append(bpy.data.materials.new(name="MouseMaterial"))
mouse.data.materials[0].diffuse_color = (0, 0, 0,1) 
#camera
bpy.ops.object.camera_add(enter_editmode=False, align='VIEW', location=(6.38, 1.17, 0), rotation=(89.5288, 0, 102.297), scale=(1, 1, 1))
#light
bpy.ops.object.light_add(type='SPOT', align='WORLD', location=(9.17, -2.08, 3.11), scale=(1, 1, 1))
bpy.context.object.rotation_euler[0] = 0.279253
bpy.context.object.rotation_euler[1] = 1.06465
bpy.context.object.rotation_euler[2] = 6.37045

bpy.context.object.data.energy = 900

# 设置渲染分辨率
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480

狗头

图片描述

import bpy
import mathutils
import math

def create_triangular_prism(location, name):
    vertices = [
        mathutils.Vector((-0.5, -0.5, 0)),
        mathutils.Vector((0.5, -0.5, 0)),
        mathutils.Vector((0, 0.5, 0)),
        mathutils.Vector((-0.5, -0.5, 1)),
        mathutils.Vector((0.5, -0.5, 1)),
        mathutils.Vector((0, 0.5, 1))
    ]
    faces = [(0, 1, 2), (3, 4, 5), (0, 3, 4, 1), (1, 4, 5, 2), (2, 5, 3, 0)]
    
    mesh_data = bpy.data.meshes.new(name + "_mesh")
    mesh_data.from_pydata(vertices, [], faces)
    mesh_data.update()
    
    obj = bpy.data.objects.new(name, mesh_data)
    bpy.context.collection.objects.link(obj)
    obj.location = location
    return obj

def create_material(name, color):
    mat = bpy.data.materials.new(name=name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def assign_material(obj, mat):
    if obj.data.materials:
        obj.data.materials[0] = mat
    else:
        obj.data.materials.append(mat)

def create_fox_head():
    # 清除场景
    for obj in bpy.data.objects:
        bpy.data.objects.remove(obj, do_unlink=True)
    
    # 创建空物体作为父对象
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
    cat = bpy.context.active_object
    cat.name = "Cat"
    
    # 创建耳朵
    left_ear = create_triangular_prism(mathutils.Vector((-0.5, 0, 0)), "左耳")
    right_ear = create_triangular_prism(mathutils.Vector((0.5, 0, 0)), "右耳")
    
    # 调整耳朵位置和旋转
    for ear in [left_ear, right_ear]:
        ear.location.z += 0.8
        ear.location.y += 0.2
        ear.scale = (0.6, 1, 0.6)
        ear.parent = cat  # 设置父对象
    
    # 耳朵旋转
    angle_45 = math.pi / 4
    angle_30 = math.pi / 6
    angle_20 = math.pi * 20 / 180
    
    for ear in [left_ear, right_ear]:
        ear.rotation_euler.x = angle_45 + angle_30
    
    left_ear.rotation_euler.y = -angle_20
    right_ear.rotation_euler.y = angle_20
    
    # 创建头部
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 0, 0))
    head = bpy.context.active_object
    head.name = "头"
    head.parent = cat  # 设置父对象
    
    # 创建嘴部
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.8, location=(0, -0.5, -0.3))
    mouth = bpy.context.active_object
    mouth.name = "嘴"
    mouth.scale.z = 0.8
    mouth.parent = cat  # 设置父对象
    
    # 创建眼睛
    for i in [-1, 1]:
        bpy.ops.mesh.primitive_uv_sphere_add(radius=0.15, 
            location=(i * 0.3, -0.8, 0.4))
        eye = bpy.context.active_object
        eye.name = f"{'左' if i < 0 else '右'}眼"
        eye.parent = cat  # 设置父对象
    
    # 创建鼻子
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, 
        location=(0, -1.25, -0.26))
    nose = bpy.context.active_object
    nose.name = "鼻子"
    nose.parent = cat  # 设置父对象
    
    # 创建材质
    orange_mat = create_material("橙色材质", (1.0, 0.5, 0.0, 1.0))
    black_mat = create_material("黑色材质", (0, 0, 0, 1))
    brown_mat = create_material("棕色材质", (0.6, 0.4, 0.2, 1))
    
    # 应用材质
    for obj in [head, left_ear, right_ear]:
        assign_material(obj, orange_mat)
    
    for eye_name in ["左眼", "右眼"]:
        eye = bpy.data.objects.get(eye_name)
        if eye:
            assign_material(eye, black_mat)
    
    nose = bpy.data.objects.get("鼻子")
    if nose:
        assign_material(nose, brown_mat)
    
    return cat  # 返回父对象以便后续操作

if __name__ == "__main__":
    create_fox_head()

奶龙

图片描述

import bpy
from math import pi

bpy.ops.object.select_all(action = 'SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_uv_sphere_add()
head = bpy.context.object
yellow = bpy.data.materials.new('yellow_head')
yellow.use_nodes = True
color = (0.8,0.5,0,1)
yellow.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(yellow)
bpy.context.object.location = (0.7,0,2)
bpy.context.object.scale = (1.5,1.8,1.6)
bpy.context.object.name = "head"

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.data.materials.append(yellow)
bpy.context.object.location = (1,0,1.2)
bpy.context.object.scale = (1.5,1.8,1.3)
bpy.context.object.name = "face"

r_beye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_beye"
bpy.context.object.location = (0.87,0.38,0.1)
bpy.context.object.scale = (0.1,0.12,0.1)
mat = bpy.data.materials.new('mat_eye')
mat.use_nodes = True
color = (0,0,0,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

l_beye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_beye"
bpy.context.object.location = (0.85,-0.48,0.1)
bpy.context.object.scale = (0.1,0.12,0.1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

r_weye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_weye"
bpy.context.object.location = (0.8,0.4,0)
bpy.context.object.scale = (0.2,0.25,0.2)
bpy.context.object.parent = head

l_weye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_weye"
bpy.context.object.location = (0.8,-0.4,0)
bpy.context.object.scale = (0.2,0.25,0.2)
bpy.context.object.parent = head

bpy.ops.mesh.primitive_uv_sphere_add()
pink = bpy.data.materials.new('pink_touth')
pink.use_nodes = True
color = (0.9,0.2,0.2,1)
bpy.context.object.data.materials.append(pink)
bpy.context.object.scale = (0.5,0.7,0.1)
bpy.context.object.location = (2.5,0.3,1.3)
pink.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.rotation_euler[1] = pi/6
bpy.context.object.name = "touth"

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.scale = (0.5,0.7,0.1)
bpy.context.object.location = (2.3,0,1.3)
bpy.context.object.scale = (0.2,0.8,0.3)
bpy.context.object.data.materials.append(mat)
bpy.context.object.name = "mouse"

轻松熊

图片描述

import bpy
from math import pi

bpy.ops.object.select_all(action = 'SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_uv_sphere_add()
head = bpy.context.object
bpy.context.object.location = (0,0,1)
bpy.context.object.scale = (1.2,1.55,1.3)
brown = bpy.data.materials.new('brown_head')
brown.use_nodes = True  # 启用节点
color = (1.2,0.5,0.2,1)
brown.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(brown)
bpy.context.object.name = "head"

r_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eye"
bpy.context.object.location = (0.9,0.45,-0.1)
bpy.context.object.scale = (0.1,0.1,0.1)
mat = bpy.data.materials.new('mat_eye')
mat.use_nodes = True  # 启用节点
color = (0.1,0,0,1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

l_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eye"
bpy.context.object.location = (0.9,-0.45,-0.1)
bpy.context.object.scale = (0.1,0.1,0.1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head


mouse = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "mouse"
bpy.context.object.location = (0.7,0,-0.2)
bpy.context.object.scale = (0.4,0.38,0.35)
bpy.context.object.parent = head

nose = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "nose"
bpy.context.object.location = (1.1,0,-0.2)
bpy.context.object.scale = (0.1,0.1,0.1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

l_ear = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_ear"
bpy.context.object.location = (0,-0.8,0.8)
bpy.context.object.scale = (0.3,0.4,0.4)
bpy.context.object.data.materials.append(brown)
bpy.context.object.parent = head

r_ear = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_ear"
bpy.context.object.location = (0,0.8,0.8)
bpy.context.object.scale = (0.3,0.4,0.4)
bpy.context.object.data.materials.append(brown)
bpy.context.object.parent = head

l_earin = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_earin"
yellow = bpy.data.materials.new('yellow_earin')
yellow.use_nodes = True  # 启用节点
color = (0.9,0.7,0,1)
yellow.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(yellow)
bpy.context.object.location = (0.1,-0.8,0.8)
bpy.context.object.scale = (0.3,0.3,0.3)
bpy.context.object.parent = head

r_earin = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_earin"
bpy.context.object.location = (0.1,0.8,0.8)
bpy.context.object.scale = (0.3,0.3,0.3)
bpy.context.object.data.materials.append(yellow)
bpy.context.object.parent = head

line = bpy.ops.mesh.primitive_cylinder_add()
bpy.context.object.name = "line"
bpy.context.object.location = (1,-0.1,-0.32)
bpy.context.object.scale = (0.1,0.1,0.03)
bpy.context.object.rotation_euler[0] = pi/3
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

line = bpy.ops.mesh.primitive_cylinder_add()
bpy.context.object.name = "line"
bpy.context.object.location = (1,0.1,-0.32)
bpy.context.object.scale = (0.1,0.1,0.03)
bpy.context.object.rotation_euler[0] = -pi/3
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = head

球体化

  • 要求全部用椭球制作
    • 气球小狗

图片描述

import bpy
from math import pi

def create_material(name, color):
    material = bpy.data.materials.new(name)
    material.use_nodes = True
    material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return material

def create_body_part(name, lodogion, scale, rotation=(0,0,0), material=None):
    bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=lodogion)
    part = bpy.context.object
    part.name = name
    part.scale = scale
    part.rotation_euler = rotation
    if material:
        part.data.materials.append(material)
    return part

def create_dog():
    # 清除场景
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # 创建空物体作为父对象
    bpy.ops.object.empty_add(type='PLAIN_AXES', )
    dog = bpy.context.active_object
    dog.name = "dog"
    
    # 创建材质
    pink = create_material('pink_material', (0.5, 1, 0.2, 0.9))
    
    # 创建身体部件
    parts = [
        # name, lodogion, scale, rotation
        ("head", (5, 0, 5), (1.5, 0.8, 0.8), (0, 0, 0)),
        ("l_ear", (3.5, 0.7, 6), (0.8, 0.8, 1.5), (-pi/6, 0, 0)),
        ("r_ear", (3.5, -0.7, 6), (0.8, 0.8, 1.5), (pi/6, 0, 0)),
        ("neck", (3.5, 0, 4), (0.8, 0.8, 1), (0, pi/6, 0)),
        ("l_fleg", (3.5, 0.7, 2), (0.8, 0.8, 1.8), (pi/8, -pi/6, 0)),
        ("r_fleg", (3.5, -0.7, 2), (0.8, 0.8, 1.8), (-pi/8, -pi/6, 0)),
        ("body", (1.5, 0, 3.5), (0.8, 0.8, 1.5), (0, pi/2, 0)),
        ("l_bleg", (-0.7, 0.7, 2), (0.8, 0.8, 1.8), (pi/8, pi/6, 0)),
        ("r_bleg", (-0.7, -0.7, 2), (0.8, 0.8, 1.8), (-pi/8, pi/6, 0)),
        ("tail", (-0.7, 0, 4), (0.8, 0.8, 1.2), (0, -pi/4, 0))
    ]
    # 创建所有部件
    dog_parts = {}
    for name, loc, scale, rot in parts:
        part = create_body_part(name, loc, scale, rot, pink)
        part.parent = dog
        dog_parts[name] = part
    return dog, dog_parts

if __name__ == "__main__":
    dog, parts = create_dog()

椭球化的企鹅

图片描述

import bpy

# 切换到对象模式
bpy.ops.object.mode_set(mode='OBJECT')

# 获取当前场景
scene = bpy.context.scene

# 遍历场景中的所有物体
for obj in scene.objects:
    # 移除物体
    bpy.data.objects.remove(obj, do_unlink=True)

# 步骤1: 创建并缩放球体形成椭圆
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 0, 0))
ellipse_sphere = bpy.context.active_object
ellipse_sphere.scale = (1.15, 1, 1)

# 步骤2: 创建用于凹陷的小球体(眼睛)
eye_radius = 0.2
eye_offset_x = 0.6
eye_offset_y = 0

# 创建左眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=eye_radius, location=(-eye_offset_x, -0.7, 0.2))
left_eye = bpy.context.active_object

# 创建右眼
bpy.ops.mesh.primitive_uv_sphere_add(radius=eye_radius, location=(eye_offset_x, -0.7, 0.2))
right_eye = bpy.context.active_object

#
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.27, location=(0, -0.9, -0.2))
sphere = bpy.context.active_object
sphere.name = "MySphere"

# 步骤 2: 在不同轴向上进行缩放以形成椭圆形状
sphere.scale = (2.3, 1.7, 1.2)

import math

# 获取名为 "MySphere" 的球体对象
sphere = bpy.data.objects.get("MySphere")

if sphere:
    # 旋转角度(这里设置绕 Z 轴旋转 45 度,需要将角度转换为弧度,因为 Blender 使用弧度制)
    rotation_angle = math.radians(90)
    # 设置球体绕 Z 轴旋转指定角度
    sphere.rotation_euler.z = rotation_angle
    # 更新视图以显示旋转变化
    bpy.context.view_layer.update()
else:
    print("未找到名为 'MySphere' 的球体对象。")

#body
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(0, 0,-2.7))
sphere = bpy.context.active_object
sphere.name = "MySphere.2"

sphere = bpy.data.objects.get("MySphere.2")
rotation_angle = math.radians(90)
# 设置球体绕 Y 轴旋转指定角度
sphere.rotation_euler.y = rotation_angle

sphere = bpy.data.objects.get("MySphere.2")
rotation_angle = math.radians(90)
# 设置球体绕 X 轴旋转指定角度
sphere.rotation_euler.x = rotation_angle

# 步骤 2: 在不同轴向上进行缩放以形成椭圆形状
sphere.scale = (2.3, 1.7, 1.5)

# 定义圆柱体的参数
radius = 0.185
depth = 2
# 第一个圆柱体的位置
location_1 = (0.65, 0, -4.35)

# 创建第一个圆柱体
bpy.ops.mesh.primitive_cylinder_add(
    radius=radius, 
    depth=depth, 
    location=location_1
)
cylinder_1 = bpy.context.active_object
cylinder_1.name = "Cylinder_1"

# 计算第二个圆柱体的对称位置
# 以 Z 轴为对称轴,X 和 Y 坐标取相反数
location_2 = (-location_1[0], -location_1[1], location_1[2])

# 创建第二个圆柱体
bpy.ops.mesh.primitive_cylinder_add(
    radius=radius, 
    depth=depth, 
    location=location_2
)
cylinder_2 = bpy.context.active_object
cylinder_2.name = "Cylinder_2"

# 更新视图以显示创建的圆柱体
bpy.context.view_layer.update()

# 定义椭圆的参数
radius = 1
scale_x = 3  # X 轴缩放比例,使椭圆变扁
scale_y = 0.5  # Y 轴缩放比例,使椭圆变扁
offset_x = 2  # 椭圆在 X 轴上的偏移量
rotation_angle = math.radians(15)  # 椭圆的旋转角度,呈现外八形态

# 定义参数
radius = 0.8
scale_x = 2  # X 轴缩放比例,使球体在 X 方向拉长
scale_y = 1  # Y 轴缩放比例,使球体在 Y 方向压缩,从而变扁
scale_z = 0.5  # Z 轴缩放比例,使球体在 Z 方向压缩,从而变扁
offset_x = 1.18  # 球体在 X 轴上的偏移量
offset_y = -0.8
offset_z = -5.25  # 向下平移的距离,可根据需要调整
rotation_angle = math.radians(125)  # 球体的旋转角度,呈现外八形态

# 创建第一个球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(offset_x, offset_y, offset_z))
sphere_1 = bpy.context.active_object
sphere_1.name = "Sphere_Ellipse_1"
# 调整球体形状使其变扁
sphere_1.scale = (scale_x, scale_y, scale_z)
# 旋转球体
sphere_1.rotation_euler.z = rotation_angle

# 创建第二个球体,关于 Z 轴对称
bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(-offset_x, offset_y, offset_z))
sphere_2 = bpy.context.active_object
sphere_2.name = "Sphere_Ellipse_2"
# 调整球体形状使其变扁
sphere_2.scale = (scale_x, scale_y, scale_z)
# 旋转球体,方向与第一个球体相反
sphere_2.rotation_euler.z = -rotation_angle

# 更新视图以显示创建的椭圆状球体
bpy.context.view_layer.update()
# 更新视图以显示创建的椭圆
bpy.context.view_layer.update()

import bpy
import math

# 定义参数
radius = 1
scale_x = 0.25  # 使椭圆在 X 方向变扁
scale_y = 0.7  # 使椭圆在 Y 方向变扁
scale_z = 2    # 使椭圆在 Z 方向拉长
offset_x = 2   # X 轴偏移量,控制两个椭圆的间距
rotation_angle = math.radians(10)  # 椭圆旋转角度,呈现八字形态
rotation_angle2 = math.radians(25) # 椭圆旋转角度,呈现八字形态

# 创建第一个椭圆
bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(offset_x, 0.5, 0))
ellipse_1 = bpy.context.active_object
ellipse_1.name = "Ellipse_1"
# 调整椭圆形状
ellipse_1.scale = (scale_x, scale_y, scale_z)
# 旋转椭圆,使其向外
ellipse_1.rotation_euler.z = rotation_angle
# 旋转椭圆,使其成八字
ellipse_1.rotation_euler.y = rotation_angle2

# 创建第二个椭圆,关于 Z、Y 轴对称
bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=(-offset_x, 0.5, 0))
ellipse_2 = bpy.context.active_object
ellipse_2.name = "Ellipse_2"
# 调整椭圆形状
ellipse_2.scale = (scale_x, scale_y, scale_z)
# 旋转椭圆,使其顶部向外,与第一个椭圆形成八字
ellipse_2.rotation_euler.z = -rotation_angle
ellipse_2.rotation_euler.y = -rotation_angle2


    
# 更新视图以显示变化
bpy.context.view_layer.update()

面包狗

图片描述

import bpy
from math import pi

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
	mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_dog():
    # Clear scene
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # Create empty parent object
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
    dog = bpy.context.active_object
    dog.name = "Dog"
    
    # Create materials
    orange_mat = create_material('orange_body', (0.45, 0.2, 0.05, 1))
    black_mat = create_material('mat_eye', (0, 0, 0, 1))
    
    # Create body
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0, 0, 2))
    body = bpy.context.object
    body.name = "body"
    body.scale = (4, 3, 3)
    body.data.materials.append(orange_mat)
    body.parent = dog
    
    # Create eyes and point
    eyes = [
        ("r_eye", (0.9, 0.3, 0.05), (0.07, 0.1, 0.15)),
        ("l_eye", (0.9, -0.3, 0.05), (0.07, 0.1, 0.15)),
        ("point", (1.15, 0, -0.15), (0.05, 0.05, 0.05))
    ]
    
    for name, loc, scale in eyes:
        bpy.ops.mesh.primitive_uv_sphere_add(location=loc)
        eye = bpy.context.object
        eye.name = name
        eye.scale = scale
        eye.data.materials.append(black_mat)
        eye.parent = body
    
    # Create face and tongue
    bpy.ops.mesh.primitive_uv_sphere_add(location=(2, 0, 1))
    face = bpy.context.object
    face.name = "face"
    face.scale = (2, 3, 2)
    face.parent = dog
    
    bpy.ops.mesh.primitive_cone_add(location=(4, 0, 1.5))
    tongue = bpy.context.object
    tongue.name = "tongue"
    tongue.scale = (1, 1.5, 0.7)
    tongue.rotation_euler[1] = pi/2
    tongue.parent = dog
    
    # Create ears
    ears = [
        ("r_ear", (1.5, -1.5, 4.5), (1, 0.7, 0.5), pi/6),
        ("l_ear", (1.5, 1.5, 4.5), (1, 0.7, 0.5), -pi/6)
    ]
    
    for name, loc, scale, rot in ears:
        bpy.ops.mesh.primitive_cone_add(location=loc)
        ear = bpy.context.object
        ear.name = name
        ear.scale = scale
        ear.rotation_euler[0] = rot
        ear.data.materials.append(orange_mat)
        ear.parent = dog
    
    # Create legs
    legs = [
        ("leg1", (1.5, -1.5, -1), (0.7, 0.7, 0.5), pi/6*5),
        ("leg2", (1.5, 1.5, -1), (0.7, 0.7, 0.5), -pi/6*5),
        ("leg3", (-1.5, 1.5, -0.7), (0.7, 0.7, 0.5), -pi/6*5),
        ("leg4", (-1.5, -1.5, -0.7), (0.7, 0.7, 0.5), pi/3*2.5)
    ]
    
    for name, loc, scale, rot in legs:
        bpy.ops.mesh.primitive_cone_add(location=loc)
        leg = bpy.context.object
        leg.name = name
        leg.scale = scale
        leg.rotation_euler[0] = rot
        leg.data.materials.append(orange_mat)
        leg.parent = dog
    
    return dog

if __name__ == "__main__":
    create_dog()

小黑猫

图片描述

import bpy
from math import pi

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_cat():
    # 清除场景
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # 创建空物体作为父对象
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
    cat = bpy.context.active_object
    cat.name = "Cat"
    
    # 创建材质
    brown_mat = create_material('brown_head', (0, 0, 0, 1))
    
    # 创建头部
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0.5, 0, 2.5))
    head = bpy.context.object
    head.name = "head"
    head.scale = (1.2, 1.55, 1.3)
    head.data.materials.append(brown_mat)
    head.parent = cat
    
    # 创建眼睛
    eyes_data = [
        ("r_eye", (0.9, 0.45, -0.1), pi/10),
        ("l_eye", (0.9, -0.45, -0.1), -pi/10)
    ]
    
    for name, loc, rot in eyes_data:
        bpy.ops.mesh.primitive_uv_sphere_add(location=loc)
        eye = bpy.context.object
        eye.name = name
        eye.scale = (0.1, 0.2, 0.25)
        eye.rotation_euler[2] = rot
        eye.parent = head
    
    # 创建线条
    lines_data = [
        (1, 0, -0.32, pi/3),
        (1, 0.1, -0.32, -pi/3)
    ]
    
    for x, y, z, rot in lines_data:
        bpy.ops.mesh.primitive_cylinder_add(location=(x, y, z))
        line = bpy.context.object
        line.name = "line"
        line.scale = (0.1, 0.1, 0.03)
        line.rotation_euler[0] = rot
        line.parent = head
    
    # 创建耳朵
    ears_data = [
        ("r_ear", (-0.5, 0.8, 0.55), (-pi/4, -pi/4)),
        ("l_ear", (-0.5, -0.8, 0.55), (pi/4, -pi/5))
    ]
    
    for name, loc, (rot_x, rot_y) in ears_data:
        bpy.ops.mesh.primitive_cone_add(location=loc)
        ear = bpy.context.object
        ear.name = name
        ear.scale = (0.5, 0.6, 0.5)
        ear.rotation_euler[0] = rot_x
        ear.rotation_euler[1] = rot_y
        ear.data.materials.append(brown_mat)
        ear.parent = head
    
    # 创建身体
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0, 0, 1))
    body = bpy.context.object
    body.name = "body"
    body.scale = (1.5, 1.5, 1)
    body.data.materials.append(brown_mat)
    body.parent = cat
    
    # 创建尾巴
    bpy.ops.mesh.primitive_cylinder_add(location=(-1, -1, 1.5))
    tail = bpy.context.object
    tail.name = "tail"
    tail.scale = (0.2, 0.2, 2)
    tail.rotation_euler[0] = pi/4
    tail.rotation_euler[1] = -pi/5
    tail.data.materials.append(brown_mat)
    tail.parent = cat
    
    return cat

if __name__ == "__main__":
    create_cat()

小鸟

图片描述

import bpy
from math import pi

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_bird():
    # Clear scene
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # Create empty parent object
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
    bird = bpy.context.active_object
    bird.name = "Bird"
    
    # Create materials
    materials = {
        'body': create_material('green_head', (0.9, 0.5, 0, 1)),
        'eye': create_material('mat_eye', (0, 0, 0, 1)),
        'beak': create_material('orange_mouse', (0.9, 0.3, 0, 1)),
        'wing': create_material('green_wing', (0.1, 0, 0, 1))
    }
    
    # Create head
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0.7, 0, 2))
    head = bpy.context.object
    head.name = "head"
    head.scale = (1.5, 1.5, 1.2)
    head.data.materials.append(materials['body'])
    head.parent = bird
    
    # Create eyes
    eyes_data = [
        ("r_eye", (0.9, 0.4, 0)),
        ("l_eye", (0.9, -0.4, 0))
    ]
    
    for name, loc in eyes_data:
        bpy.ops.mesh.primitive_uv_sphere_add(location=loc)
        eye = bpy.context.object
        eye.name = name
        eye.scale = (0.1, 0.1, 0.1)
        eye.data.materials.append(materials['eye'])
        eye.parent = head
    
    # Create body
    bpy.ops.mesh.primitive_uv_sphere_add(location=(0, 0, 0.5))
    body = bpy.context.object
    body.name = "body"
    body.scale = (2, 2, 1.5)
    body.data.materials.append(materials['body'])
    body.parent = bird
    
    # Create beak
    bpy.ops.mesh.primitive_cone_add(location=(0.8, 0, -0.1))
    beak = bpy.context.object
    beak.name = "beak"
    beak.scale = (0.5, 0.5, 0.3)
    beak.rotation_euler[1] = pi/2
    beak.data.materials.append(materials['beak'])
    beak.parent = head
    
    # Create wings
    wings_data = [
        ("r_wing", (0, -1.7, 0.5)),
        ("l_wing", (0, 1.7, 0.5))
    ]
    
    for name, loc in wings_data:
        bpy.ops.mesh.primitive_uv_sphere_add(location=loc)
        wing = bpy.context.object
        wing.name = name
        wing.scale = (0.9, 0.6, 1)
        wing.rotation_euler[1] = pi/4
        wing.data.materials.append(materials['wing'])
        wing.parent = bird
    
    # Create tail
    bpy.ops.mesh.primitive_cone_add(location=(-2, 0, 0.5))
    tail = bpy.context.object
    tail.name = "tail"
    tail.scale = (0.3, 0.6, 1.5)
    tail.rotation_euler[1] = pi/2
    tail.data.materials.append(materials['wing'])
    tail.parent = bird
    
    return bird

if __name__ == "__main__":
    create_bird()

小鸭

图片描述

import bpy

bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()


yazi = bpy.data.objects.new("yazi", None)

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name="tou"
bpy.context.object.name = "tou"
bpy.context.object.location = (1.9, 0, 2.5)
bpy.context.object.scale = (1,1,2)
mat = bpy.data.materials.new('mat_tou')
mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 1, 1, 1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = yazi

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name="body"
bpy.context.object.location = (0, 0 ,0.5)
bpy.context.object.scale = (2.8,2,2)
mat = bpy.data.materials.new('mat_body')
mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 1, 1, 1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = yazi

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name="zui"
bpy.data.objects["zui"].location = (2.8, 0 ,3)
bpy.data.objects["zui"].scale = (1,0.8,0.3)
mat = bpy.data.materials.new('mat_zui')
mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.5, 0.3, 0, 1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = yazi

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name="eye1"
bpy.context.object.name = "eye1"
bpy.context.object.location = (2.6, -0.5, 3.8)
bpy.context.object.scale = (0.2,0.2,0.2)
mat = bpy.data.materials.new('mat_eye1')
mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.01, 0.01, 0.01, 1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = yazi

bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name="eyer"
bpy.context.object.name = "eyer"
bpy.context.object.location = (2.6,0.5, 3.8)
bpy.context.object.scale = (0.2,0.2,0.2)
mat = bpy.data.materials.new('mat_eyer')
mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.01, 0.01, 0.01, 1)
bpy.context.object.data.materials.append(mat)
bpy.context.object.parent = yazi

小鸡

图片描述

import bpy
from math import pi

bpy.ops.object.select_all(action = 'SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_uv_sphere_add()
head = bpy.context.object
head.location = (0,0,1)
head.scale = (1.2,1.55,1.3)
brown = bpy.data.materials.new('brown_head')
brown.use_nodes = True
color = (1.2,0.5,0.2,1)
brown.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
head.data.materials.append(brown)
head.name = "head"

# 创建右眼睛
bpy.ops.mesh.primitive_uv_sphere_add()
r_eye = bpy.context.object
r_eye.name = "r_eye"
r_eye.location = (0.9,0.45,-0.1)
r_eye.scale = (0.1,0.1,0.1)
mat_eye = bpy.data.materials.new('mat_eye')
mat_eye.use_nodes = True
color = (0.1,0,0,1)
mat_eye.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
r_eye.data.materials.append(mat_eye)
r_eye.parent = head

# 创建左眼睛
bpy.ops.mesh.primitive_uv_sphere_add()
l_eye = bpy.context.object
l_eye.name = "l_eye"
l_eye.location = (0.9,-0.45,-0.1)
l_eye.scale = (0.1,0.1,0.1)
l_eye.data.materials.append(mat_eye)
l_eye.parent = head

# 创建嘴巴
mouse = bpy.ops.mesh.primitive_uv_sphere_add()
mouse = bpy.context.object
mouse.name = "mouse"
mouse.location = (0.7,0,-0.2)
mouse.scale = (0.4,0.38,0.35)
mouse.parent = head

# 创建鼻子
bpy.ops.mesh.primitive_uv_sphere_add()
nose = bpy.context.object
nose.name = "nose"
nose.location = (1.1,0,-0.2)
nose.scale = (0.1,0.1,0.1)
nose.data.materials.append(mat_eye)
nose.parent = head

# 创建左耳
bpy.ops.mesh.primitive_uv_sphere_add()
l_ear = bpy.context.object
l_ear.name = "l_ear"
l_ear.location = (0,-0.8,0.8)
l_ear.scale = (0.3,0.4,0.4)
l_ear.data.materials.append(brown)
l_ear.parent = head

# 创建右耳
bpy.ops.mesh.primitive_uv_sphere_add()
r_ear = bpy.context.object
r_ear.name = "r_ear"
r_ear.location = (0,0.8,0.8)
r_ear.scale = (0.3,0.4,0.4)
r_ear.data.materials.append(brown)
r_ear.parent = head

# 创建左耳内部
bpy.ops.mesh.primitive_uv_sphere_add()
l_earin = bpy.context.object
l_earin.name = "l_earin"
yellow = bpy.data.materials.new('yellow_earin')
yellow.use_nodes = True
color = (0.9,0.7,0,1)
yellow.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
l_earin.data.materials.append(yellow)
l_earin.location = (0.1,-0.8,0.8)
l_earin.scale = (0.3,0.3,0.3)
l_earin.parent = head

# 创建右耳内部
bpy.ops.mesh.primitive_uv_sphere_add()
r_earin = bpy.context.object
r_earin.name = "r_earin"
r_earin.location = (0.1,0.8,0.8)
r_earin.scale = (0.3,0.3,0.3)
r_earin.data.materials.append(yellow)
r_earin.parent = head

# 创建右胡须
line = bpy.ops.mesh.primitive_cylinder_add()
line = bpy.context.object
line.name = "r_whisker"
line.location = (1,-0.1,-0.32)
line.scale = (0.1,0.1,0.03)
line.rotation_euler[0] = pi/3
line.data.materials.append(mat_eye)
line.parent = head

# 创建左胡须
line = bpy.ops.mesh.primitive_cylinder_add()
line = bpy.context.object
line.name = "l_whisker"
line.location = (1,0.1,-0.32)
line.scale = (0.1,0.1,0.03)
line.rotation_euler[0] = -pi/3
line.data.materials.append(mat_eye)
line.parent = head


def create_niao():
    niao = bpy.data.objects.new("niao", None)
    bpy.data.collections["Collection"].objects.link(niao)
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    head_obj = bpy.context.object
    head_obj.name = "niao_head"
    head_obj.location = (0.7, 0, 4.5)
    mat_tou = bpy.data.materials.new('mat_tou')
    mat_tou.use_nodes = True
    mat_tou.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = (1,1,1,1)
    head_obj.data.materials.append(mat_tou)
    head_obj.parent = niao
    
    bpy.context.scene.frame_start = 1
    bpy.context.scene.frame_end = 10
    
    head_obj.location = (0.7, 0, 4.5)
    head_obj.rotation_euler = (0, 0, 0)
    head_obj.keyframe_insert(data_path="location", index=-1, frame=1)
    head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=1)
    
    head_obj.location = (1.5,0,3)
    head_obj.rotation_euler = (0,1,0)
    head_obj.keyframe_insert(data_path="location", index=-1, frame=5)
    head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=5)
    
    head_obj.location = (0.7,0,4.5)
    head_obj.rotation_euler = (0,0,0)
    head_obj.keyframe_insert(data_path="location", index=-1, frame=10)
    head_obj.keyframe_insert(data_path="rotation_euler", index=-1, frame=10)
    
    # 创建身体
    bpy.ops.mesh.primitive_uv_sphere_add()
    body_obj = bpy.context.object
    body_obj.name = "body"
    body_obj.parent = niao
    body_obj.location = (-1,0,2.5)
    body_obj.rotation_euler = (0,0,0)
    body_obj.scale = (2,2,2)
    mat_body = bpy.data.materials.new('mat_body')
    mat_body.use_nodes = True
    color = (1,1,1,1)
    mat_body.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    body_obj.data.materials.append(mat_body)
    
    # 创建嘴
    bpy.ops.mesh.primitive_cone_add()
    zui_obj = bpy.context.object
    zui_obj.name = "zui"
    zui_obj.location = (1.2,0,0)
    zui_obj.rotation_euler = (0,1.5,0)
    zui_obj.scale = (0.6,0.7,0.7)
    mat_zui = bpy.data.materials.new('mat_zui')
    mat_zui.use_nodes = True
    color = (0.5,0.3,0.2,1)
    mat_zui.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    zui_obj.data.materials.append(mat_zui)
    zui_obj.parent = head_obj
    
    # 创建头饰1
    bpy.ops.mesh.primitive_uv_sphere_add()
    toushil_obj = bpy.context.object
    toushil_obj.name = "toushi1"
    toushil_obj.location = (-0,0,1.3)
    toushil_obj.scale = (0.5,0.5,0.5)
    mat_toushil = bpy.data.materials.new('mat_toushil')
    mat_toushil.use_nodes = True
    color = (0.3,0.1,0.1,1)
    mat_toushil.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    toushil_obj.data.materials.append(mat_toushil)
    toushil_obj.parent = head_obj
    
    # 创建头饰2
    bpy.ops.mesh.primitive_uv_sphere_add()
    toushi2_obj = bpy.context.object
    toushi2_obj.name = "toushi2"
    toushi2_obj.location = (-0.8,0,1)
    toushi2_obj.scale = (0.5,0.5,0.5)
    mat_toushi2 = bpy.data.materials.new('mat_toushi2')
    mat_toushi2.use_nodes = True
    color = (0.3,0.1,0.1,1)
    mat_toushi2.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    toushi2_obj.data.materials.append(mat_toushi2)
    toushi2_obj.parent = head_obj
    
    # 创建头饰3
    bpy.ops.mesh.primitive_uv_sphere_add()
    toushi3_obj = bpy.context.object
    toushi3_obj.name = "toushi3"
    toushi3_obj.location = (0.7,0,-1)
    toushi3_obj.scale = (0.5,0.5,0.5)
    mat_toushi3 = bpy.data.materials.new('mat_toushi3')
    mat_toushi3.use_nodes = True
    color = (0.3,0.1,0.1, 1)
    mat_toushi3.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    toushi3_obj.data.materials.append(mat_toushi3)
    toushi3_obj.parent = head_obj
    
    # 创建左眼
    bpy.ops.mesh.primitive_uv_sphere_add()
    eye_l_obj = bpy.context.object
    eye_l_obj.name = "eye_l"
    eye_l_obj.location = (0.8,0.5,0.4)
    eye_l_obj.scale = (0.3,0.3,0.3)
    mat_eye_l = bpy.data.materials.new('mat_eye_l')
    mat_eye_l.use_nodes = True
    color = (0,0,0,1)
    mat_eye_l.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    eye_l_obj.data.materials.append(mat_eye_l)
    eye_l_obj.parent = head_obj
    
    # 创建右眼
    bpy.ops.mesh.primitive_uv_sphere_add()
    eye_r_obj = bpy.context.object
    eye_r_obj.name = "eye_r"
    eye_r_obj.location = (0.8,-0.5,0.4)
    eye_r_obj.scale = (0.3,0.3,0.3)
    mat_eye_r = bpy.data.materials.new('mat_eye_r')
    mat_eye_r.use_nodes = True
    color = (0,0,0,1)
    mat_eye_r.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    eye_r_obj.data.materials.append(mat_eye_r)
    eye_r_obj.parent = head_obj
    
    # 创建右脚
    bpy.ops.mesh.primitive_cylinder_add()
    jiaor_obj = bpy.context.object
    jiaor_obj.name = "jiaor"
    jiaor_obj.parent = niao
    jiaor_obj.location = (-1,-0.5,0)
    jiaor_obj.rotation_euler = (0,0,0)
    jiaor_obj.scale = (0.1,0.1,0.8)
    mat_jiaor = bpy.data.materials.new('mat_jiaor')
    color = (0.5,0.3,0.2,1)
    mat_jiaor.diffuse_color = color
    jiaor_obj.data.materials.append(mat_jiaor)
    
    # 创建左脚
    bpy.ops.mesh.primitive_cylinder_add()
    jiaol_obj = bpy.context.object
    jiaol_obj.name = "jiaol"
    jiaol_obj.parent = niao
    jiaol_obj.location = (-1,0.5,0)
    jiaol_obj.rotation_euler = (0,0,0)
    jiaol_obj.scale = (0.1,0.1,0.8)
    mat_jiaol = bpy.data.materials.new('mat_jiaol')
    mat_jiaol.use_nodes = True
    color = (0.5,0.3,0.2,1)
    mat_jiaol.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    jiaol_obj.data.materials.append(mat_jiaol)
    
    # 创建草地
    bpy.ops.mesh.primitive_cylinder_add()
    cao_obj = bpy.context.object
    cao_obj.name = "cao"
    cao_obj.location = (0,0,0)
    cao_obj.rotation_euler = (0,0,0)
    cao_obj.scale = (20,20,0.1)
    mat_cao = bpy.data.materials.new('mat_cao')
    mat_cao.use_nodes = True
    color = (0.1,0.6,0.1,1)
    mat_cao.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    cao_obj.data.materials.append(mat_cao)
    
    # 创建小米
    bpy.ops.mesh.primitive_uv_sphere_add()
    mi_obj = bpy.context.object
    mi_obj.name = "mi"
    mi_obj.location = (2,.7,0.2)
    mi_obj.rotation_euler = (0,0,5)
    mi_obj.scale = (0.3,0.1,0.1)
    mat_mi = bpy.data.materials.new('mat_mi')
    color = (1,1,0.4,1)
    mat_mi.diffuse_color = color
    mi_obj.data.materials.append(mat_mi)


def init_camera():
    camera = bpy.data.cameras.new('Camera')
    camera_obj = bpy.data.objects.new('Camera', camera)
    bpy.data.collections["Collection"].objects.link(camera_obj)
    camera.lens = 58
    camera.sensor_width = 36
    camera.sensor_height = 24
    camera_obj.location = (21, 5.9, 8.1)
    camera_obj.rotation_euler = (1.172, 0, 1.8)
    bpy.context.scene.camera = camera_obj


def create_camera():
    bpy.ops.object.camera_add()
    camera = bpy.context.object
    camera.location = (12.101823806762695, 4.5784618854522705, 8.910723686218262)
    camera.rotation_euler = (1.1, 0.013265283778309822, 1.1482713222503662)
    bpy.context.scene.camera = camera


def create_light():
    bpy.ops.object.light_add(type='SPOT')
    light = bpy.context.object
    light.location = (6.413093566894531, -6.293662071228027, 12.76513385772705)
    light.rotation_euler = (0.6422812938690186, -1.269639060552663e-08, 0.802851676940918)
    light.data.energy = 8000
    light.scale = (3,3,3)


def render():
    bpy.context.scene.frame_end = 1
    bpy.context.scene.render.resolution_x = 1280
    bpy.context.scene.render.resolution_y = 720
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = '/tmp/render'
    bpy.ops.render.render(animation=True)

# 执行主函数
create_niao()
create_camera()
create_light()
render()

派大星路障

图片描述

图片描述

import bpy

bpy.ops.object.select_all(action="SELECT") # 取消选择所有物体
if bpy.context.selected_objects:  # 只有当有物体被选中时才执行删除
    bpy.ops.object.delete() # 删除选定的物体

# 创建身体
body = bpy.ops.mesh.primitive_cone_add()
bpy.context.object.name = "body"
bpy.context.object.location = (0, 0, 0.9)
bpy.context.object.scale = (0.85, 0.85, 0.9)
mat = bpy.data.materials.new('mat_body')
    mat.use_nodes = True
color = (1, 0.3, 0.2, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建底部
bottom = bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=0.08)
bpy.context.object.name = "bottom"
bpy.context.object.location = (0, 0, 0)
bpy.context.object.scale = (1, 1, 1)
mat = bpy.data.materials.new('mat_bottom')
    mat.use_nodes = True
color = (1, 0.3, 0.2, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建白色圆环
whitecircle = bpy.ops.surface.primitive_nurbs_surface_torus_add(radius=0.7)
bpy.context.object.name = "whitecircle"
bpy.context.object.location = (0, 0, 0.35)
bpy.context.object.scale = (1, 1, 1.7)
mat = bpy.data.materials.new('mat_whitecircle')
    mat.use_nodes = True
color = (2.25, 2.25, 2.25, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建左眼白
l_eye_w = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eye_w"
bpy.context.object.location = (0.35, -0.17, 1)
bpy.context.object.scale = (0.2, 0.2, 0.3)
mat = bpy.data.materials.new('mat_l_eye_w')
    mat.use_nodes = True
color = (2.25, 2.25, 2.25, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建右眼白
r_eye_w = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eye_w"
bpy.context.object.location = (0.35, 0.17, 1)
bpy.context.object.scale = (0.2, 0.2, 0.3)
mat = bpy.data.materials.new('mat_r_eye_w')
    mat.use_nodes = True
color = (2.25, 2.25, 2.25, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建左眼黑
l_eye_b = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eye_b"
bpy.context.object.location = (0.55, -0.17, 1.1)
bpy.context.object.scale = (0.1, 0.1, 0.15)
mat = bpy.data.materials.new('mat_l_eye_b')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建右眼黑
r_eye_b = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eye_b"
bpy.context.object.location = (0.55, 0.17, 1.1)
bpy.context.object.scale = (0.1, 0.1, 0.15)
mat = bpy.data.materials.new('mat_r_eye_b')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建左眉毛
l_mm = bpy.ops.mesh.primitive_cube_add()
bpy.context.object.name = "l_mm"
bpy.context.object.location = (0.25, -0.17, 1.35)
bpy.context.object.scale = (0.05, 0.15, 0.05)
mat = bpy.data.materials.new('mat_l_mm')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 创建右眉毛
r_mm = bpy.ops.mesh.primitive_cube_add()
bpy.context.object.name = "r_mm"
bpy.context.object.location = (0.25, 0.17, 1.35)
bpy.context.object.scale = (0.05, 0.15, 0.05)
mat = bpy.data.materials.new('mat_r_mm')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 添加地面
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -2))  # 这里的size可以根据需要调整

# 设置地面类型为Shadow Catcher
ground = bpy.context.object
ground.name = "Ground"
ground.cycles.is_shadow_catcher = True

# 设置摄影机
bpy.ops.object.camera_add(location=(7, 0.4, 1.36), rotation=(-1.59, 3.14, -1.52))
camera = bpy.context.object
camera.name = "Camera"
bpy.context.scene.camera = camera

# 设置灯光
bpy.ops.object.light_add(type='SPOT', radius=1, location=(0, -2.5, 2.5), rotation=(2.53, 2.62, 0.61))
light = bpy.context.object
light.name = "Light"
light.data.energy = 2000  # 设置光照强度

# 调整渲染设置
bpy.context.scene.render.engine = 'CYCLES'  # 使用Cycles渲染引擎

# 设置渲染分辨率
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50

# 设置渲染输出路径
bpy.context.scene.render.filepath = "/tmp/my_render_output.png"

# 渲染图像
bpy.ops.render.render(write_still=True)

海绵宝宝

图片描述

import bpy

# 清空场景
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

# 创建头部
bpy.ops.mesh.primitive_cube_add(size=3, location=(0, 0, 2.5))
head = bpy.context.object
head.name = "SpongeBob_Head"
head.scale = (1.5, 0.5, 1.4)

mat = bpy.data.materials.new(name="Yellow")# 添加一个新材质
head.data.materials.append(mat)    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 0.9, 0, 1)  # 设置材质的颜色为黄色

# 创建身体
bpy.ops.mesh.primitive_cube_add(size=3, location=(0, 0, -0.5))
body = bpy.context.object
body.name = "SpongeBob_Body"
body.scale = (1.5, 0.5, 0.6)

mat = bpy.data.materials.new(name="Yellow")# 添加一个新材质
body.data.materials.append(mat)    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 0.9, 0, 1)  # 设置材质的颜色为黄色

# 创建鼻子
bpy.ops.object.metaball_add(type='CAPSULE', enter_editmode=False, align='WORLD', location=(0, 1, 2), scale=(0.1, 0.1, 0.1))
nose = bpy.context.object
nose.name = "Nose"

bpy.ops.object.mode_set(mode='EDIT')# 进入编辑模式
nose.scale = (0.2, 0.2, 0.2)  # 调整鼻子的大小
nose.rotation_euler.x = 0.7854  # 45度旋转,X轴方向# 调整鼻子的朝向
nose.rotation_euler.y = 1.5708 # 180度旋转,Y轴方向
nose.rotation_euler.z = 0  # 0度旋转,Z轴方向
bpy.ops.object.mode_set(mode='OBJECT')# 离开编辑模式

mat_nose = bpy.data.materials.new(name="Yellow_Nose")# 添加黄色材质到鼻子上
nose.data.materials.append(mat_nose)
mat_nose.diffuse_color = (1, 0.9, 0, 1)  

# 创建眼睛
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.7, location=(-1, 0.7,2.7))
eye_left = bpy.context.object
eye_left.name = "Eye_Left"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.7, location=(1, 0.7, 2.7))
eye_right = bpy.context.object
eye_right.name = "Eye_Right"

mat_eye = bpy.data.materials.new(name="White_Eye")# 创建白色材质
mat_eye.diffuse_color = (1, 1, 1, 1)  

for eye in [eye_left, eye_right]:# 将白色材质应用于眼睛
    if eye.data.materials:
        eye.data.materials[0] = mat_eye
    else:
        eye.data.materials.append(mat_eye)
        
# 创建大眼球
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(-1, 1,2.7))
Beye_left = bpy.context.object
Beye_left.name = "Beye_Left"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(1, 1, 2.7))
Beye_right = bpy.context.object
Beye_right.name = "Beye_Right"

mat_blue_eye = bpy.data.materials.new(name="Blue_Eye")# 创建蓝色材质
mat_blue_eye.diffuse_color = (0, 0.5, 1, 1)  # 蓝色,完全不透明

for eye in [Beye_left, Beye_right]:# 将蓝色材质应用于大眼球
    if eye.data.materials:
        eye.data.materials[0] = mat_blue_eye
    else:
        eye.data.materials.append(mat_blue_eye)

# 创建小眼球
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.325, location=(-1, 1.2,2.65))
Seye_left = bpy.context.object
Seye_left.name = "Seye_Left"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.325, location=(1, 1.2, 2.65))
Seye_right = bpy.context.object
Seye_right.name = "Seye_Right"

# 创建黑色材质
mat_black_eye = bpy.data.materials.new(name="Black_Eye")
mat_black_eye.diffuse_color = (0, 0, 0, 0.7)  # 黑色,完全不透明

# 将黑色材质应用于小眼球
for eye in [Seye_left, Seye_right]:
    if eye.data.materials:
        eye.data.materials[0] = mat_black_eye
    else:
        eye.data.materials.append(mat_black_eye)

# 创建嘴巴
bpy.ops.mesh.primitive_torus_add(align='WORLD', location=(0, 0, 1.25), rotation=(0, 0, 0), major_radius=1, minor_radius=0.25, abso_major_rad=1.25, abso_minor_rad=0.75)
mouth = bpy.context.object
mouth.name = "Mouth"

mouth.location.y = 0.9# 调整嘴巴的位置和大小
mouth.scale = (0.6, 0.6, 0.6)

# 创建红色材质
mat_red_mouth = bpy.data.materials.new(name="Red_Mouth")
mat_red_mouth.diffuse_color = (0.6, 0, 0, 1)  # 红色,完全不透明

# 将红色材质应用于嘴巴
if mouth.data.materials:
    mouth.data.materials[0] = mat_red_mouth
else:
    mouth.data.materials.append(mat_red_mouth)

# 创建牙齿
bpy.ops.mesh.primitive_cube_add(size=0.4, location=(-0.1, 1.25, 1)) 
tooth1 = bpy.context.object
tooth1.name = "Tooth1"
tooth1.scale = (0.5, 0.5, 1)  # 将长方体拉长,使其成为长方体形状

bpy.ops.mesh.primitive_cube_add(size=0.4, location=(0.1, 1.25, 1))  
tooth2 = bpy.context.object
tooth2.name = "Tooth2"
tooth2.scale = (0.5, 0.5, 1)  # 将长方体拉长,使其成为长方体形状

# 创建白色材质
mat_white_tooth = bpy.data.materials.new(name="White_Tooth")
mat_white_tooth.diffuse_color = (1, 1, 1, 1)  # 白色,完全不透明

# 将白色材质应用于牙齿
for tooth in [tooth1, tooth2]:
    if tooth.data.materials:
        tooth.data.materials[0] = mat_white_tooth
    else:
        tooth.data.materials.append(mat_white_tooth)

# 创建袖子
bpy.ops.mesh.primitive_cone_add(radius1=0.7, radius2=0, depth=1, location=(-2.3, 0, 1))
cone1 = bpy.context.object
cone1.name = "Cone1"

bpy.ops.mesh.primitive_cone_add(radius1=0.7, radius2=0, depth=1, location=(2.3, 0, 1))
cone2 = bpy.context.object
cone2.name = "Cone2"

# 创建白色材质
mat_white_sleeve = bpy.data.materials.new(name="White_Sleeve")
mat_white_sleeve.diffuse_color = (1, 1, 1, 1)  # 白色,完全不透明

# 将白色材质应用于袖子
for cone in [cone1, cone2]:
    if cone.data.materials:
        cone.data.materials[0] = mat_white_sleeve
    else:
        cone.data.materials.append(mat_white_sleeve)

#创建手臂
bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=2, location=(-2.6, 0, -0.5))
arm_left = bpy.context.object
arm_left.name = "Arm_Left"

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=2, location=(2.6, 0,-0.5))
arm_right = bpy.context.object.copy()
arm_right.name = "Arm_Right"

# 创建手
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.3, location=(-2.6, 0, -1.3))
hand_left = bpy.context.object
hand_left.name = "Hand_Left"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.3, location=(2.6, 0, -1.3))
hand_right = bpy.context.object
hand_right.name = "Hand_Right"

# 创建黄色材质
mat_yellow_arm_hand = bpy.data.materials.new(name="Yellow_Arm_Hand")
mat_yellow_arm_hand.diffuse_color = (1, 1, 0, 1)  # 黄色,完全不透明

# 将黄色材质应用于手臂和手
for obj in [arm_left, arm_right, hand_left, hand_right]:
    if obj.data.materials:
        obj.data.materials[0] = mat_yellow_arm_hand
    else:
        obj.data.materials.append(mat_yellow_arm_hand)

# 创建裤子
bpy.ops.mesh.primitive_cylinder_add(radius=0.6, depth=0.5, location=(1, 0, -1.5))
pant_left = bpy.context.object
pant_left.name = "Pant_Left"

bpy.ops.mesh.primitive_cylinder_add(radius=0.6, depth=0.5, location=(-1, 0, -1.5))
pant_right = bpy.context.object.copy()
pant_right.name = "Pant_Right"

# 创建棕色材质
mat_brown_pants = bpy.data.materials.new(name="Brown_Pants")
mat_brown_pants.diffuse_color = (0.6, 0.4, 0.2, 1)  # 棕色,完全不透明

# 将棕色材质应用于裤子
for pant in [pant_left, pant_right]:
    if pant.data.materials:
        pant.data.materials[0] = mat_brown_pants
    else:
        pant.data.materials.append(mat_brown_pants)

# 创建腿
bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=2, location=(1, 0, -2.5))
leg_left = bpy.context.object
leg_left.name = "Leg_Left"

bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=2, location=(-1, 0, -2.5))
leg_right = bpy.context.object.copy()
leg_right.name = "Leg_Right"

# 创建黄色材质
mat_yellow_leg = bpy.data.materials.new(name="Yellow_Leg")
mat_yellow_leg.diffuse_color = (1, 1, 0, 1)  # 黄色,完全不透明

# 将黄色材质应用于腿
for leg in [leg_left, leg_right]:
    if leg.data.materials:
        leg.data.materials[0] = mat_yellow_leg
    else:
        leg.data.materials.append(mat_yellow_leg)

# 创建鞋跟
bpy.ops.mesh.primitive_cube_add(size=0.7, location=(1, 0, -3.5))
heel_left = bpy.context.object
heel_left.name = "Heel_Left"

bpy.ops.mesh.primitive_cube_add(size=0.7, location=(-1, 0, -3.5))
heel_right = bpy.context.object
heel_right.name = "Heel_Right"

# 创建鞋头
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.6, location=(1, 0.6, -3.5))
toe_left = bpy.context.object
toe_left.name = "Toe_Left"

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.6, location=(-1, 0.6, -3.5))
toe_right = bpy.context.object
toe_right.name = "Toe_Right"

# 创建黑色材质
mat_black_shoe = bpy.data.materials.new(name="Black_Shoe")
mat_black_shoe.diffuse_color = (0, 0, 0, 1)  # 黑色,完全不透明

# 将黑色材质应用于鞋跟和鞋头
for obj in [heel_left, heel_right, toe_left, toe_right]:
    if obj.data.materials:
        obj.data.materials[0] = mat_black_shoe
    else:
        obj.data.materials.append(mat_black_shoe)
               

# 创建领带
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0.75, 0.15))
tie_main = bpy.context.object
tie_main.name = "Tie_Main"

# 创建领结
bpy.ops.mesh.primitive_cone_add(radius1=0.3, radius2=0, depth=0.8, location=(0, 0.75, -0.4))
tie_knot = bpy.context.object
tie_knot.name = "Tie_Knot"

# 创建正圆锥
bpy.ops.mesh.primitive_cone_add(radius1=0.3, radius2=0, depth=0.4, location=(0, 0.75, -1))
cone_positive = bpy.context.object
cone_positive.name = "Cone_Positive"

# 创建倒圆锥
bpy.ops.mesh.primitive_cone_add(radius1=0.3, radius2=0, depth=0.4, location=(0, 0.75, -1))
cone_negative = bpy.context.object
cone_negative.name = "Cone_Negative"

cone_negative.rotation_euler.y = 3.1416  # 调整倒圆锥的位置使其倒立

# 创建棕色材质
mat_brown_tie = bpy.data.materials.new(name="Brown_Tie")
mat_brown_tie.diffuse_color = (0.6, 0.4, 0.2, 1)  # 棕色,完全不透明

# 将棕色材质应用于领带
for obj in [tie_main, tie_knot, cone_positive, cone_negative]:
    if obj.data.materials:
        obj.data.materials[0] = mat_brown_tie
    else:
        obj.data.materials.append(mat_brown_tie)
        
# 添加地面
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -4))  # 这里的size可以根据需要调整

# 设置地面类型为Shadow Catcher
ground = bpy.context.object
ground.name = "Ground"
ground.cycles.is_shadow_catcher = True

# 设置摄影机
camera_data = bpy.data.cameras.new('Camera')  # 创建摄影机数据
camera_obj = bpy.data.objects.new('Camera', camera_data)  # 使用摄影机数据创建摄影机对象
bpy.data.collections["Collection"].objects.link(camera_obj)  # 将摄影机对象添加到场景中
camera_data.lens = 50  # 焦距(毫米)
camera_data.sensor_width = 36  # 传感器宽度(毫米)
camera_data.sensor_height = 24  # 传感器高度(毫米)
camera_obj.location =(13.6, 8, 12) # 摄影机位置(X、Y、Z坐标)
camera_obj.rotation_euler = (-2.233, 3.14, -1.047)# 摄影机旋转欧拉角
bpy.context.scene.camera = camera_obj  # 将当前摄影机设置为场景摄影机

# 添加聚光灯
bpy.ops.object.light_add(type='SPOT', radius=3)  # 添加聚光灯
spot_light = bpy.context.object  # 获取新添加的聚光灯对象
spot_light.data.energy = 5000  # 设置聚光灯能量
spot_light.location = (7, -7, 5) # 设置聚光灯位(X、Y、Z坐标)
spot_light.rotation_euler = (1.172 ,0, 0.907)  # 设置聚光灯旋转欧拉角

# 设置渲染分辨率
bpy.context.scene.render.resolution_x = 640  # 渲染分辨率宽度
bpy.context.scene.render.resolution_y = 480  # 渲染分辨率高度
bpy.context.scene.render.resolution_percentage = 50  # 渲染分辨率百分比


# 渲染设置
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = "/tmp/r.png"  # 设置渲染结果保存路径
# 执行渲染
bpy.ops.render.render(write_still=True)

章鱼哥

图片描述


import bpy
from math import pi

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_character():
    # 清除场景
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()
    
    # 创建材质
    materials = {
        'green': create_material('green_head', (0.7, 1, 1, 1)),
        'black': create_material('black_eyeball', (0, 0, 0, 1)),
        'orange': create_material('orange_yifu', (1.2, 0.5, 0.2, 1))
    }
    
    # 创建空物体作为父对象
    empty_obj = bpy.data.objects.new("empty", None)
    empty_obj.name = "tou"
    bpy.data.collections["Collection"].objects.link(empty_obj)
    
    # 创建头部部件
    head_parts = [
        ("head", "uv_sphere", (0, 0, 20.5), (2, 4, 3), 'green'),
        ("xiaba", "uv_sphere", (0, 0, 16), (2, 3, 1), 'green'),
        ("naogan", "cylinder", (0, 0, 17), (1.5, 1.9, 1.5), 'green'),
        ("l_eye", "uv_sphere", (2, -0.8, 19), (0.5, 0.9, 1.2), None),
        ("r_eye", "uv_sphere", (2, 0.8, 19), (0.5, 0.9, 1.2), None),
        ("l_eyeball", "uv_sphere", (2.3, -0.8, 19), (0.3, 0.3, 0.6), 'black'),
        ("r_eyeball", "uv_sphere", (2.3, 0.8, 19), (0.3, 0.3, 0.6), 'black'),
        ("nose", "cone", (2, 0, 18), (0.5, 0.9, 1), 'green'),
        ("nosehead", "uv_sphere", (2, 0, 16.5), (0.5, 1, 1.2), 'green'),
        ("bozi", "cylinder", (0, 0, 13), (0.5, 0.5, 2), 'green')
    ]
    
    # 创建身体部件
    body_parts = [
        ("yifu", "cylinder", (0, 0, 8.5), (2, 2, 4), 'orange'),
        ("duzi", "cylinder", (0, 0, 7), (1.5, 1.5, 4), 'green'),
        ("l_leg", "cylinder", (0, -0.5, -1), (0.5, 0.5, 5), 'green'),
        ("r_leg", "cylinder", (0, 0.5, -1), (0.5, 0.5, 5), 'green')
    ]
    
    # 创建手臂部件
    arm_parts = [
        ("l_arm", "cylinder", (0, -4, 8.5), (0.5, 0.5, 3), 'green', pi/4),
        ("r_arm", "cylinder", (0, 4, 8.5), (0.5, 0.5, 3), 'green', -pi/4),
        ("l_arm2", "cylinder", (0, -4, 11), (0.5, 0.5, 2.5), 'green', -pi/3),
        ("r_arm2", "cylinder", (0, 4, 11), (0.5, 0.5, 2.5), 'green', pi/3),
        ("l_xiuzi", "cylinder", (0, -3, 11.5), (1, 1, 1), 'orange', -pi/3),
        ("r_xiuzi", "cylinder", (0, 3, 11.5), (1, 1, 1), 'orange', pi/3)
    ]
    
    # 创建脚部
    foot_parts = [
        ("l_foot", "uv_sphere", (0, -1, -5), (1.5, 3, 1), 'green', pi/3),
        ("r_foot", "uv_sphere", (0, 1, -5), (1.5, 3, 1), 'green', -pi/3)
    ]
    
    def create_part(name, type, location, scale, material_key=None, rotation=0):
        if type == "uv_sphere":
            bpy.ops.mesh.primitive_uv_sphere_add(location=location)
        elif type == "cylinder":
            bpy.ops.mesh.primitive_cylinder_add(location=location)
        elif type == "cone":
            bpy.ops.mesh.primitive_cone_add(location=location)
            
        obj = bpy.context.object
        obj.name = name
        obj.scale = scale
        if material_key and material_key in materials:
            obj.data.materials.append(materials[material_key])
        if rotation:
            if type == "uv_sphere":
                obj.rotation_euler[2] = rotation
            else:
                obj.rotation_euler[0] = rotation
        obj.parent = empty_obj
    
    # 创建所有部件
    for part in head_parts + body_parts + arm_parts + foot_parts:
        create_part(*part)
    
    # 创建天线
    bpy.ops.mesh.primitive_cylinder_add(location=(2, 0, 21))
    antenna = bpy.context.object
    antenna.scale = (0.1, 0.1, 1.5)
    antenna.rotation_euler[0] = pi/2
    antenna.data.materials.append(materials['black'])
    antenna.parent = empty_obj
    
    return empty_obj

if __name__ == "__main__":
    create_character()

尖耳朵

图片描述


import bpy

def clear_scene():
    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete()

def create_man():
    mat = bpy.data.materials.new('blue')
    mat.use_nodes = True
    color = (0.02, 0.06, 0.5, 1)
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    
    matt = bpy.data.materials.new('rad')
    matt.use_nodes = True      color = (1, 0.74, 0.45, 1)
    matt.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    
    man  = bpy.data.objects.new("man", None)
    
    bpy.data.collections["Collection"].objects.link(man)
    bpy.ops.mesh.primitive_uv_sphere_add()
    face=bpy.context.object
    face.name="face"
    face.scale=(0.87,0.8,0.8)
    face.location=(0,0.287,2.78)
    face.parent=man
    face.data.materials.append(matt)
    
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    head=bpy.context.object
    head.name="head"
    head.scale=(1.2,1,1)
    head.location=(0,0,2.8)
    head.parent=man
    head.data.materials.append(mat)
    
    bpy.ops.mesh.primitive_cone_add()
    ear_l=bpy.context.object
    ear_l.name="ear_l"
    ear_l.scale=(0.55,0.49,0.69)
    ear_l.location=(0.91,0,4)  
    ear_l.rotation_euler=(0,0.425,0,)
    ear_l.parent=man
    ear_l.data.materials.append(mat)
    
    bpy.ops.mesh.primitive_cone_add()
    ear_r=bpy.context.object
    ear_r.name="ear_r"
    ear_r.scale=(0.55,0.49,0.69)
    ear_r.location=(-0.91,0,4)  
    ear_r.rotation_euler=(0,-0.425,0,)
    ear_r.parent=man
    ear_r.data.materials.append(mat)
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    body =bpy.context.object
    body.name="body"
    body.scale=(0.9,0.85,1.05)
    body.location=(0,0,1.14)
    body.parent=man
    body.data.materials.append(mat)
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    hand_l=bpy.context.object
    hand_l.name="hand_l"
    hand_l.scale=(0.46,0.47,0.55)
    hand_l.location=(-1.02,0,1.45)
    hand_l.rotation_euler=(0,0.63,0)
    hand_l.parent=man
    hand_l.data.materials.append(matt)
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    hand_r=bpy.context.object
    hand_r.name="hand_r"
    hand_r.scale=(0.46,0.47,0.55)
    hand_r.location=(1.02,0,1.45)
    hand_r.rotation_euler=(0,-0.63,0)
    hand_r.parent=man
    hand_r.data.materials.append(matt)

 
def create_plane():
    bpy.ops.mesh.primitive_plane_add()
    plane=bpy.context.object
    plane.name="plane"
    plane.scale=(10.0, 10.0, 1.0)
    
    
clear_scene()
create_man()
create_plane()


鸵鸟

图片描述


import bpy
import random
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

ostrich = bpy.data.objects.new("ostrich", None)
bpy.data.collections["Collection"].objects.link(ostrich)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.8, location=(0.75, 0, 7))
head = bpy.context.object
head.parent = ostrich
head.name = "Head"
mat = bpy. data.materials.new ( 'head')
color = (0.9, 0.6, 0.4, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.25, depth=2.3, location=(0.75, 0, 6))
neck = bpy.context.object
neck.parent = ostrich
neck.name = "neck"
mat = bpy. data.materials.new ( 'neck')
color = (0.9, 0.6, 0.4, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=1.5, location=(0, 0, 4))
body = bpy.context.object
body.parent = ostrich
body.name = "Body"
mat = bpy. data.materials.new ( 'body')
color = (1, 0.25, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.9, location=(-0.25, -0.85, 4))
left_arm = bpy.context.object
left_arm.parent = ostrich
left_arm.name = "left_arm"
mat = bpy. data.materials.new ( 'left_arm')
color = (1, 0.25, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.9, location=(-0.25, 0.85, 4))
right_arm = bpy.context.object
right_arm.parent = ostrich
right_arm.name = "right_arm"
mat = bpy. data.materials.new ( 'right_arm')
color = (1, 0.25, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.18, depth=3, location=(0, -0.32, 1.5))
left_leg = bpy.context.object
left_leg.parent = ostrich
left_leg.name = "left_leg"
mat = bpy. data.materials.new ( 'left_leg')
color = (0.9, 0.6, 0.4, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.18, depth=3, location=(0, 0.32, 1.5))
right_leg = bpy.context.object
right_leg.parent = ostrich
right_leg.name = "right_leg"
mat = bpy. data.materials.new ( 'right_leg')
color = (0.9, 0.6, 0.4, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops .mesh.primitive_cone_add(scale = (1, 0.7, 0.8), rotation=(0, 3.14/2, 0), location=(-2, 0, 4))
tail = bpy.context.object
tail.parent = ostrich
tail.name = "tail"
mat = bpy. data.materials.new ( 'tail')
color = (1, 0.25, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops .mesh.primitive_cone_add(scale = (0.3, 0.5, 0.7), rotation=(0, 3.14/2, 0), location=(2, 0, 6.9))
mouth = bpy.context.object
mouth.parent = ostrich
mouth.name = "mouth"
mat = bpy. data.materials.new ( 'mouth')
color = (1, 0.25, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.33, location=(1, -0.52, 7))
l_eye = bpy.context.object
l_eye.parent = ostrich
l_eye.name = "l_eye"
mat = bpy. data.materials.new ( 'l_eye')
color = (1, 1, 1, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.12, location=(1.2, -0.7, 7))
l_eyeball = bpy.context.object
l_eyeball.parent = ostrich
l_eyeball.name = "l_eyeball"
mat = bpy. data.materials.new ( 'l_eyeball')
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.33, location=(1, 0.52, 7))
r_eye = bpy.context.object
r_eye.parent = ostrich
r_eye.name = "r_eye"
mat = bpy. data.materials.new ( 'r_eye')
color = (1, 1, 1, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.12, location=(1.2, 0.7, 7))
r_eyeball = bpy.context.object
r_eyeball.parent = ostrich
r_eyeball.name = "r_eyeball"
mat = bpy. data.materials.new ( 'r_eyeball')
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_plane_add(size=25)
bpy.context.object.location = (5, 0, 0)
mat = bpy. data.materials.new ( 'plane')
color = (0.2, 0.9, 0.3, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

camera = bpy.data.cameras.new('Camera')
camera_obj = bpy.data.objects.new('Camera', camera)
bpy.data.collections["Collection"].objects.link(camera_obj)
camera.lens = 50  
camera.sensor_width = 36  
camera.sensor_height = 24 
camera_obj.location = (23, 18, 12) 
camera_obj.rotation_euler = (1.3, 0, 2.4)  
bpy.context.scene.camera = camera_obj

light_data = bpy.data.lights.new(name="Light", type='POINT')
light = bpy.data.objects.new(name="Light", object_data=light_data)
bpy.context.collection.objects.link(light)
light.location = (4, 4, 11)
light.data.energy = 1000 

ostrich.location = (0, 0, 0) 
ostrich.keyframe_insert(data_path="location", frame=0)  
ostrich.location = (2, 0, 3)
ostrich.keyframe_insert(data_path="location", frame=5) 
ostrich.location = (4, 0, 0)
ostrich.keyframe_insert(data_path="location", frame=10) 
ostrich.location = (4, 0, 0)
ostrich.keyframe_insert(data_path="location", frame=11) 
ostrich.location = (6, 0, 3)
ostrich.keyframe_insert(data_path="location", frame=16) 
ostrich.location = (8, 0, 0)
ostrich.keyframe_insert(data_path="location", frame=21)
ostrich.location = (8, 0, 0)
ostrich.keyframe_insert(data_path="location", frame=22)
ostrich.location = (10, 0, 3)
ostrich.keyframe_insert(data_path="location", frame=27)  
ostrich.location = (12, 0, 0)
ostrich.keyframe_insert(data_path="location", frame=32) 

bpy.context.scene.frame_end = 50
bpy.context.scene.render.resolution_x = 200
bpy.context.scene.render.resolution_y = 150
bpy.context.scene.render.resolution_percentage = 50
bpy.context.scene.render.engine = 'CYCLES'
bpy.ops.render.render(animation=True)



鸭子

图片描述


import bpy
import math

# 删除默认场景中的所有对象
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()

# 创建小鸟的身体
bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
body = bpy.context.object
body.name = "Bird_Body"
body.scale = (1, 0.6, 0.5)  # 调整尺寸以使其看起来更像小鸟身体

# 创建小鸟的头部并为其应用棕色材质
bpy.ops.mesh.primitive_uv_sphere_add(location=(0, 1, 1), radius=0.7)
head = bpy.context.object
head.name = "Bird_Head"

# 设置头部的材质和颜色
def assign_color(obj, color):
    mat = bpy.data.materials.new(name="ColorMaterial")
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    if obj.data.materials:
        obj.data.materials[0] = mat
    else:
        obj.data.materials.append(mat)

# 将头部设置为棕色
assign_color(head, (0.6, 0.4, 0.2, 1))  # 棕色

# 创建小鸟的眼睛并将其嵌入头部
bpy.ops.mesh.primitive_uv_sphere_add(location=(-0.3, 1.3, 1.5), radius=0.2)
eye_left = bpy.context.object
eye_left.name = "Eye_Left"

bpy.ops.mesh.primitive_uv_sphere_add(location=(0.3, 1.3, 1.5), radius=0.2)
eye_right = bpy.context.object
eye_right.name = "Eye_Right"

# 将眼睛设置为黑色
assign_color(eye_left, (0, 0, 0, 1))  # 黑色
assign_color(eye_right, (0, 0, 0, 1))  # 黑色

# 创建小鸟的耳朵并将其放在头上方
bpy.ops.mesh.primitive_cone_add(location=(0, 1.8, 1.2), radius1=0.1, radius2=0.5, depth=0.5)
ear_left = bpy.context.object
ear_left.name = "Ear_Left"

bpy.ops.mesh.primitive_cone_add(location=(0, 1.8, 1.2), radius1=0.1, radius2=0.5, depth=0.5)
ear_right = bpy.context.object
ear_right.name = "Ear_Right"

# 将耳朵设置为棕色
assign_color(ear_left, (0.8, 0.6, 0.4, 1))  # 棕色
assign_color(ear_right, (0.8, 0.6, 0.4, 1))

# 创建小鸟的尾巴
bpy.ops.mesh.primitive_cylinder_add(location=(0, -0.7, 0), radius=0.1, depth=1.5, rotation=(1.5708, 0, 0))
tail = bpy.context.object
tail.name = "Bird_Tail"

# 创建小鸟的腿,均匀分布在四个角的下方
leg_locations = [(-0.5, -0.6, 0.5), (0.5, -0.6, 0.5)]

legs = []
for loc in leg_locations:
    bpy.ops.mesh.primitive_cylinder_add(location=loc, radius=0.1, depth=0.8, rotation=(1.5708, 0, 0))
    leg = bpy.context.object
    leg.name = "Leg_" + str(len(legs) + 1)
    legs.append(leg)

# 设置其他部分的材质和颜色

# 小鸟身体颜色
assign_color(body, (0.6, 0.4, 0.2, 1))  # 棕色

# 小鸟尾巴颜色
assign_color(tail, (0.6, 0.4, 0.2, 1))  # 棕色

# 小鸟腿颜色
for leg in legs:
    assign_color(leg, (0.6, 0.4, 0.2, 1))  # 棕色

# 创建翅膀
bpy.ops.mesh.primitive_plane_add(location=(-1.5, 0, 0), rotation=(0, 0, 0), size=1)
wing_left = bpy.context.object
wing_left.name = "Wing_Left"

bpy.ops.mesh.primitive_plane_add(location=(1.5, 0, 0), rotation=(0, 0, 3.14159), size=1)
wing_right = bpy.context.object
wing_right.name = "Wing_Right"

# 设置翅膀的颜色为黄色
assign_color(wing_left, (1, 1, 0, 1))  # 黄色
assign_color(wing_right, (1, 1, 0, 1))  # 黄色

# 添加地面
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -2))  # 这里的size可以根据需要调整

# 设置地面类型为Shadow Catcher
ground = bpy.context.object
ground.name = "Ground"
ground.cycles.is_shadow_catcher = True

# 创建灯光
bpy.ops.object.light_add(type='POINT', radius=1, location=(0, 0, 5))
light = bpy.context.object
light.name = "Light"
light.data.energy = 3000  # 设置光照强度

# 创建摄影机
bpy.ops.object.camera_add(location=(2, 6, 3))
camera = bpy.context.object
camera.name = "Camera"
bpy.context.scene.camera = camera

# 设置摄影机坐标和旋转角度
camera.location = (6, -6, 5)
camera.rotation_euler = (math.radians(60), 0, math.radians(45))

# 渲染设置
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = "/tmp/render2.png"  # 设置渲染结果保存路径

# 执行渲染
bpy.ops.render.render(write_still=True)

小熊

图片描述


import bpy

bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete()

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.scale[0] = 2
bpy.context.object.scale[1] = 2
bpy.context.object.scale[2] = 2
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 2.5
bpy.context.object.scale[0] = 1.5
bpy.context.object.scale[1] = 1.5
bpy.context.object.scale[2] = 1.5
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 3.8
bpy.context.object.location[1] = 1
bpy.context.object.scale[0] = 0.3
bpy.context.object.scale[1] = 0.5
bpy.context.object.scale[2] = 0.5
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(radius=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 3.8
bpy.context.object.location[1] = -1
bpy.context.object.scale[0] = 0.3
bpy.context.object.scale[1] = 0.5
bpy.context.object.scale[2] = 0.5
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 2.5
bpy.context.object.location[0] = 1
bpy.context.object.scale[0] = 0.7
bpy.context.object.scale[1] = 0.7
bpy.context.object.scale[2] = 0.7
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.051, 0.030, 0.016, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 2.7
bpy.context.object.location[0] = 1.6
bpy.context.object.scale[0] = 0.2
bpy.context.object.scale[1] = 0.3
bpy.context.object.scale[2] = 0.3
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.241, 0.146, 0.087, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 3.3
bpy.context.object.location[1] = 0.5
bpy.context.object.location[0] = 1.2
bpy.context.object.scale[0] = 0.15
bpy.context.object.scale[1] = 0.15
bpy.context.object.scale[2] = 0.15
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0, 0, 0, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = 3.3
bpy.context.object.location[1] = -0.5
bpy.context.object.location[0] = 1.2
bpy.context.object.scale[0] = 0.15
bpy.context.object.scale[1] = 0.15
bpy.context.object.scale[2] = 0.15
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0, 0, 0, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = -1.5
bpy.context.object.location[1] = 0.8
bpy.context.object.scale[0] = 0.9
bpy.context.object.scale[1] = 0.8
bpy.context.object.scale[2] = 0.9
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.location[2] = -1.5
bpy.context.object.location[1] = -0.8
bpy.context.object.scale[0] = 0.9
bpy.context.object.scale[1] = 0.8
bpy.context.object.scale[2] = 0.9
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.rotation_euler[0] = 0.785398
bpy.context.object.location[2] = 0.3
bpy.context.object.location[1] = 1.4
bpy.context.object.scale[0] = 0.5
bpy.context.object.scale[1] = 1
bpy.context.object.scale[2] = 1.5
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_uv_sphere_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.context.object.rotation_euler[0] = -0.785398
bpy.context.object.location[2] = 0.3
bpy.context.object.location[1] = -1.4
bpy.context.object.scale[0] = 0.5
bpy.context.object.scale[1] = 1
bpy.context.object.scale[2] = 1.5
bpy.ops.object.select_all(action='DESELECT')  
bpy.context.object.select_set(True)  
mat = bpy.data.materials.new(name="ColoredMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (0.135, 0.053, 0.027, 1) 
bpy.context.object.data.materials.append(mat)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.collection.objects.link(camera_obj)
camera.lens = 80  # Focal length in millimeters
camera.sensor_width = 80  # Sensor width in millimeters
camera.sensor_height = 40  # Sensor height in millimeters
camera_obj.location = (10.6, 6, 10.5)  # X, Y, Z coordinates
camera_obj.rotation_euler = (-128*0.0174444444,180*0.0174444444,-60*0.0174444444)
bpy.context.scene.camera = camera_obj

light_name = "MyLight"  
if light_name not in bpy.data.objects:  
    light_data = bpy.data.lights.new(name=light_name + "_data", type='POINT')  
    light_object = bpy.data.objects.new(name=light_name, object_data=light_data)  
    bpy.context.collection.objects.link(light_object)  
  
light = bpy.data.objects[light_name]  

light.location = (14, 5, 8) 
light.data.energy = 8000
light.data.color = (1, 1, 0.8)

scene = bpy.context.scene    
scene.render.engine = 'CYCLES'    
scene.render.resolution_x = 640    
scene.render.resolution_y = 480    
scene.render.filepath = "/tmp/rendered_image.png"    
bpy.ops.render.render(write_still=True)


星之卡比

图片描述


import bpy

bpy.ops.object.select_all(action="SELECT") # 选择所有物体
bpy.ops.object.delete() # 删除选定的物体

body = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "body"
bpy.context.object.location = (0, 0, 0)
bpy.context.object.scale = (4.0, 4.0, 4.0)
mat = bpy.data.materials.new('mat_body')
    mat.use_nodes = True
color = (1, 0.619, 0.713, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_leg = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_leg"
bpy.context.object.location = (0.4, 3.2, 0)
bpy.context.object.scale = (1.5, 2.5, 1.5)
mat = bpy.data.materials.new('mat_leg')
    mat.use_nodes = True
color = (1, 0.619, 0.713, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_leg = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_leg"
bpy.context.object.location = (0.4, -3.2, 0)
bpy.context.object.scale = (1.5, 2.5, 1.5)
mat = bpy.data.materials.new('mat_leg')
    mat.use_nodes = True
color = (1, 0.619, 0.713, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_foot = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_foot"
bpy.context.object.location = (1.5, 2.5, -2.6)
bpy.context.object.rotation_euler[1] = 1
bpy.context.object.scale = (1.5, 1.5, 1.8)
mat = bpy.data.materials.new('mat_foot')
    mat.use_nodes = True
color = (0.886, 0.098, 0.309, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_foot = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_foot"
bpy.context.object.location = (1.5, -2.5, -2.6)
bpy.context.object.rotation_euler[1] = 1
bpy.context.object.scale = (1.5, 1.5, 1.8)
mat = bpy.data.materials.new('mat_foot')
    mat.use_nodes = True
color = (0.886, 0.098, 0.309, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eye"
bpy.context.object.location = (3.6, 1.0, 1.0)
bpy.context.object.scale = (0.4, 0.4, 1.0)
mat = bpy.data.materials.new('mat_eye')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_eye = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eye"
bpy.context.object.location = (3.6, -1.0, 1.0)
bpy.context.object.scale = (0.4, 0.4, 1.0)
mat = bpy.data.materials.new('mat_eye')
    mat.use_nodes = True
color = (0, 0, 0, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_eyelittle = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_eyelittle"
bpy.context.object.location = (3.8, 1.0, 1.4)
bpy.context.object.scale = (0.25, 0.25, 0.5)
mat = bpy.data.materials.new('mat_eyelittle')
    mat.use_nodes = True
color = (1, 1, 1, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_eyelittle = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_eyelittle"
bpy.context.object.location = (3.8, -1.0, 1.4)
bpy.context.object.scale = (0.25, 0.25, 0.5)
mat = bpy.data.materials.new('mat_eyelittle')
    mat.use_nodes = True
color = (1, 1, 1, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_rouge = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_rouge"
bpy.context.object.location = (3.6, 1.8, 0)
bpy.context.object.scale = (0.3, 0.6, 0.4)
mat = bpy.data.materials.new('mat_rouge')
    mat.use_nodes = True
color = (0.933, 0.415, 0.509, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_rouge = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_rouge"
bpy.context.object.location = (3.6, -1.8, 0)
bpy.context.object.scale = (0.3, 0.6, 0.4)
mat = bpy.data.materials.new('mat_rouge')
    mat.use_nodes = True
color = (0.933, 0.415, 0.509, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

mouse = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "mouse"
bpy.context.object.location = (3.9, 0,-0.6)
bpy.context.object.scale = (0.3, 0.5, 0.4)
mat = bpy.data.materials.new('mat_mouse')
    mat.use_nodes = True
color = (1, 0.2, 0.3, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

r_bow = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "r_bow"
bpy.context.object.location = (-0.3, 1.5, 2.6)
bpy.context.object.rotation_euler[1] = 0.349
bpy.context.object.rotation_euler[2] = 1.4
bpy.context.object.scale = (1.5, 1.5, 3.0)
mat = bpy.data.materials.new('mat_row')
    mat.use_nodes = True
color = (0.98, 0.199, 0.199, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

l_bow = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_bow"
bpy.context.object.location = (-0.3, -1.5, 2.6)
bpy.context.object.rotation_euler[1] = 0.349
bpy.context.object.rotation_euler[2] = 4.8
bpy.context.object.scale = (1.5, 1.5, 3.0)
mat = bpy.data.materials.new('mat_row')
    mat.use_nodes = True
color = (0.98, 0.199, 0.199, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

m_bow = bpy.ops.mesh.primitive_uv_sphere_add()
bpy.context.object.name = "l_bow"
bpy.context.object.location = (0.0, 0.0, 3.9)
bpy.context.object.scale = (1.0, 1.0, 1.0)
mat = bpy.data.materials.new('mat_row')
    mat.use_nodes = True
color = (0.98, 0.199, 0.199, 1)
mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
bpy.context.object.data.materials.append(mat)

# 添加地面
bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -4))  # 这里的size可以根据需要调整

# 设置地面类型为Shadow Catcher
ground = bpy.context.object
ground.name = "Ground"
ground.cycles.is_shadow_catcher = True

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 50  # Focal length in millimeters
camera.sensor_width = 36  # Sensor width in millimeters
camera.sensor_height = 24  # Sensor height in millimeters
camera_obj.location = (29.57, -3.37, 7.35)  # X, Y, Z coordinates
camera_obj.rotation_euler = (1.38, 0.014, 1.44)
bpy.context.scene.camera = camera_obj

# 添加聚光灯
bpy.ops.object.light_add(type='SPOT', radius=3)  # 添加聚光灯
spot_light = bpy.context.object  # 获取新添加的聚光灯对象
spot_light.data.energy = 8000  # 设置聚光灯能量
spot_light.location = (13.5, -10, 12) # 设置聚光灯位(X、Y、Z坐标)
spot_light.rotation_euler = (0.675 ,0.749, 12.79)  # 设置聚光灯旋转欧拉角

# 设置渲染分辨率
bpy.context.scene.render.resolution_x = 1280  # 渲染分辨率宽度
bpy.context.scene.render.resolution_y = 880  # 渲染分辨率高度
bpy.context.scene.render.resolution_percentage = 100  # 渲染分辨率百分比

# 设置渲染引擎(例如CYCLES、BLENDER_EEVEE)
bpy.context.scene.render.engine = 'CYCLES'

# 设置输出文件路径
bpy.context.scene.render.filepath = './tmp/my_render_output1.png'

# 渲染当前视图
bpy.ops.render.render(write_still=True)

图片描述


import bpy
import math

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_cat():
    # 清空场景
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

    # 创建身体
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0.5, 1))
    body = bpy.context.active_object
    body.name = "body"
    body.scale = (1, 2, 1)
    body.data.materials.append(create_material('mat_body', (0.5, 0.3, 0, 1)))

    # 创建头部
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 1.7))
    head = bpy.context.active_object
    head.name = "head"
    head.data.materials.append(create_material('mat_head', (0.5, 0.3, 0, 1)))

    # 创建眼睛
    eye_mat = create_material('mat_eye', (0, 0, 0, 1))
    
    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=(-0.2, -0.4, 0.1))
    left_eye = bpy.context.active_object
    left_eye.name = "left_eye"
    left_eye.data.materials.append(eye_mat)
    left_eye.parent = head

    bpy.ops.mesh.primitive_uv_sphere_add(radius=0.1, location=(0.2, -0.4, 0.1))
    right_eye = bpy.context.active_object
    right_eye.name = "right_eye"
    right_eye.data.materials.append(eye_mat)
    right_eye.parent = head

    # 创建耳朵
    ear_mat = create_material('mat_ear', (0.5, 0.3, 0, 1))
    
    bpy.ops.mesh.primitive_cone_add(radius1=0.2, radius2=0.0, depth=0.35, location=(-0.2, 0, 0.45))
    left_ear = bpy.context.active_object
    left_ear.name = "left_ear"
    left_ear.rotation_euler = (0, 0, math.radians(30))
    left_ear.data.materials.append(ear_mat)
    left_ear.parent = head

    bpy.ops.mesh.primitive_cone_add(radius1=0.2, radius2=0.0, depth=0.35, location=(0.2, 0, 0.45))
    right_ear = bpy.context.active_object
    right_ear.name = "right_ear"
    right_ear.rotation_euler = (0, 0, math.radians(-30))
    right_ear.data.materials.append(ear_mat)
    right_ear.parent = head

    # 创建胡须
    beard_length = 0.5
    beard_thickness = 0.02

    for i in range(3):
        bpy.ops.mesh.primitive_cylinder_add(
            radius=beard_thickness, 
            depth=beard_length,
            location=(-0.3, -0.4, -0.3 + i * 0.1)
        )
        left_beard = bpy.context.active_object
        left_beard.name = f"LeftBeard{i + 1}"
        left_beard.rotation_euler = (0, math.radians(85 + i * 5), 0)
        left_beard.parent = head

        bpy.ops.mesh.primitive_cylinder_add(
            radius=beard_thickness, 
            depth=beard_length,
            location=(0.3, -0.4, -0.3 + i * 0.1)
        )
        right_beard = bpy.context.active_object
        right_beard.name = f"RightBeard{i + 1}"
        right_beard.rotation_euler = (0, math.radians(95 + i * -5), 0)
        right_beard.parent = head

    # 创建腿
    leg_mat = create_material('mat_leg', (0.5, 0.3, 0, 1))
    leg_radius = 0.1
    leg_depth = 1

    leg_positions = [
        (-0.3, -0.25, -0.5, "left_front_leg"),
        (0.3, -0.25, -0.5, "right_front_leg"),
        (-0.3, 0.25, -0.5, "left_back_leg"),
        (0.3, 0.25, -0.5, "right_back_leg")
    ]

    for pos in leg_positions:
        bpy.ops.mesh.primitive_cylinder_add(
            radius=leg_radius, 
            depth=leg_depth, 
            location=pos[:3]
        )
        leg = bpy.context.active_object
        leg.name = pos[3]
        leg.data.materials.append(leg_mat)
        leg.parent = body

    # 创建尾巴
    curve = bpy.data.curves.new(name="CatTailCurve", type='CURVE')
    curve.dimensions = '3D'
    spline = curve.splines.new(type='BEZIER')
    spline.bezier_points.add(3)

    tail_points = [
        (0, 1.5, 1),
        (0, 2.1, 1.3),
        (0, 2.4, 1.9),
        (0, 2.8, 2.1)
    ]

    for i, point in enumerate(tail_points):
        spline.bezier_points[i].co = point
        spline.bezier_points[i].handle_left_type = 'AUTO'
        spline.bezier_points[i].handle_right_type = 'AUTO'

    curve_obj = bpy.data.objects.new("tail", curve)
    bpy.context.collection.objects.link(curve_obj)
    curve.bevel_depth = 0.1

    # 创建尾巴材质
    tail_mat = create_material('mat_tail', (0.5, 0.3, 0, 1))
    curve_obj.data.materials.append(tail_mat)

    # 创建主容器并设置父级关系
    cat = bpy.data.objects.new("cat", None)
    bpy.data.collections["Collection"].objects.link(cat)
    head.parent = cat
    body.parent = cat
    curve_obj.parent = cat
    return cat

if __name__ == "__main__":
    cat = create_cat()

蛋仔

图片描述


import bpy

def create_material(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    mat.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color
    return mat

def create_egg():
    # 清空场景
    bpy.ops.object.select_all(action="SELECT")
    bpy.ops.object.delete()
    
    # 创建空对象作为父级
    egg = bpy.data.objects.new("egg", None)
    bpy.data.collections["Collection"].objects.link(egg)
    
    # 创建各个部件
    parts = {}
    
    # 身体
    bpy.ops.mesh.primitive_uv_sphere_add()
    parts['body'] = bpy.context.object
    parts['body'].name = "body"
    parts['body'].location = (0, 0, 0)
    parts['body'].scale = (5.0, 5.0, 5.0)
    parts['body'].data.materials.append(create_material('mat_body', (1, 0.779, 0.012, 1)))
    
    # 脸部
    bpy.ops.mesh.primitive_uv_sphere_add()
    parts['face'] = bpy.context.object
    parts['face'].name = "face"
    parts['face'].location = (1.5, 0, 0.5)
    parts['face'].scale = (3.85, 3.9, 3.9)
    parts['face'].data.materials.append(create_material('mat_face', (1, 0.829, 0.726, 1)))
    
    # 手部
    hand_configs = [
        ("r_hand", (0.4, 3.8, 0), (1.6, 4.5, 1.6), (0, 0.35, 0)),
        ("l_hand", (0.4, -3.8, 0.5), (1.6, 4.5, 1.6), (-0.6, 0.35, 0))
    ]
    
    for name, loc, scale, rot in hand_configs:
        bpy.ops.mesh.primitive_uv_sphere_add()
        hand = bpy.context.object
        hand.name = name
        hand.location = loc
        hand.scale = scale
        hand.rotation_euler = rot
        hand.data.materials.append(create_material('mat_hand', (1, 0.779, 0.012, 1)))
        parts[name] = hand
    
    # 脚部
    foot_configs = [
        ("r_foot", (4.5, 2.5, -2.6), (1.5, 1.6, 2.0), (0, 0.35, 0.87)),
        ("l_foot", (-0.5, -2.8, -4.5), (1.6, 1.8, 2.0), (-0.5, 0, -0.6))
    ]
    
    for name, loc, scale, rot in foot_configs:
        bpy.ops.mesh.primitive_uv_sphere_add()
        foot = bpy.context.object
        foot.name = name
        foot.location = loc
        foot.scale = scale
        foot.rotation_euler = rot
        foot.data.materials.append(create_material('mat_foot', (1, 1, 1, 1)))
        parts[name] = foot
    
    # 头顶装饰
    bpy.ops.mesh.primitive_cylinder_add()
    parts['kaka'] = bpy.context.object
    parts['kaka'].name = "kaka"
    parts['kaka'].location = (0, 0, 5.5)
    parts['kaka'].scale = (0.4, 0.4, 1)
    parts['kaka'].data.materials.append(create_material('mat_kaka', (1, 0.779, 0.012, 1)))
    
    bpy.ops.mesh.primitive_uv_sphere_add()
    parts['jiujiu'] = bpy.context.object
    parts['jiujiu'].name = "jiujiu"
    parts['jiujiu'].location = (0, 0, 6.5)
    parts['jiujiu'].scale = (0.9, 0.9, 0.9)
    parts['jiujiu'].data.materials.append(create_material('mat_jiujiu', (1, 0.779, 0.012, 1)))
    
    # 眼睛
    eye_configs = [
        ("r_eye", (4.7, 1.3, 2.0), (0.6, 0.6, 0.6), (1, 1, 1, 1)),
        ("l_eye", (4.7, -1.3, 2.0), (0.6, 0.6, 0.6), (1, 1, 1, 1)),
        ("r_eyelittle", (5.0, 1.3, 2.1), (0.4, 0.4, 0.4), (0, 0, 0, 1)),
        ("l_eyelittle", (5.0, -1.3, 2.1), (0.4, 0.4, 0.4), (0, 0, 0, 1))
    ]
    
    for name, loc, scale, color in eye_configs:
        bpy.ops.mesh.primitive_uv_sphere_add()
        eye = bpy.context.object
        eye.name = name
        eye.location = loc
        eye.scale = scale
        eye.data.materials.append(create_material(f'mat_{name}', color))
        parts[name] = eye
    
    # 嘴巴
    bpy.ops.mesh.primitive_uv_sphere_add()
    parts['mouth'] = bpy.context.object
    parts['mouth'].name = "mouth"
    parts['mouth'].location = (5.3, 0, 0.5)
    parts['mouth'].scale = (0.2, 0.4, 0.6)
    parts['mouth'].data.materials.append(create_material('mat_mouth', (1, 0.2, 0.3, 1)))

    # 选择所有对象
    bpy.ops.object.select_all(action='DESELECT')
    for obj in bpy.data.objects:
        if obj != egg and obj.type == 'MESH':
            obj.select_set(True)
            obj.parent = egg
    return egg

def setup_scene():
    # 创建地面
    bpy.ops.mesh.primitive_plane_add(size=20, location=(0, 0, -6))
    ground = bpy.context.object 
    ground.name = "Ground" 
    ground.cycles.is_shadow_catcher = True
    
    # 设置相机
    camera = bpy.data.cameras.new('MyCamera')  
    camera_obj = bpy.data.objects.new('CameraObj', camera) 
    bpy.context.scene.collection.objects.link(camera_obj)  
    camera.lens = 50  
    camera.sensor_width = 36 
    camera.sensor_height = 24 
    camera_obj.location = (29.57, -3, 7.35)   
    camera_obj.rotation_euler = (1.38, 0.014, 1.44)   
    bpy.context.scene.camera = camera_obj
    
    # 设置灯光
    bpy.ops.object.light_add(type='SPOT', radius=1)
    spot_light = bpy.context.object 
    spot_light.data.energy = 8000
    spot_light.location = (13.5, 10, 10)
    spot_light.rotation_euler = (-0.6, 0.749, 12.5)
    
    # 设置渲染参数
    bpy.context.scene.render.resolution_x = 640
    bpy.context.scene.render.resolution_y = 480
    bpy.context.scene.render.resolution_percentage = 50
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.render.filepath = '/tmp/render2.png'

if __name__ == "__main__":
    egg = create_egg()
    setup_scene()
    bpy.ops.render.render(write_still=True)

马里奥的蘑菇

图片描述


import bpy
import bmesh
import math

if bpy.context.object and bpy.context.object.mode != 'OBJECT':
    bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action="SELECT") 
bpy.ops.object.delete()

bpy.ops.mesh.primitive_cube_add()
bpy.context.object.name = "cube"
cube = bpy.context.object
mesh = cube.data
cube.location.z -= 0.16
custom_material = bpy.data.materials.new(name="Custom_Color_Material")
    material.use_nodes = True      custom_material.use_nodes = True  custom_material.diffuse_color = (1, 0.746, 0.352, 1)  
if cube.data.materials:
    cube.data.materials.clear()
cube.data.materials.append(custom_material)

bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(mesh)
edges = [e for e in bm.edges]
for edge in edges:
    edge.select = True

bmesh.ops.bevel(
    bm, 
    geom=edges,   
    offset=0.95,     
    segments=11,     
    profile=0.66,    
    affect='EDGES'   
)
bmesh.update_edit_mesh(mesh)
bpy.ops.object.mode_set(mode='OBJECT')


base_obj = bpy.context.object
if base_obj is None:
    raise ValueError("请先选择一个物体")

bpy.ops.mesh.primitive_uv_sphere_add(segments=46, ring_count=32, radius=1.83)
sphere = bpy.context.object
sphere.name = "Modified_Sphere"
sphere.location = (0, 0, base_obj.dimensions.z / 2 -0.4)  
sphere.scale.z = 0.93
sphere.location.y -= 0.3
sphere.rotation_euler.x += math.radians(18)

red_material = bpy.data.materials.new(name="Red_Material")
    material.use_nodes = True      red_material.use_nodes = True  red_material.diffuse_color = (1, 0, 0, 1)  
if sphere.data.materials:
    sphere.data.materials.clear()
sphere.data.materials.append(red_material)

bpy.ops.object.mode_set(mode='EDIT')
mesh = sphere.data
bm = bmesh.from_edit_mesh(mesh)

min_z = min(v.co.z for v in bm.verts)  
max_z = max(v.co.z for v in bm.verts)  
threshold_z = min_z + (max_z - min_z) * 0.28
falloff_distance = (max_z - threshold_z) * 1
shrink_factor = 0.2

for vert in bm.verts:
    if vert.co.z > threshold_z:  
        influence = (vert.co.z - threshold_z) / falloff_distance  
        shrink_amount = 1 - shrink_factor * influence  
        vert.co.x *= shrink_amount
        vert.co.y *= shrink_amount
        
bmesh.update_edit_mesh(mesh)
bpy.ops.object.mode_set(mode='OBJECT')


bpy.ops.object.mode_set(mode='EDIT')


mesh = sphere.data
bm = bmesh.from_edit_mesh(mesh)

min_z = min(v.co.z for v in bm.verts)
bottom_verts = [v for v in bm.verts if v.co.z == min_z]

falloff_distance = 0.4

for vert in bm.verts:
    distance = abs(vert.co.z - min_z) 
    if distance < falloff_distance:
        influence = 1- (distance / falloff_distance) 
        vert.co.z += 0.1 * influence  
        vert.co.x *= 1 - 0.9 * influence 
        vert.co.y *= 1 - 0.9 * influence  

bottom_edges = [e for e in bm.edges if all(v.co.z == min_z for v in e.verts)]
bmesh.ops.subdivide_edges(bm, edges=bottom_edges, cuts=5)

bmesh.update_edit_mesh(mesh)
bpy.ops.object.mode_set(mode='OBJECT')



bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(sphere.data)

min_z = min(v.co.z for v in bm.verts)
max_z = max(v.co.z for v in bm.verts)
z_threshold = min_z + (max_z - min_z) * 0.5 

falloff_radius = 1.4
bend_strength = 0.85
sharpness = 5    

def calculate_influence(distance):
    """ 计算软选择影响力 """
    return max(0, (1 - distance / falloff_radius) ** sharpness)

for v in bm.verts:
    if v.co.z > z_threshold:
        continue  
    
    distance = abs(v.co.z - min_z)
    influence = calculate_influence(distance)

    v.co.z += bend_strength * influence * (1 - abs(v.co.x) / falloff_radius)

bmesh.update_edit_mesh(sphere.data)
bpy.ops.object.mode_set(mode='OBJECT')


bpy.ops.object.mode_set(mode='OBJECT')

original_cube = bpy.data.objects['cube']
bpy.ops.object.select_all(action='DESELECT')
original_cube.select_set(True)
bpy.context.view_layer.objects.active = original_cube

bpy.ops.object.duplicate()
eye1 = bpy.context.active_object
eye1.name = "eye1"

bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)

eye1.scale = (0.08, 0.09, 0.24) 
eye1.location.x += 0.32
eye1.location.y += 0.95
eye1.location.z -= 0.33

bpy.context.view_layer.update()

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
eye1 = bpy.data.objects['eye1']
eye1.select_set(True)
bpy.context.view_layer.objects.active = eye1

bpy.ops.object.duplicate()
eye2 = bpy.context.active_object
eye2.name = "eye2"

eye2.scale.x *= -1  
eye2.location.x = -eye1.location.x  

bpy.context.view_layer.update()


bpy.ops.object.mode_set(mode='OBJECT')

def duplicate_and_adjust(eye_name, new_name):
    bpy.ops.object.select_all(action='DESELECT')
    eye = bpy.data.objects[eye_name]
    eye.select_set(True)
    bpy.context.view_layer.objects.active = eye

    bpy.ops.object.duplicate()
    new_eye = bpy.context.active_object
    new_eye.name = new_name

    new_eye.scale = (0.05, 0.1, 0.08)  

    new_eye.location.z += 0.12

    bpy.context.view_layer.update()

duplicate_and_adjust("eye1", "eye1_copy")

duplicate_and_adjust("eye2", "eye2_copy")


bpy.ops.object.mode_set(mode='OBJECT')


def apply_color(obj_name, color):
    bpy.ops.object.select_all(action='DESELECT')
    obj = bpy.data.objects[obj_name]
    obj.select_set(True)
    bpy.context.view_layer.objects.active = obj

    material = bpy.data.materials.new(name=f"{obj_name}_Material")
    material.use_nodes = True      material.node_tree.nodes["Principled BSDF"].inputs['Base Color'].default_value = color 

    if obj.data.materials:
        obj.data.materials.clear()
    obj.data.materials.append(material)

    bpy.context.view_layer.update()

apply_color("eye1", (0.05, 0.01, 0, 1)) 
apply_color("eye2", (0.05, 0.01, 0, 1)) 

apply_color("eye1_copy", (1, 1, 1, 1)) 
apply_color("eye2_copy", (1, 1, 1, 1))  



import bpy
import math
import mathutils


modified_sphere = bpy.data.objects.get("Modified_Sphere")
if not modified_sphere:
    raise ValueError("未找到 Modified_Sphere,请确保它已创建!")


offset = 1.4  
radius = 0.9 
scale_factor = (1, 1, 0.3)  

directions = [
    mathutils.Vector((0, 1, 0)),  # 前
    mathutils.Vector((0, -1, -0.5)), # 后
    mathutils.Vector((1.05, 0, -0.3)),  # 右
    mathutils.Vector((-1.05, 0, -0.3)), # 左
]

for i, direction in enumerate(directions):
    pos = modified_sphere.location + (direction * offset)
    pos.z += 0.48

    normal = direction.normalized()  
    
    bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=pos)
    sphere = bpy.context.object
    sphere.name = f"Eye_{i+1}" 
    
    rot_quat = normal.to_track_quat('Z', 'Y')  
    sphere.rotation_euler = rot_quat.to_euler()
    sphere.rotation_euler.x += math.radians(-27) 

    sphere.scale = scale_factor

    mat = bpy.data.materials.get("WhiteMaterial")
    if not mat:
        mat = bpy.data.materials.new(name="WhiteMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 1, 1, 1)  
    
    if len(sphere.data.materials) == 0:
        sphere.data.materials.append(mat)
    else:
        sphere.data.materials[0] = mat


# 头顶方向
top_direction = mathutils.Vector((0, 0, 1))  

# 计算位置
top_pos = modified_sphere.location + (top_direction * offset)
top_pos.z -= 0.05  # 让它更靠上
top_pos.y -= 0.5  

# 创建头顶的白色圆形
bpy.ops.mesh.primitive_uv_sphere_add(radius=radius, location=top_pos)
top_sphere = bpy.context.object
top_sphere.name = "Top_White_Spot"

# 计算旋转(稍微向后倾斜)
top_rot_quat = top_direction.to_track_quat('Z', 'Y')  
top_sphere.rotation_euler = top_rot_quat.to_euler()
top_sphere.rotation_euler.x += math.radians(-20)  # 向后倾斜 20°



# 调整形状
top_sphere.scale = scale_factor

# 赋予白色材质
if not mat:
    mat = bpy.data.materials.new(name="WhiteMaterial")
    mat.use_nodes = True
mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (1, 1, 1, 1)

if len(top_sphere.data.materials) == 0:
    top_sphere.data.materials.append(mat)
else:
    top_sphere.data.materials[0] = mat

mushroom = bpy.data.objects.new("Mario Mushroom", None)
bpy.context.collection.objects.link(mushroom)


for obj in bpy.data.objects:
    if obj.type == 'MESH': 
        obj.parent = mushroom

mushroom.location = (0, 0, 0)

print("所有对象已封装进 Mario Mushroom")



# ---- 新增:灯光、摄像机与渲染 ----

# 确保在对象模式
bpy.ops.object.mode_set(mode='OBJECT')

# 创建灯光(右上方)
bpy.ops.object.light_add(type='POINT', location=(3, 5, 2))
light = bpy.context.object
light.data.energy = 1500  # 设置灯光强度

# 使灯光朝向物体
track_to = light.constraints.new(type='TRACK_TO')
track_to.target = bpy.data.objects['Mario Mushroom']  # 指向封装的对象
track_to.track_axis = 'TRACK_NEGATIVE_Z'  # 灯光朝向物体
track_to.up_axis = 'UP_Y'

# 创建第二盏灯(左下方)
bpy.ops.object.light_add(type='POINT', location=(-3, -5, -2))
light2 = bpy.context.object
light2.data.energy = 600  # 设置灯光强度

# 使第二盏灯朝向物体
track_to2 = light2.constraints.new(type='TRACK_TO')
track_to2.target = bpy.data.objects['Mario Mushroom']  # 指向封装的对象
track_to2.track_axis = 'TRACK_NEGATIVE_Z'  # 灯光朝向物体
track_to2.up_axis = 'UP_Y'  # 设置上方向


import bpy
import math
import os

# 获取蘑菇对象
ball = bpy.data.objects['Mario Mushroom']
# 确保在对象模式
bpy.ops.object.mode_set(mode='OBJECT')
# 删除所有现有的关键帧
ball.animation_data_clear()
# 创建新的动画数据
if ball.animation_data is None:
    ball.animation_data_create()
    
# 设置动画的总帧数 - 增加一点帧数来容纳短暂停顿
frame_start = 1
frame_end = 50  # 增加5帧用于短暂停顿

# 记录初始位置
original_z = ball.location.z

# 1. 蓄力阶段 (帧 1-8)
for frame in range(frame_start, 8):
    bpy.context.scene.frame_set(frame)
    
    factor = math.sin((frame - frame_start) / 7 * math.pi/2) * 0.3
    ball.scale = (1 + factor, 1 + factor, 1 - factor)
    ball.location.z = original_z
    
    ball.keyframe_insert(data_path="scale")
    ball.keyframe_insert(data_path="location")

# 2. 起跳阶段 (帧 8-20)
jump_height = 5
for frame in range(8, 20):
    bpy.context.scene.frame_set(frame)
    
    progress = (frame - 8) / 12
    height = jump_height * math.sin(progress * math.pi/2)
    
    ball.scale = (1, 1, 1)
    ball.location.z = original_z + height
    
    ball.keyframe_insert(data_path="scale")
    ball.keyframe_insert(data_path="location")

# 3. 下落阶段 (帧 20-35)
for frame in range(20, 35):
    bpy.context.scene.frame_set(frame)
    
    progress = (frame - 20) / 15
    height = jump_height * math.cos(progress * math.pi/2)
    
    ball.location.z = original_z + height
    ball.scale = (1, 1, 1)
    
    ball.keyframe_insert(data_path="location")
    ball.keyframe_insert(data_path="scale")

# 4. 落地缓冲阶段 (帧 35-45)
for frame in range(35, 45):
    bpy.context.scene.frame_set(frame)
    
    progress = (frame - 35) / 10
    squash = math.sin(progress * math.pi) * 0.3
    
    ball.scale = (1 + squash, 1 + squash, 1 - squash)
    ball.location.z = original_z
    
    ball.keyframe_insert(data_path="scale")
    ball.keyframe_insert(data_path="location")

# 5. 短暂停顿阶段 (帧 45-50):添加5帧的短暂停顿
for frame in range(45, frame_end + 1):
    bpy.context.scene.frame_set(frame)
    
    ball.scale = (1, 1, 1)
    ball.location.z = original_z
    
    ball.keyframe_insert(data_path="scale")
    ball.keyframe_insert(data_path="location")

# 设置循环动画
action = ball.animation_data.action
if action:
    for fc in action.fcurves:
        for kf in fc.keyframe_points:
            kf.interpolation = 'BEZIER'
            kf.handle_left_type = 'AUTO'
            kf.handle_right_type = 'AUTO'
        fc.modifiers.new('CYCLES')

# 设置场景的起始和结束帧
bpy.context.scene.frame_start = frame_start
bpy.context.scene.frame_end = frame_end

# 返回到起始帧
bpy.context.scene.frame_set(frame_start)  

# 删除现有的摄像机
for obj in bpy.data.objects:
    if obj.type == 'CAMERA':
        bpy.data.objects.remove(obj, do_unlink=True)

# 创建新的摄像机并设置固定位置
bpy.ops.object.camera_add(location=(0, 20, -1))
camera = bpy.context.object
camera.rotation_euler = (math.radians(100), 0, math.radians(180))

# 设置为场景相机
bpy.context.scene.camera = camera


# 使用Cycles渲染引擎获得更好的质量
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.samples = 256  # 增加采样数提高质量
bpy.context.scene.cycles.preview_samples = 32

# 提高分辨率到1080p
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.resolution_percentage = 100

# 设置输出路径
output_path = r"C:\Users\段\Desktop\作业"
if not os.path.exists(output_path):
    os.makedirs(output_path)

# 设置输出文件格式,使用高质量编码
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'
bpy.context.scene.render.ffmpeg.codec = 'H264'
bpy.context.scene.render.ffmpeg.constant_rate_factor = 'HIGH'  # 提高视频质量
bpy.context.scene.render.ffmpeg.ffmpeg_preset = 'GOOD'
bpy.context.scene.render.ffmpeg.gopsize = 10  # 较小的GOP尺寸提供更好的质量
bpy.context.scene.render.ffmpeg.use_max_b_frames = True  # 使用B帧提高压缩质量
bpy.context.scene.render.ffmpeg.video_bitrate = 10000  # 提高比特率
bpy.context.scene.render.filepath = os.path.join(output_path, "mario_mushroom_animation_hq.mp4")

# 确保渲染完整的50帧
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 50

# 设置世界背景为纯白色
world = bpy.context.scene.world
if world is None:
    world = bpy.data.worlds.new("World")
    bpy.context.scene.world = world
world.use_nodes = True
world.node_tree.nodes["Background"].inputs[0].default_value = (1, 1, 1, 1)

# 开始渲染
print(f"开始渲染,输出将保存到: {output_path}")
bpy.ops.render.render(animation=True)
print(f"渲染完成!文件已保存到: {output_path}")

总结 🤔

  • blender可以给我们制作各种角色

图片描述

  • 但是 这些角色 在什么场景中出现 呢?🤔
  • 我们下次再说!👋

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。