Skip to content

Latest commit

 

History

History
1535 lines (1276 loc) · 50.3 KB

649-2716064-优秀作品展映_器物篇.sy.md

File metadata and controls

1535 lines (1276 loc) · 50.3 KB
show version enable_checker
step
1.0
true

python_blender

开始

  • 上次通过python设置了舞台基本要素
    • 灯光
    • 物体
    • 摄像机
  • 并且将结果渲染为一张png
  • 有什么优秀作品可以看一下吗??🤔

茶壶

图片描述

import bpy

# 删除所有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# 创建茶壶主体
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=1, location=(0, 0, 1.5))
body = bpy.context.object
body.name = 'Teapot_Body'

# 创建茶壶盖
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=0.8, location=(0, 0, 2.5))
lid = bpy.context.object
lid.name = 'Teapot_Lid'

# 将盖子和主体分离
bpy.ops.object.select_all(action='DESELECT')
lid.select_set(True)
bpy.context.view_layer.objects.active = lid
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')
lid.data.polygons[0].select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.mode_set(mode='OBJECT')

# 创建壶嘴
bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=0.1, depth=2, location=(1.2, 0, 1.8))
spout = bpy.context.object
spout.name = 'Teapot_Spout'
spout.rotation_euler[1] = 0.785398  # 45 degrees in radians

# 创建壶把
bpy.ops.mesh.primitive_torus_add(major_radius=1, minor_radius=0.1, location=(-1.2, 0, 1.8))
handle = bpy.context.object
handle.name = 'Teapot_Handle'
handle.rotation_euler[0] = 1.5708  # 90 degrees in radians
handle.scale[2] = 1.5

# 合并所有部分
bpy.ops.object.select_all(action='DESELECT')
body.select_set(True)
lid.select_set(True)
spout.select_set(True)
handle.select_set(True)
bpy.context.view_layer.objects.active = body
bpy.ops.object.join()

# 添加材质
mat_body = bpy.data.materials.new(name="Teapot_Body_Material")
mat_body.diffuse_color = (0.8, 0.4, 0.2, 1.0)  # RGB with alpha

mat_lid = bpy.data.materials.new(name="Teapot_Lid_Material")
mat_lid.diffuse_color = (0.2, 0.4, 0.8, 1.0)  # RGB with alpha

mat_spout = bpy.data.materials.new(name="Teapot_Spout_Material")
mat_spout.diffuse_color = (0.4, 0.8, 0.2, 1.0)  # RGB with alpha

mat_handle = bpy.data.materials.new(name="Teapot_Handle_Material")
mat_handle.diffuse_color = (0.8, 0.8, 0.2, 1.0)  # RGB with alpha

# 将材质分配给对象
for obj in bpy.context.selected_objects:
    if obj.name == 'Teapot_Body':
        obj.data.materials.append(mat_body)
    elif obj.name == 'Teapot_Lid':
        obj.data.materials.append(mat_lid)
    elif obj.name == 'Teapot_Spout':
        obj.data.materials.append(mat_spout)
    elif obj.name == 'Teapot_Handle':
        obj.data.materials.append(mat_handle)

# 设置旋转动画
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 250

# 创建旋转动画关键帧
body.animation_data_create()
body.animation_data.action = bpy.data.actions.new(name="RotateAction")

fcurves = body.animation_data.action.fcurves
data_path = "rotation_euler"
for i in range(3):
    fcurve = fcurves.new(data_path=data_path, index=i)
    kf = fcurve.keyframe_points
    kf.add(2)

kf = fcurves[2].keyframe_points
kf[0].co = 1, 0  # Frame 1, Rotation 0
kf[1].co = 250, 6.28319  # Frame 250, Rotation 360 degrees (2*PI)

# 设置插值模式为线性
for fcurve in fcurves:
    for kf in fcurve.keyframe_points:
        kf.interpolation = 'LINEAR'

# 创建聚光灯
bpy.ops.object.light_add(type='SPOT', radius=1, location=(5, -5, 5))
spotlight = bpy.context.object
spotlight.data.energy = 500
spotlight.data.spot_size = 0.785398  # 45 degrees in radians

bpy.ops.object.light_add(type='SPOT', radius=1, location=(-5, 5, 5))
spotlight2 = bpy.context.object
spotlight2.data.energy = 500
spotlight2.data.spot_size = 0.785398  # 45 degrees in radians

# 创建摄像机
bpy.ops.object.camera_add(location=(7, -7, 7))
camera = bpy.context.object
camera.rotation_euler = (1.1, 0, 0.785398)  # 63 degrees in radians on X and 45 degrees on Z

# 设置摄像机为活动摄像机
bpy.context.scene.camera = camera

print("Tea pot created successfully with materials, rotation, lights, and camera setup!")

bpy.context.scene.camera = camera
bpy.ops.object.light_add(type='SPOT', radius=2, location=(-2, 0, 10))
bpy.context.object.data.energy = 1000
bpy.context.object.location = (6.27,-3.4,6.83)
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

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

花朵

图片描述

import bpy
import math
import colorsys

# 删除默认的场景内容
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()

# 创建花的中心
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 0))
center = bpy.context.object
center.name = "flower_center"

# 创建一个黄色的材料(花心)
yellow_material = bpy.data.materials.new(name="Yellow")
yellow_material.diffuse_color = (1, 1, 0, 1)  # RGBA: 黄色
center.data.materials.append(yellow_material)

# 创建花瓣并摆放成花朵形状
num_petals = 8
angle_step = 360 / num_petals
petal_radius = 0.3

