Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0717
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在:
个人网站: `https://oeasy.org`
蓝桥云课: `https://www.lanqiao.cn/courses/3584`
GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
Gitee: `https://gitee.com/overmind1980/oeasypython`
---import bpy
import math
# 清除场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
# 创建三角形(增大尺寸并居中)
vertices = [(-2, -2, 0), (2, -2, 0), (0, 2, 2)] # 居中且增大的三角形
faces = [(0, 1, 2)]
mesh = bpy.data.meshes.new("Triangle")
obj = bpy.data.objects.new("Triangle", mesh)
bpy.context.collection.objects.link(obj)
mesh.from_pydata(vertices, [], faces)
# 添加明显的颜色材质
material = bpy.data.materials.new(name="ColorMaterial")
material.use_nodes = True
bnodes = material.node_tree.nodes
bnodes["Principled BSDF"].inputs["Base Color"].default_value = (0.8, 0.2, 0.2, 1.0) # 红色
obj.data.materials.append(material)
# 添加摄影机
camera_distance = 8 # 增加距离以确保完整显示
bpy.ops.object.camera_add(location=(camera_distance, -camera_distance, camera_distance))
camera = bpy.context.active_object
bpy.context.scene.camera = camera
# 设置摄影机的目标点为场景中心
look_at = (0, 0, 0.67) # 三角形中心
# 计算摄影机指向目标点的旋转角度
import mathutils
quat = (mathutils.Vector(look_at) - camera.location).to_track_quat('-Z', 'Y')
camera.rotation_euler = quat.to_euler()
# 调整摄影机视野角度
camera.data.angle = math.radians(60) # 增大视野角度
# 添加光照并增加强度
bpy.ops.object.light_add(type='POINT', location=(6, 6, 8))
light = bpy.context.active_object
light.data.energy = 2000 # 增加光照强度
# 设置渲染参数
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.image_settings.color_mode = 'RGBA'
bpy.context.scene.cycles.samples = 128 # 减少采样数加快渲染
bpy.context.scene.cycles.preview_samples = 32
bpy.context.scene.render.filepath = '/Users/easyo/Desktop/test/render_result.png'
# 渲染
bpy.ops.render.render(write_still=True)
print("渲染完成!结果保存为:", bpy.context.scene.render.filepath)
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。