Blender 通过 AI 自动创建脚本代码(示例)

笔记 创建于: , 更新于: | Blender

介绍

在 Blender 里面,有时候我们不需要去画每个图形,可以利用其脚本功能,通过 AI 生成脚本,然后导入到 Blender 里面,Blender 会自动生成对应的物体。 例如下面的代码是生成一个立方体每个面上贴上一个字,并让字发光。

效果

Blender脚本文字发光效果图

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
import bpy
import math
from mathutils import Vector, Matrix

# 定义常量
CUBE_TEXT = '你'  # 立方体表面的文字
CUBE_SIZE = 2     # 立方体大小
TEXT_SIZE = 1.5   # 文字大小

# 创建立方体
bpy.ops.mesh.primitive_cube_add(size=CUBE_SIZE, location=(0, 0, 0))
cube = bpy.context.active_object

# 创建新材质(普通材质,不发光)
mat = bpy.data.materials.new(name="Cube_Material")
cube.data.materials.append(mat)

# 创建6个面的文字配置
offsets = [
    (0, -1, 0),    # 前
    (0, 1, 0),     # 后
    (-1, 0, 0),    # 左
    (1, 0, 0),     # 右
    (0, 0, 1),     # 上
    (0, 0, -1)     # 下
]
rotations = [
    (math.pi/2, 0, 0),          # 前(修改)
    (-math.pi/2, 0, 0),           # 后
    (0, -math.pi/2, 0),          # 左(修改)
    (0, math.pi/2, 0),           # 右(修改)
    (0, 0, 0),                   # 上
    (math.pi, 0, 0)              # 下
]

for offset, rot in zip(offsets, rotations):
    # 创建文字
    bpy.ops.object.text_add(location=(0, 0, 0))
    text_obj = bpy.context.active_object
    text_obj.data.body = CUBE_TEXT
    
    # 设置文字属性
    text_obj.data.size = TEXT_SIZE
    text_obj.data.align_x = 'CENTER'
    text_obj.data.align_y = 'CENTER'
    
    # 将文字移动到原点
    text_obj.data.transform(Matrix.Translation(-Vector(text_obj.bound_box[0])))
    
    # 应用旋转
    text_obj.rotation_euler = rot
    
    # 移动到立方体表面
    text_obj.location = Vector(offset)
    
    # 稍微向外偏移以避免z-fighting
    offset_direction = Vector(offset).normalized()
    text_obj.location += offset_direction * 0.001
    
    # 创建发光材质
    text_mat = bpy.data.materials.new(name=f"Text_Material_{CUBE_TEXT}")
    text_mat.use_nodes = True
    text_nodes = text_mat.node_tree.nodes
    
    # 清除默认节点
    for node in text_nodes:
        text_nodes.remove(node)
    
    # 创建发光节点
    emission = text_nodes.new('ShaderNodeEmission')
    output = text_nodes.new('ShaderNodeOutputMaterial')
    
    # 设置发光颜色和强度
    emission.inputs[0].default_value = (1, 1, 0, 1)  # 黄色
    emission.inputs[1].default_value = 5.0  # 发光强度
    
    # 连接节点
    text_mat.node_tree.links.new(emission.outputs[0], output.inputs[0])
    
    # 应用材质
    text_obj.data.materials.append(text_mat)
    
    # 将文字设为立方体的子对象
    text_obj.parent = cube

# 如果场景中没有摄像机,添加摄像机
if not any(obj.type == 'CAMERA' for obj in bpy.data.objects):
    bpy.ops.object.camera_add(location=(5, -5, 5))
    camera = bpy.context.active_object
    camera.rotation_euler = (0.9, 0, 0.8)
    bpy.context.scene.camera = camera

# 如果场景中没有光源,添加环境光
if not any(obj.type == 'LIGHT' for obj in bpy.data.objects):
    bpy.ops.object.light_add(type='SUN', location=(5, 5, 5))
    sun = bpy.context.active_object
    sun.data.energy = 5.0

# 设置渲染引擎为Cycles并启用GPU
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.cycles.device = 'GPU'

复制代码后,在顶部工作区(Workspace)选项卡中点击 Scripting。 点击中上方编辑器里的 + New 按钮创建一个新文件。 粘贴上面的 Python 代码。 点击右上角的 Run Script 按钮(或按 Alt + P)。

Blender脚本设置

笔记标签:

评论 ( 如有任何问题,请在下方留言和讨论 )