for i in range(num_petals):
    angle = math.radians(i * angle_step)
    x = math.cos(angle) * 1.5
    y = math.sin(angle) * 1.5
    
    # 创建一个花瓣
    bpy.ops.mesh.primitive_uv_sphere_add(radius=petal_radius, location=(x, y, 0))
    petal = bpy.context.object
    petal.name = f"petal_{i+1}"
    
    # 设定花瓣形状
    petal.scale[0] = 4
    petal.scale[1] = 1
    petal.scale[2] = 0.2
    
    # 将花瓣旋转到合适的位置
    petal.rotation_euler = (3, 0, angle)
    
    # 设置花瓣的父对象为花的中心
    petal.parent = center
    
    # 创建粉色渐变材质
    hue = 0.95  # 粉色系的色相(H值)
    saturation = 0.6 + (i / num_petals) * 0.4  # 从0.6到1的渐变
    value = 1  # 保持最大亮度
    r, g, b = colorsys.hsv_to_rgb(hue, saturation, value)
    
    petal_material = bpy.data.materials.new(name=f"Petal_{i+1}")
    petal_material.diffuse_color = (r, g, b, 1)  # RGBA
    
    # 将材质应用到花瓣上
    petal.data.materials.append(petal_material)

# 创建聚光灯并调整其属性
bpy.ops.object.light_add(type='SPOT', radius=1.0, location=(0, 0, 3))
spot_light = bpy.context.object
spot_light.name = "SpotLight"
spot_light.data.energy = 200.0  # 设置光源强度
spot_light.data.spot_size = math.radians(60)  # 聚光灯的角度
spot_light.data.spot_blend = 0.2  # 聚光灯的软化程度
spot_light.rotation_euler = (math.radians(0), 0, 0)  # 调整光源角度

# 创建摄像机并移动到远一点的位置
bpy.ops.object.camera_add(location=(4, -6, 3))
camera = bpy.context.object
camera.name = "Camera"
camera.rotation_euler = (math.radians(60), 0, math.radians(45))

# 设置渲染设置
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'  # 如果有GPU可用的话
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.filepath = '/path/to/save/render.png'

# 设置场景的摄像机
bpy.context.scene.camera = camera

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

盆栽

图片描述

  • 盆栽
import bpy
import math

# 删除所有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# 创建基础圆柱体
bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=1, depth=1.5, location=(0, 0, 0))
pot = bpy.context.active_object
pot.name = "Flower_Pot"

# 编辑模式下挤出和缩放以创建花盆形状
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.transform.translate(value=(0, 0, 0.8))  # 移动到正确的位置
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, 0.7)})  # 向下挤出
bpy.ops.transform.resize(value=(1.2, 1.2, 0.3))  # 缩小底部
bpy.ops.object.mode_set(mode='OBJECT')


# 创建花盆的颜色材质
pot_material = bpy.data.materials.new(name="Pot_Material")
pot_material.diffuse_color = (0.6, 0.3, 0.1, 1)  
pot.data.materials.append(pot_material)



# 创建一个花茎(圆柱体)
bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=0.2, depth=2, location=(0, 0, 2.5))
stem = bpy.context.active_object
stem.name = "Flower_stem"

#color
mat = bpy.data.materials.new('mat_tree')
color = (0.8, 0.2, 0.1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 创建圆柱体
bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=0.1, depth=0.7, location=(0, -0.2, 2.5))
cylinder = bpy.context.active_object
cylinder.name = "Slanted_cylinder"

#color
mat = bpy.data.materials.new('mat_tree')
color = (0.6, 0.3, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 设置圆柱体的旋转角度(以弧度为单位)
# 欧拉角使用顺序为 X-Y-Z (pitch-roll-yaw)
rotation_angle_x = math.radians(45)  # 在X轴方向上旋转45度

# 设置旋转角度
cylinder.rotation_euler = (rotation_angle_x, 0, 0)

# 创建圆柱体2
bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=0.1, depth=0.7, location=(0, 0.4, 2.8))
cylinder = bpy.context.active_object
cylinder.name = "Slanted_cylinder2"

#color
mat = bpy.data.materials.new('mat_tree')
color = (0.6, 0.3, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 设置圆柱体的旋转角度(以弧度为单位)
# 欧拉角使用顺序为 X-Y-Z (pitch-roll-yaw)
rotation_angle_x = math.radians(-45)  # 在X轴方向上旋转45度

# 设置旋转角度
cylinder.rotation_euler = (rotation_angle_x, 0, 0)

# 添加简单变形修改器以弯曲花茎
bend_modifier = stem.modifiers.new(name="SimpleDeform", type='SIMPLE_DEFORM')
bend_modifier.deform_method = 'BEND'
bend_modifier.angle = math.radians(20)  # 你可以根据需要调整弯曲角度
bend_modifier.origin = bpy.context.object


# 更新视图以应用修改
bpy.context.view_layer.update()


# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.7, location=(0, 0.1, 3.8))
mat = bpy.data.materials.new('mat_tree')
color = (0.2, 1, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)


# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.6, location=(0, 0.1, 4.3))
mat = bpy.data.materials.new('mat_tree')
color = (0.5, 1, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0.1, 4.8))
mat = bpy.data.materials.new('mat_tree')
color = (0, 1, 0.5, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.38, location=(0, -0.5, 2.8))
mat = bpy.data.materials.new('mat_tree')
color = (0, 1, 0.7, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.26, location=(0, 0.7, 3.1))
mat = bpy.data.materials.new('mat_tree')
color = (0.2, 1.2, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)


# 创建球体
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.2, location=(0, 0.82, 3.2))
mat = bpy.data.materials.new('mat_tree')
color = (0.2, 1, 0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)


