Python API

Ok, trying again in the right forum:

Anyone know where the default meshes/objects come from? I was hoping the API had like “object = Blender.Object.New(‘sphere’);”

I have yet to find the default things, so maybe I’m just doing something wrong? Any clues?

Funny you should ask. Someone is working on a set of Mesh primitives. But right now, they are all roll-your-own.

LOL… Totally the answer I didn’t want, but was expecting anyway. :stuck_out_tongue:

No big. :slight_smile:

codes his own then

Hi

I made a function to create a polygon mesh to (0,0,0). Feel free to enhance it:


def Polygon(name, numberOfSides, radius):
  #Create a new mesh
  poly = Mesh.New(name)

  #Populate mesh with vertices
  for i in range(0,numberOfSides):
    phi = 3.141592653589 * 2 * i / numberOfSides
    x = radius * cos(phi)
    y = radius * sin(phi)
    poly.verts.extend(x,y,0)

  #Add a new vertex to the center
  cVert = [(0.,0.,0.)]
  poly.verts.extend(cVert)

  #Add faces
  for i in range(0,numberOfSides-1):
    poly.faces.extend(poly.verts[-1], poly.verts[i], poly.verts[i+1])
    if i == numberOfSides-2:
      poly.faces.extend(poly.verts[-1], poly.verts[i+1], poly.verts[0])  
      
  return poly

To use it, do following:


scn = Blender.Scene.getCurrent() 

#create polygon
obPoly = Object.New("Mesh", "poly")  #creates object
mePoly = Polygon("poly", 5, 1) #creates mesh
obPoly.link(mePoly) #links mesh to object
scn.link(obPoly) #links object to scene
Blender.Redraw() #redraw window so you will see it

BeBraw: niceness… but how about putting the entire creation of the object and mesh in one function in stead of still having to do multiple things…

Here’s a better version. The user can get either Mesh or Object object.


def polyMesh(name, numberOfSides, radius, center = [0.,0.,0.]):
  #Create a new mesh
  poly = Mesh.New(name)

  #Populate mesh with vertices
  for i in range(0,numberOfSides):
    phi = 3.141592653589 * 2 * i / numberOfSides
    x = radius * cos(phi) + center[0]
    y = radius * sin(phi) + center[1]
    poly.verts.extend(x,y,0)

  #Add a new vertex to the center
  cVert = [center]
  poly.verts.extend(cVert)

  #Add faces
  for i in range(0,numberOfSides-1):
    poly.faces.extend(poly.verts[-1], poly.verts[i], poly.verts[i+1])
    if i == numberOfSides-2:
      poly.faces.extend(poly.verts[-1], poly.verts[i+1], poly.verts[0])

  return poly

def polyObj(name, numberOfSides, radius, center = [0.,0.,0.]):
  obPoly = Object.New("Mesh", name)
  mePoly = polyMesh(name, numberOfSides, radius, center)
  obPoly.link(mePoly)
  return obPoly

It might be smart to add possibility to disable the creation of faces so that only the outline is created.