Ошибка возникает из-за того, что в Blender 4.2.3 LTS для точек NURBS-кривых требуется 4 координаты (x, y, z, w), а не 3. Вот исправленный и улучшенный код:
import bpy
import math
from mathutils import Vector
# Очистка сцены
bpy.ops.wm.read_factory_settings(use_empty=True)
# Функция для создания линии с градиентом
def create_gradient_curve(name, points, radius=0.05, color_start=(1, 0, 0, 1), color_end=(0, 0, 1, 1)):
# Создаем кривую
curve = bpy.data.curves.new(name=name, type='CURVE')
curve.dimensions = '3D'
curve.resolution_u = 12
# Создаем сплайн
spline = curve.splines.new('NURBS')
spline.points.add(len(points) - 1)
# Заполняем точки (4 координаты: x, y, z, w)
for i, point in enumerate(points):
spline.points[i].co = (*point[:3], 1.0) # Добавляем w=1.0
spline.points[i].radius = radius
# Создаем объект
obj = bpy.data.objects.new(name, curve)
bpy.context.collection.objects.link(obj)
# Добавляем материал с градиентом
mat = bpy.data.materials.new(name=f"{name}_mat")
mat.use_nodes = True
nodes = mat.node_tree.nodes
nodes.clear()
# Создаем узлы для градиента
output = nodes.new(type='ShaderNodeOutputMaterial')
output.location = (300, 0)
bsdf = nodes.new(type='ShaderNodeBsdfPrincipled')
bsdf.location = (100, 0)
bsdf.inputs['Base Color'].default_value = (0.8, 0.8, 0.8, 1)
bsdf.inputs['Metallic'].default_value = 0.5
bsdf.inputs['Roughness'].default_value = 0.2
# Градиент по длине кривой
curve_param = nodes.new(type='ShaderNodeValue')
curve_param.location = (-200, 100)
curve_param.outputs[0].default_value = 0.0
color_ramp = nodes.new(type='ShaderNodeValToRGB')
color_ramp.location = (-100, 0)
color_ramp.color_ramp.elements[0].color = color_start
color_ramp.color_ramp.elements[1].color = color_end
# Подключаем узлы
mat.node_tree.links.new(curve_param.outputs[0], color_ramp.inputs[0])
mat.node_tree.links.new(color_ramp.outputs[0], bsdf.inputs['Base Color'])
mat.node_tree.links.new(bsdf.outputs[0], output.inputs[0])
# Назначаем материал объекту
if obj.data.materials:
obj.data.materials[0] = mat
else:
obj.data.materials.append(mat)
# Добавляем модификатор Bevel для объема
bevel = obj.modifiers.new(name="Bevel", type='BEVEL')
bevel.width = radius * 2
bevel.segments = 8
bevel.profile = 0.5
return obj
# Создаем стебель (волна)
stem_points = []
for i in range(50):
x = 0
y = math.sin(i * 0.3) * 0.1
z = i * 0.2
stem_points.append((x, y, z))
stem = create_gradient_curve("Stem", stem_points, radius=0.03,
color_start=(0.1, 0.5, 0.1, 1),
color_end=(0.1, 0.3, 0.1, 1))
# Создаем сердцевину (спираль)
spiral_points = []
for i in range(100):
angle = i * 0.2
radius = 0.1 * (1 - i/100)
x = math.cos(angle) * radius
y = math.sin(angle) * radius
z = 1.0 + i * 0.02
spiral_points.append((x, y, z))
spiral = create_gradient_curve("Spiral", spiral_points, radius=0.02,
color_start=(1, 1, 0, 1),
color_end=(1, 0.5, 0, 1))
# Создаем лепестки (волны, закручивающиеся в овал)
colors = [
(1, 0, 0, 1), # Красный
(1, 0.5, 0, 1), # Оранжевый
(1, 1, 0, 1), # Желтый
(0, 1, 0, 1), # Зеленый
(0, 0, 1, 1), # Синий
(0.5, 0, 1, 1) # Фиолетовый
]
for i in range(6): # 6 лепестков
petal_points = []
angle_offset = i * (2 * math.pi / 6)
for j in range(50):
angle = j * 0.1 + angle_offset
radius = 0.3 + math.sin(j * 0.2) * 0.1
x = math.cos(angle) * radius
y = math.sin(angle) * radius
z = 1.0 + j * 0.01
petal_points.append((x, y, z))
color_start = colors[i % len(colors)]
color_end = colors[(i + 1) % len(colors)]
petal = create_gradient_curve(f"Petal_{i}", petal_points, radius=0.02,
color_start=color_start, color_end=color_end)
# Позиционируем лепестки
petal.rotation_euler.z = i * (2 * math.pi / 6)