#camera
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 = (10.787, 0.34901, 10.312)  # X, Y, Z coordinates
camera_obj.rotation_euler = (53.2*0.0174444444,0*0.0174444444,90*0.0174444444)

#渲染?
bpy.context.scene.camera = camera_obj
bpy.ops.object.light_add(type='SPOT', radius=1)
bpy.context.object.data.energy = 1000
bpy.context.object.location = (6.27,-3.4,5.33)
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


# 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 = './render2.png'
# Render the current view
bpy.ops.render.render(write_still=True)

大树

图片描述

import bpy

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

# 定义圣诞树的层数和参数
num_layers = 3
layer_height = 1.3  # 每层的高度
base_radius = 0.2  # 底部圆柱体的半径

tree = bpy.data.objects.new("tree", None)
bpy.data.collections["Collection"].objects.link(tree)
# 创建圣诞树的主干(圆柱体)
bpy.ops.mesh.primitive_cylinder_add(radius=base_radius, depth=num_layers * layer_height, location=(0, 0, 0))
trunk = bpy.context.object
trunk.parent = tree

# 创建圣诞树的第一层树冠(圆台)
bottom_radius1 = 1.2  # 第一层底部圆台的底半径
top_radius1 = 0.3  # 第一层底部圆台的顶半径
height1 = 1.0  # 第一层底部圆台的高度
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=bottom_radius1, radius2=top_radius1, depth=height1, location=(0, 0, height1 / 2))
tree_top1 = bpy.context.object
tree_top1.parent = tree

# 创建圣诞树的第二层树冠(圆台)
bottom_radius2 = 0.8  # 第二层底部圆台的底半径
top_radius2 = 0.1  # 第二层底部圆台的顶半径
height2 = 1.0  # 第二层底部圆台的高度
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=bottom_radius2, radius2=top_radius2, depth=height2, location=(0, 0, height1 + height2 / 2))
tree_top2 = bpy.context.object
tree_top2.parent = tree

# 创建圣诞树的第三层树冠(圆锥体)
bottom_radius3 = 0.5  # 第三层底部圆锥体的底半径
height3 = 1.0  # 第三层底部圆锥体的高度
bpy.ops.mesh.primitive_cone_add(vertices=32, radius1=bottom_radius3, depth=height3, location=(0, 0, height1 + height2 + height3 / 2 - 0.3))  # 往下移动一些,与第二层圆台贴合
tree_top3 = bpy.context.object
tree_top3.parent = tree

# 设置圣诞树的材质
mat_green = bpy.data.materials.new(name="GreenMaterial")
mat_green.diffuse_color = (0.1, 0.5, 0.1, 1.0)  # 绿色
trunk.data.materials.append(mat_green)
tree_top1.data.materials.append(mat_green)
tree_top2.data.materials.append(mat_green)
tree_top3.data.materials.append(mat_green)

bpy.ops.mesh.primitive_plane_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.location[2] = -2
bpy.context.object.scale = (10,10,1)

bpy.ops.object.light_add(type='SPOT', radius=1)
light = bpy.context.object
light.name = "Light"
light.data.energy = 1000
light.location = (2.100301504135132, -2.5599961280822754, 4.457478046417236)
light.rotation_euler = (0.6562445163726807, 8.790517114221075e-08, 0.48171061277389526)

bpy.ops.object.camera_add()
camera = bpy.context.object
camera.location = (-6.545682430267334, -6.106528282165527, 7.655974388122559)
camera.rotation_euler = (0.8726650476455688, 3.8112457900751906e-07, -0.760964035987854)
bpy.context.scene.camera = camera

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

def delete_all():
    # clear current scene
    bpy.ops.object.select_all(action="SELECT") # 选择所有物体
    bpy.ops.object.delete() # 删除选定的物体

def create_car_body():
    bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 1))
    car_body = bpy.context.object
    car_body.name = "CarBody"
    return car_body

def create_wheel(location):
    bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=0.3, location=location)
    wheel = bpy.context.object
    wheel.rotation_euler[0] = 1.5708 # 旋转90度使轮子正确对齐
    wheel.name = "Wheel"
    return wheel

def create_car():
    car_body = create_car_body()
    wheel_locations = [
    (-0.8, -1.2, 0.3),
    (0.8, -1.2, 0.3),
    (-0.8, 1.2, 0.3),
    (0.8, 1.2, 0.3)
    ]
    wheels = [create_wheel(loc) for loc in wheel_locations]
    return car_body, wheels

def apply_material(obj, name, color):
    material = bpy.data.materials.new(name=name)
    material.diffuse_color = color
    obj.data.materials.append(material)

def set_lighting():
    bpy.ops.object.light_add(type='SPOT', location=(4, 3.26, 2.89))
    spot_light = bpy.context.object
    spot_light.data.energy = 1000
    spot_light.data.spot_size = 0.7854 # 45 degrees
    spot_light.data.spot_blend = 0.15
    spot_light.rotation_euler = (71/360.0*6.28, 0, 125.4/360.0*6.28) # 45 degrees

def set_camera():
    bpy.ops.object.camera_add(location=(6, -6, 4))
    camera = bpy.context.object
    camera.rotation_euler = (1.2, 0, 0.7854) # 设置适当的角度
    bpy.context.scene.camera = camera

def render_image(filepath):
    bpy.context.scene.render.filepath = filepath
    bpy.ops.render.render(write_still=True)


