You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blender中编写创建网格时弹出的特定Add菜单代码?

给Blender添加自定义网格到Add菜单的代码示例

直接上可运行的完整代码,你可以替换其中的网格生成逻辑为自己的需求:

1. 自定义网格生成操作

import bpy
from bpy.types import Operator
from bpy.props import FloatVectorProperty
from mathutils import Vector

class OBJECT_OT_custom_mesh_add(Operator):
    """添加自定义网格"""
    bl_idname = "mesh.custom_mesh_add"
    bl_label = "自定义网格"
    bl_options = {'REGISTER', 'UNDO'}

    # 可选:添加位置参数,和内置网格保持一致的交互
    location: FloatVectorProperty(
        name="位置",
        subtype='TRANSLATION',
    )

    def execute(self, context):
        # 这里替换成你的网格生成逻辑,示例为简单四面体
        verts = [
            Vector((1, 1, 1)),
            Vector((-1, -1, 1)),
            Vector((-1, 1, -1)),
            Vector((1, -1, -1)),
        ]
        edges = []
        faces = [
            (0, 1, 2),
            (0, 1, 3),
            (0, 2, 3),
            (1, 2, 3),
        ]

        # 创建网格并添加到场景
        mesh = bpy.data.meshes.new(name="自定义网格")
        mesh.from_pydata(verts, edges, faces)
        mesh.update()

        obj = bpy.data.objects.new("自定义网格", mesh)
        obj.location = self.location
        context.collection.objects.link(obj)
        
        # 自动选中新对象
        bpy.context.view_layer.objects.active = obj
        obj.select_set(True)

        return {'FINISHED'}

2. 绑定到Add菜单

from bpy.types import Menu

def draw_custom_mesh_entry(self, context):
    # 添加分隔线和自定义选项,和内置网格菜单排版一致
    self.layout.separator()
    self.layout.operator(OBJECT_OT_custom_mesh_add.bl_idname)

# 注册/注销逻辑
def register():
    bpy.utils.register_class(OBJECT_OT_custom_mesh_add)
    # 绑定到3D视图的网格添加菜单(就是你截图里的Add菜单)
    bpy.types.VIEW3D_MT_mesh_add.append(draw_custom_mesh_entry)

def unregister():
    bpy.utils.unregister_class(OBJECT_OT_custom_mesh_add)
    bpy.types.VIEW3D_MT_mesh_add.remove(draw_custom_mesh_entry)

if __name__ == "__main__":
    register()

关键提示

  • 你截图里的菜单对应Blender内置类型VIEW3D_MT_mesh_add,这是绑定的核心
  • bl_idname必须唯一,不能和现有操作重名
  • 把示例中的四面体生成代码替换成你自己的网格逻辑即可,只要正确输出顶点、边、面数据就行
  • 运行脚本后,按Shift+A打开Add菜单,就能在网格分类里看到你的自定义选项

内容的提问来源于stack exchange,提问作者Luis Albacete

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 19:21:34