6

I would like to create a simple mesh in Blender (2.50) via the Python API but the examples from the API documentation don't work yet.

I tried the following but it's from API 2.49

   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

This does not work because the mesh object doesn't have any faces or verts members.

Are there any options to do this?

guerda
  • 23,388
  • 27
  • 97
  • 146

2 Answers2

3

Try this documentation for the 2.5x API. I understand that despite the big warnings at the top, the most used sections are fairly stable now. I've not tried it yet.

EDIT:

I think the relevant bit is this section - it seems you create a list of vertices faces etc. and pass it to this. This seems to have changed from the most recent examples I can find. Try looking in your scripts folder - there might be an example there that you can look at.

EDIT 2: I have updated the link to point to the current live docs. The notes there suggest that there are probably better ways of doing this now but it is a long time since I have done any blender scripting so can't help more.

neil
  • 3,387
  • 1
  • 14
  • 11
  • Thank you for this link, even when I already knew this. Could you please point me to a specific page in this documentation? I haven't found a working example yet. – guerda Sep 07 '10 at 09:36
  • I could not write a working example with the documentation. Could you help me with that? – guerda Sep 08 '10 at 08:51
  • Sorry, I've not done anything with the new API, I'm just looking at the docs and going by what I've read elsewhere. You'll probably get more help on blenderartists.org in the Python and Plugins forum. – neil Sep 08 '10 at 09:13
  • The link for "this section" is currently dead. –  Oct 16 '14 at 18:21
1

Thanks to neil, I found the following section from the documentation:

Scripts for Blender 2.50 - Add Mesh Scripts

I will try the following script and report my results:

Add Solid Object Mesh

guerda
  • 23,388
  • 27
  • 97
  • 146