# Step 1: Delete all existing objects
delete_all()
# Step 2: Create the car model (素模)
car_body, wheels = create_car()
# Set lighting and camera
set_lighting()
set_camera()
# Render and save 素模
render_image('/tmp/car_plain.png')
# Step 3: Apply materials for 彩模
apply_material(car_body, "CarBodyMaterial", (0.8, 0.1, 0.1, 1)) # Red color
for wheel in wheels:
    apply_material(wheel, "WheelMaterial", (0.1, 0.1, 0.1, 1)) # Black color
# Render and save 彩模
render_image('car_colored.png')

法拉利赛车

图片描述

from math import pi

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

bpy.ops.mesh.primitive_cube_add( scale=(3, 5, 2))
mat = bpy.data.materials.new('mat_car')
color = (4,0.3,0.3,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=3, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (4, 4 ,0)
mat = bpy.data.materials.new('mat_car1')
color = (1,1.1,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=3, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (-4, 4 ,0)
mat = bpy.data.materials.new('mat_car1')
color = (1,1.1,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=2, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (-4, -4 ,-1)
mat = bpy.data.materials.new('mat_car2')
color = (1,2,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=2, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (4, -4 ,-1)
mat = bpy.data.materials.new('mat_car2')
color = (1,2,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (3, -4 ,-0.5)
mat = bpy.data.materials.new('mat_car3')
color = (0,0,0,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)


bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (-3, -4 ,-0.5)
mat = bpy.data.materials.new('mat_car3')
color = (0,0,0,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (-3, 4 ,0)
mat = bpy.data.materials.new('mat_car3')
color = (0,0,0,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=1)
bpy.context.object.rotation_euler[1] = pi /2
bpy.context.object.location = (3, 4 ,0)
mat = bpy.data.materials.new('mat_car3')
color = (0,0,0,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_plane_add(size=30)
bpy.context.object.location = (0,0,-3)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 24  # Focal length in millimeters
camera_obj.location = (13,8,9)  # X, Y, Z coordinates
camera_obj.rotation_euler = (4,-9.5,5.3)
bpy.context.scene.camera = camera_obj

bpy.ops.object.light_add(type='SPOT', radius=3)    
light = bpy.context.object  
light.data.energy = 10000
light.location = (30.8, -14.4,33)  
light.rotation_euler = (6.98, 0, 0.98)  

bpy.context.scene.render.engine = '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/xuanran.png"
bpy.context.scene.render.image_settings.file_format = 'PNG'  
bpy.context.scene.render.image_settings.color_mode = 'RGBA'  
  
bpy.ops.render.render(write_still=True)

tank

图片描述

  • tank
# 创建一个材质
import bpy,math
# 清除场景中的默认物体

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

bpy.ops.mesh.primitive_cube_add(size=2)
tank_body = bpy.context.object
tank_body.location = (0, 0, 0)
tank_body.scale = (1, 1, 0.5)

# 炮筒
bpy.ops.mesh.primitive_cylinder_add(radius=0.2, depth=1.5)
tank_cannon = bpy.context.object
tank_cannon.location = (0, -1.5, 0)
tank_cannon.rotation_euler = (0, math.radians(-90), math.radians(90))

# 轮子(简单示例,实际需要多个并合理布局)
bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=0.2)
tank_wheel_a = bpy.context.object
tank_wheel_a.location = (0.7, -0.5, -0.5)
tank_wheel_a.rotation_euler = (0, math.radians(90), 0)

bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=0.2)
tank_wheel_b = bpy.context.object
tank_wheel_b.location = (0.7, 0.5, -0.5)
tank_wheel_b.rotation_euler = (0, math.radians(90), 0)

bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=0.2)
tank_wheel_c = bpy.context.object
tank_wheel_c.location = (-0.7, -0.5, -0.5)
tank_wheel_c.rotation_euler = (0, math.radians(90), 0)


bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=0.2)
tank_wheel_d = bpy.context.object
tank_wheel_d.location = (-0.7, 0.5, -0.5)
tank_wheel_d.rotation_euler = (0, math.radians(90), 0)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (0.8, 0.2, 0.2, 1.0)
tank_cannon.data.materials.append(mat)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (1, 1, 0.5, 1.0)
tank_body.data.materials.append(mat)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (0.8, 0.3, 0.8, 1.0)
tank_wheel_a.data.materials.append(mat)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (0.8, 0.2, 0.9, 1.0)
tank_wheel_b.data.materials.append(mat)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (0.8, 0.1, 0.5, 1.0)
tank_wheel_c.data.materials.append(mat)

mat = bpy.data.materials.new(name="ColorMaterial")
mat.diffuse_color = (0.1, 0.3, 0.9, 1.0)
tank_wheel_d.data.materials.append(mat)

# 设置渲染引擎为Cycles
bpy.context.scene.render.engine = 'CYCLES'

# 设置渲染分辨率
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
light_data = bpy.data.lights.new(name="Light", type='SUN')
light_data.energy = 10  # 设置光的强度
light_object = bpy.data.objects.new(name="LightObject", object_data=light_data)
bpy.context.collection.objects.link(light_object)
# 设置光源的位置
light_object.location = (5, 5, 5)

# 创建一个摄像机
bpy.ops.object.camera_add(location=(2.94, -4.78, 0.53))
camera = bpy.context.object
camera.rotation_euler = (83/180*3.14,0,29.6/180*3.14)
# 设置摄像机的方向
bpy.context.scene.camera = camera
bpy.ops.object.select_all(action='DESELECT')
camera.select_set(True)
bpy.ops.object.constraint_add(type='TRACK_TO')
bpy.context.object.constraints["Track To"].target = tank_body
bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.filepath = '/tmp/tank.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"]
ship = bpy.data.objects.new("ship", None)
collection.objects.link(ship)
bpy.ops.mesh.primitive_cylinder_add(radius=2, depth=8)
bpy.context.object.location = (0, 0, 0)
bpy.context.object.rotation_euler[0] = pi / 2
bpy.context.object.parent = ship
bpy.context.object.name = "body"
blue_material = bpy.data.materials.new('BlueMaterial')
color = (0, 0, 1, 1) 
blue_material.diffuse_color = color
bpy.context.object.data.materials.append(blue_material)
bpy.ops.mesh.primitive_cone_add(radius1=2, depth=2)
bpy.context.object.location = (0, -4, 0)
bpy.context.object.rotation_euler[0] = pi / 2
bpy.context.object.parent = ship
bpy.context.object.name = "stern"
blue_material = bpy.data.materials.new('SkyBlueMaterial')
color = (0.53, 0.81, 0.92, 1) 
blue_material.diffuse_color = color
bpy.context.object.data.materials.append(blue_material)
bpy.ops.mesh.primitive_cone_add(radius1=2, depth=2)
bpy.context.object.location = (0, 4, 0)
bpy.context.object.rotation_euler[0] = pi / 2
bpy.context.object.parent = ship
bpy.context.object.name = "bow"
blue_material = bpy.data.materials.new('SkyBlueMaterial')
color = (0.53, 0.81, 0.92, 1) 
blue_material.diffuse_color = color
bpy.context.object.data.materials.append(blue_material)
bpy.ops.mesh.primitive_cube_add(size=1)
bpy.context.object.location = (0, 2, 2.5)
bpy.context.object.scale = (1.5, 2, 1)
bpy.context.object.parent = ship
bpy.context.object.name = "cabin"
black_material = bpy.data.materials.new('BlackMaterial')
color = (0, 0, 0, 1) 
black_material.diffuse_color = color
bpy.context.object.data.materials.append(black_material)
bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=2)
bpy.context.object.location = (0, 2, 4)
# bpy.context.object.rotation_euler[0] 
bpy.context.object.parent = ship
bpy.context.object.name = "chimney"
red_material = bpy.data.materials.new('RedMaterial')
color = (1, 0, 0, 1)  
red_material.diffuse_color = color
bpy.context.object.data.materials.append(red_material)
bpy.ops.mesh.primitive_cube_add(size=0.5)
bpy.context.object.location = (0, 1, 2.5)
bpy.context.object.parent = ship
bpy.context.object.name = "window"
blue_material = bpy.data.materials.new('BlueMaterial')
color = (0, 0, 1, 1)  
blue_material.diffuse_color = color
bpy.context.object.data.materials.append(blue_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 = (22.44, -4.18, 20.36)
camera_obj.rotation_euler = (50 * 0.0174533, 8 * 0.0174533, 66 * 0.0174533)
bpy.ops.mesh.primitive_plane_add(size=50, location=(0, 0, -4))
plane = bpy.context.object
bpy.context.scene.camera = camera_obj
bpy.context.scene.render.filepath = '/tmp/rendered_image.png'
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.ops.object.light_add(type='SPOT', radius=10, location=(10, -10, 10))
spot_light = bpy.context.object
spot_light.data.energy = 8000
spot_light.data.spot_size = pi / 2  
spot_light.data.spot_blend = 0.5 
spot_light.rotation_euler = (pi / 4, 0, pi / 4)
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
from math import pi

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

bpy.ops.mesh.primitive_torus_add(major_radius=3.75, minor_radius=0.5)
bpy.context.object.location = (0, 0 ,4.5)
bpy.context.object.rotation_euler[1] = pi /2
mat = bpy.data.materials.new('mat_ring')
color = (2,1,1,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_torus_add(major_radius=0.5, minor_radius=0.3)
bpy.context.object.location = (0, 0 ,9)
mat = bpy.data.materials.new('mat_di')
color = (2,2,1,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, radius=1)
bpy.context.object.location = (0, 0 ,9.7)
mat = bpy.data.materials.new('mat_top')
color = (1,2,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_plane_add(size=20)
bpy.context.object.location = (0,0,0)
bpy.context.object.scale = (10,10,1)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 30  # Focal length in millimeters
camera_obj.location = (10,8,19)  # X, Y, Z coordinates
camera_obj.rotation_euler = (4,-9.5,5.3)
bpy.context.scene.camera = camera_obj

bpy.ops.object.light_add(type='SPOT', radius=1)    
light = bpy.context.object  
  
light.data.energy = 2000  
light.location = (6.7,-3,15)  
light.rotation_mode = 'XYZ'  
light.rotation_euler = (7, 0, 1)  
  
bpy.context.scene.render.resolution_x = 1200 
bpy.context.scene.render.resolution_y = 900 
bpy.context.scene.render.resolution_percentage = 100

bpy.context.scene.render.engine = 'CYCLES'  
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.image_settings.file_format = 'PNG'  
bpy.context.scene.render.image_settings.color_mode = 'RGBA'  
bpy.context.scene.render.filepath = '/tmp/ring.png'
bpy.ops.render.render(write_still=True)

圣剑

图片描述

import bpy
import math

# 删除所有现有对象
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# 创建剑刃 (素模)
bpy.ops.mesh.primitive_cone_add(vertices=4, depth=4.0, radius1=0.1, radius2=0, location=(0, 0, 1.3))
blade = bpy.context.object
blade.scale = (0.5, 0.1, 0.6)
blade.rotation_euler = (0, 0, math.radians(90))
blade.name = 'Blade'

# 创建护手 (素模)
bpy.ops.mesh.primitive_cylinder_add(vertices=16, depth=0.2, radius=0.7, location=(0, 0, 0.1))
guard = bpy.context.object
guard.scale = (0.2, 0.1, 0.1)
guard.rotation_euler = (0, 0, math.radians(90))
guard.name = 'Guard'

# 创建剑柄 (素模)
bpy.ops.mesh.primitive_cylinder_add(vertices=16, depth=1.2, radius=0.3, location=(0, 0, -0.2))
hilt = bpy.context.object
hilt.scale = (0.1, 0.1, 0.5)
hilt.rotation_euler = (0, 0, 0)
hilt.name = 'Hilt'

# 创建剑柄末端的饰品 (素模)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=0.1, location=(0, 0, -0.5))
pommel = bpy.context.object
pommel.scale = (0.5, 0.5, 0.5)
pommel.name = 'Pommel'

# 剑的材质 (彩模)
material_blade = bpy.data.materials.new(name='BladeMaterial')
material_blade.diffuse_color = (0.8, 0.8, 0.9, 1)  # 银色
blade.data.materials.append(material_blade)

material_guard = bpy.data.materials.new(name='GuardMaterial')
material_guard.diffuse_color = (0.8, 0.7, 0.0, 1)  # 金色
guard.data.materials.append(material_guard)

material_hilt = bpy.data.materials.new(name='HiltMaterial')
material_hilt.diffuse_color = (0.2, 0.1, 0.0, 1)  # 深棕色
hilt.data.materials.append(material_hilt)

material_pommel = bpy.data.materials.new(name='PommelMaterial')
material_pommel.diffuse_color = (0.8, 0.7, 0.0, 1)  # 金色
pommel.data.materials.append(material_pommel)

# 创建一个平面作为地面
bpy.ops.mesh.primitive_plane_add(size=10, location=(0, 0, -2))
ground = bpy.context.object
ground.name = 'Ground'
material_ground = bpy.data.materials.new(name='GroundMaterial')
material_ground.diffuse_color = (0.2, 0.2, 0.2, 1)
ground.data.materials.append(material_ground)

# 设置相机
bpy.ops.object.camera_add(location=(2.65, -2.55, 5), rotation=(math.radians(44), 0, math.radians(46)))
camera = bpy.context.object
bpy.context.scene.camera = camera

# 设置灯光
bpy.ops.object.light_add(type='SUN', location=(5, -5, 5))
light = bpy.context.object
light.data.energy = 5

# 设置渲染设置
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'  # 如果使用 GPU 渲染
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.filepath = '/tmp/holy_sword_render_white.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() # 删除选定的物体
bpy.ops.mesh.primitive_plane_add(size = 1000)
bpy.context.object.location = (0,0,-25)
bpy.ops.mesh.primitive_cylinder_add(radius=1, depth=18)
bpy.context.object.location = (0,0,0)
bpy.context.object.rotation_euler[0] = pi /2
mat = bpy.data.materials.new('mat_1')
color = (0.266, 1.01, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=2)
bpy.context.object.scale = (0.2,0.75,1)
bpy.context.object.rotation_euler[0] = - pi / 4
bpy.context.object.location = (0, 5 ,1)
mat = bpy.data.materials.new('mat_2')
color = (0.266, 1.01, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=2)
bpy.context.object.scale = (0.2,0.75,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, 5 ,-1)
mat = bpy.data.materials.new('mat_3')
color = (0.266, 1.01, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=3)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, 7 ,-1)
mat = bpy.data.materials.new('mat_4')
color = (0.266, 0.8, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=3)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.location = (0, 7 ,1)
bpy.context.object.rotation_euler[0] = - pi / 4
mat = bpy.data.materials.new('mat_4')
color = (0.266, 0.8, 1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / 4
bpy.context.object.location = (0, 11 ,1)
mat = bpy.data.materials.new('mat_5')
color = (0.783, 0.8, 0, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, 11 ,-1)
mat = bpy.data.materials.new('mat_6')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, 11 ,-1)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / 4
bpy.context.object.location = (0, 15 ,-1)
mat = bpy.data.materials.new('mat_7')
color = (0.801, 0, 0.0026, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, 15 ,1)
mat = bpy.data.materials.new('mat_8')
color = (0, 0.800, 0.08, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=16)
bpy.context.object.scale = (0.1,0.2,1.5)
bpy.context.object.rotation_euler[0] = - pi / 1
bpy.context.object.location = (0, -6 ,0)
mat = bpy.data.materials.new('mat_9')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=10)
bpy.context.object.scale = (0.2,0.2,1.5)
bpy.context.object.rotation_euler[0] = - pi / 1
bpy.context.object.location = (0, -6 ,0)
mat = bpy.data.materials.new('mat_10')
color = (0, 0.8, 0.744, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=4)
bpy.context.object.scale = (0.8,1,1.5)
bpy.context.object.rotation_euler[0] = - pi / 1
bpy.context.object.location = (0, -6 ,10)
mat = bpy.data.materials.new('mat_11')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=4)
bpy.context.object.scale = (0.8,1,1.5)
bpy.context.object.rotation_euler[0] = - pi / 1
bpy.context.object.location = (0, -6 ,-10)
mat = bpy.data.materials.new('mat_12')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=50)
bpy.context.object.scale = (0.04,0.15,1)
bpy.context.object.rotation_euler[0] = - pi / 2
bpy.context.object.location = (0, -31 ,0)
mat = bpy.data.materials.new('mat_13')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -2
bpy.context.object.location = (0, -6 ,-11)
mat = bpy.data.materials.new('mat_14')
color = (0.10464, 0.0862603, 0.800797, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=6)
bpy.context.object.scale = (0.2,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -2
bpy.context.object.location = (0, -6 ,11)
mat = bpy.data.materials.new('mat_15')
color = (0.10464, 0.0862603, 0.800797, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=28)
bpy.context.object.scale = (0.12,0.08,1.9)
bpy.context.object.rotation_euler[0] = - pi / 2
bpy.context.object.location = (0, -32 ,0)
mat = bpy.data.materials.new('mat_16')
color = (0, 0.8, 0.744, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=5.5)
bpy.context.object.scale = (0.35,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / 4
bpy.context.object.location = (0, -57 ,1)
mat = bpy.data.materials.new('mat_13')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)
bpy.ops.mesh.primitive_cube_add(size=5.5)
bpy.context.object.scale = (0.35,0.5,1)
bpy.context.object.rotation_euler[0] = - pi / -4
bpy.context.object.location = (0, -57 ,-1)
mat = bpy.data.materials.new('mat_13')
color = (0, 0.479, 0.8, 1)
mat.diffuse_color = 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 = 40  # Focal length in millimeters
camera.sensor_width = 36  # Sensor width in millimeters
camera.sensor_height = 24  # Sensor height in millimeters
camera_obj.location = (50.623, -78.94, 30.428)  # X, Y, Z coordinates
camera_obj.rotation_euler = (0,149.6,-135.9)
bpy.context.scene.camera = camera_obj
bpy.ops.object.light_add(type='SPOT', radius=3)
bpy.context.object.data.energy = 1000
bpy.context.object.location = (50.623,-78.94,30.428)
bpy.context.object.rotation_euler = (67.85,0,54)
bpy.context.scene.render.resolution_x = 740
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.resolution_percentage = 50

# 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
from math import pi

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

bang = bpy.data.objects.new("bang", None)
bpy.data.collections["Collection"].objects.link(bang)
bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=7)
bpy.context.object.rotation_euler[0] = pi /2
mat = bpy.data.materials.new('mat_bang1')
color = (1,1.1,0.8,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.5, depth=25)
bpy.context.object.rotation_euler[2] = pi /2
bpy.context.object.location = (0, 0 ,-10.5)
bpy.context.object.name = "gan"
mat = bpy.data.materials.new('mat_bang2')
color = (1,1.1,0.8,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_monkey_add(size=5)
bpy.context.object.location = (0, 0 ,3)
bpy.context.object.rotation_euler[2] = pi /2
bpy.context.object.rotation_euler[0] = -pi /8
bpy.context.object.name = "head"
mat = bpy.data.materials.new('mat_head')
color = (1,2,2,1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

empty_obj = bpy.data.objects.new("empty", None)
empty_obj.name = "xiangquan"
bpy.data.collections["Collection"].objects.link(empty_obj)
bpy.ops.mesh.primitive_torus_add(major_radius=3.75, minor_radius=0.25)
bpy.context.object.location = (0.25, 0 ,0.25)
bpy.context.object.rotation_euler[1] = pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "out"
mat = bpy.data.materials.new('mat_huan1')
color = (2, 2,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_torus_add(major_radius=2.4, minor_radius=0.35)
bpy.context.object.location = (0.25, 0 ,0.25)
bpy.context.object.rotation_euler[1] = pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "middle"
mat = bpy.data.materials.new('mat_huan2')
color = (2, 1,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_torus_add(major_radius=1.25, minor_radius=0.5)
bpy.context.object.location = (0.25, 0 ,0.25)
bpy.context.object.rotation_euler[1] = pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "in"
mat = bpy.data.materials.new('mat_huan2')
color = (2, 1,4, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

empty_obj = bpy.data.objects.new("empty", None)
empty_obj.name = "king"
bpy.data.collections["Collection"].objects.link(empty_obj)
bpy.ops.mesh.primitive_torus_add(major_radius=1.25, minor_radius=0.3)
bpy.context.object.location = (-1, 0 ,5)
bpy.context.object.rotation_euler[1] = -pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "bottom"
mat = bpy.data.materials.new('mat_di')
color = (1, 1,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, radius=0.75)
bpy.context.object.location = (-1.25, 0 ,6)
bpy.context.object.parent = empty_obj
bpy.context.object.name = "top"
mat = bpy.data.materials.new('mat_zuan')
color = (2,4,2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cone_add(radius1=0.4, radius2=0, depth=0.8)
bpy.context.object.location = (-0.1, 0 ,5.8)
bpy.context.object.rotation_euler[1] = -pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "middle"
mat = bpy.data.materials.new('mat_zhong')
color = (3, 2,0.2, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cone_add(radius1=0.4, radius2=0, depth=0.6)
bpy.context.object.location = (-0.1, 0.3 ,5.8)
bpy.context.object.rotation_euler[1] = -pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "left"
mat = bpy.data.materials.new('mat_zuo')
color = (3, 2,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cone_add(radius1=0.4, radius2=0, depth=0.6)
bpy.context.object.location = (-0.1, -0.3 ,5.8)
bpy.context.object.rotation_euler[1] = -pi /8
bpy.context.object.parent = empty_obj
bpy.context.object.name = "right"
mat = bpy.data.materials.new('mat_you')
color = (3, 2,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

empty_obj = bpy.data.objects.new("empty", None)
empty_obj.name = "erhuan"
bpy.data.collections["Collection"].objects.link(empty_obj)
bpy.ops.mesh.primitive_cone_add(radius1=0.25, radius2=0, depth=0.6)
bpy.context.object.location = (-0.9, -2.5 ,1.75)
bpy.context.object.rotation_euler[0] = pi 
bpy.context.object.parent = empty_obj
bpy.context.object.name = "zuoer1"
mat = bpy.data.materials.new('mat_zuoer1')
color = (4, 4,4, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cone_add(radius1=0.25, radius2=0, depth=0.6)
bpy.context.object.location = (-0.9, 2.5 ,1.75)
bpy.context.object.rotation_euler[0] = pi 
bpy.context.object.parent = empty_obj
bpy.context.object.name = "youer1"
mat = bpy.data.materials.new('mat_youer1')
color = (4, 4,4, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.03, depth=0.5)
bpy.context.object.location = (-0.9, -2.5 ,2.25)
bpy.context.object.parent = empty_obj
bpy.context.object.name = "zuoer2"
mat = bpy.data.materials.new('mat_zhu1')
color = (1,4,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_cylinder_add(radius=0.03, depth=0.5)
bpy.context.object.location = (-0.9, 2.5 ,2.25)
bpy.context.object.parent = empty_obj
bpy.context.object.name = "youer2"
mat = bpy.data.materials.new('mat_zhu2')
color = (1,4,1, 1)
mat.diffuse_color = color
bpy.context.object.data.materials.append(mat)

bpy.ops.mesh.primitive_plane_add(size=60)
bpy.context.object.location = (0,0,-23)

camera = bpy.data.cameras.new('MyCamera')
camera_obj = bpy.data.objects.new('CameraObj', camera)
bpy.context.scene.collection.objects.link(camera_obj)
camera.lens = 14  # Focal length in millimeters
camera_obj.location = (10,4,9)  # X, Y, Z coordinates
camera_obj.rotation_euler = (4,-9.5,5.3)
bpy.context.scene.camera = camera_obj

bpy.ops.object.light_add(type='SPOT', radius=1)    
light = bpy.context.object  
  
light.data.energy = 1000  
light.location = (6.27, -3.4,11)  
light.rotation_mode = 'XYZ'  
light.rotation_euler = (7, 0, 1)  
  
bpy.context.scene.render.resolution_x = 800  
bpy.context.scene.render.resolution_y = 600  
bpy.context.scene.render.resolution_percentage = 100

bpy.context.scene.render.engine = 'CYCLES'  
bpy.context.scene.render.resolution_x = 1920  
bpy.context.scene.render.resolution_y = 1080  
bpy.context.scene.render.resolution_percentage = 100  
bpy.context.scene.render.filepath = "/tmp/xuanran.png"
bpy.context.scene.render.image_settings.file_format = 'PNG'  
bpy.context.scene.render.image_settings.color_mode = 'RGBA'  
  
bpy.ops.render.render(write_still=True)

太阳系

图片描述

import bpy

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

from random import random

def create_sphere(radius, distance_to_sun, obj_name):
    obj = bpy.ops.mesh.primitive_uv_sphere_add(
        radius=radius,
        location=(distance_to_sun, 0, 0),
        scale=(1, 1, 1)
    )

    bpy.context.object.name = obj_name

    bpy.ops.object.shade_smooth()
    return bpy.context.object

def create_torus(radius, obj_name):
    obj = bpy.ops.mesh.primitive_torus_add(
        location=(0, 0, 0),
        major_radius=radius,
        minor_radius=0.1,
        major_segments=60
    )
    bpy.context.object.name = obj_name
    return bpy.context.object

    bpy.ops.object.shade_smooth()
    return bpy.context.objectd

def create_emission_shader(color, strength, mat_name):
    mat = bpy.data.materials.new(mat_name)
    mat.use_nodes = True
    
    nodes = mat.node_tree.nodes
    nodes.clear()

    node_emission = nodes.new(type="ShaderNodeEmission")
    node_emission.inputs[0].default_value = color
    node_emission.inputs[1].default_value = strength
    
    node_output = nodes.new(type="ShaderNodeOutputMaterial")
    
    links = mat.node_tree.links
    link = links.new(node_emission.outputs[0], node_output.inputs[0])

    return mat


N_PLANETS = 8

ring_mat = create_emission_shader(
    (1, 1, 1, 1), 1, "RingMat"
)
for n in range(N_PLANETS):
    
    r = 1 + random() * 4
    d = 30 + n * 12 + (random() * 4 - 2)
    create_sphere(r, d, "Planet-{:02d}".format(n))
    create_torus(d, "Radius-{:02d}".format(n))

    planet = create_sphere(r, d, "Planet-{:02d}".format(n))
    planet.data.materials.append(
        create_emission_shader(
            (random(), random(), 1, 1),
            2,
            "PlanetMat-{:02d}".format(n)
        )
    )
    
    ring = create_torus(d, "Radius-{:02d}".format(n))
    ring.data.materials.append(ring_mat)

sun = create_sphere(12, 0, "Sun")
sun.data.materials.append(
    create_emission_shader(
        (1, 0.66, 0.08, 1), 10, "SunMat"
    )
)

sun = create_sphere(12, 0, "Sun")


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 = (40, -40, 200)  # X, Y, Z coordinates
camera_obj.rotation_euler = (10*0.0174444444,-3*0.0174444444,-0 )

bpy.ops.object.light_add(type='SPOT', radius=1, location=(0, 0, 100))

bpy.context.scene.camera = camera_obj
bpy.context.scene.render.resolution_x = 640
bpy.context.scene.render.resolution_y = 480
bpy.context.scene.render.resolution_percentage = 50


# 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)

总结 🤔

  • 我们下次再说!👋