如何通过Python API在Blender 2.50中创建简单网格

6 投票
2 回答
14165 浏览
提问于 2025-04-16 03:48

我想在Blender(2.50版本)里通过Python接口创建一个简单的网格,但API文档里的例子现在都不管用了。

我试过以下代码,但这是2.49版本的API的内容。

   from Blender import *
   import bpy

   editmode = Window.EditMode()    # are we in edit mode?  If so ...
   if editmode: Window.EditMode(0) # leave edit mode before getting the mesh

   # define vertices and faces for a pyramid
   coords=[ [-1,-1,-1], [1,-1,-1], [1,1,-1], [-1,1,-1], [0,0,1] ]  
   faces= [ [3,2,1,0], [0,1,4], [1,2,4], [2,3,4], [3,0,4] ]

   me = bpy.data.meshes.new('myMesh')          # create a new mesh

   me.verts.extend(coords)          # add vertices to mesh
   me.faces.extend(faces)           # add faces to the mesh (also adds edges)

   me.vertexColors = 1              # enable vertex colors 
   me.faces[1].col[0].r = 255       # make each vertex a different color
   me.faces[1].col[1].g = 255
   me.faces[1].col[2].b = 255

   scn = bpy.data.scenes.active     # link object to current scene
   ob = scn.objects.new(me, 'myObj')

   if editmode: Window.EditMode(1)  # optional, just being nice

这个代码不管用,因为网格对象没有任何facesverts的成员。

有没有其他方法可以做到这一点呢?

2 个回答

1

感谢neil,我在文档中找到了以下内容:

Blender 2.50的脚本 - 添加网格脚本

我会尝试以下这个脚本,然后告诉大家我的结果:

添加固体对象网格

3

你可以试试这个文档,里面讲的是2.5x版本的API。我知道虽然顶部有很大的警告,但现在最常用的部分已经相对稳定了。我自己还没试过。

编辑:

我觉得相关的内容在这一部分 - 看起来你需要创建一个包含顶点、面等的列表,然后把它传递给这个。这个部分似乎和我找到的最新示例有些不同。你可以看看你的脚本文件夹,那里可能有你可以参考的示例。

编辑2:我更新了链接,指向当前的在线文档。那里的说明建议现在可能有更好的方法来做这个,但我已经很久没做过blender脚本了,所以帮不了更多。

撰写回答