An easy way to create new meshes with Python?

Hi folks, I am new to this forum, and to Python, but Blender and me have been friends for a while now.

Is it possible to create a new mesh in a Python script using the tools which are available in the GUI of Blender? A simple example would be creating a sphere. In Blender we just press ‘Space’, then specify the following:

Add > Mesh > UVSphere
Segments: 32
Rings: 32

and presto! we have a sphere. I would like to be able to make an equivalent call in Python, something of the form:

Blender.Mesh.new(‘UVSphere’, ‘segments’=32, ‘rings’=32)

(Excuse my pseudo-py!). From what I’ve examined of the Blender Python API, it seems as though I have to specify the position of each vertex individually, then create lines and faces too. I’m ashamed to say, my maths just isn’t up to it! Any help would be appreciated.

Thanks!

You can do it with current cvs-version.

From api-docs:

UVsphere(segments=32, rings=32, diameter=2.8283999999999998)

Construct a UV sphere mesh. The defaults create a 32 by 32 sphere with a diameter of 2*sqrt(2) Blender units, identical to the Blender UI.
Parameters:segments - optional number of longitudinal divisions. Value must be in the range [3,100].
(type=int) rings - optional number of latitudinal divisions. Value must be in the range [3,100].
(type=int) diameter - optional diameter of the sphere.
(type=float) Returns: returns a mesh object.

The correct API function to do what you want is in the MeshPrimitives Module which is available in 2.42.

Learn to love the API docs, http://www.blender.org/documentation/242PythonDoc/index.html

You’ll find the UVsphere function under MeshPrimitives.

An example is -


sphere_mesh = Blender.Mesh.MeshPrimitives.UVsphere(segs, rings, d)

Sorry, my bad. So it IS possible with 2.42 too.

Thanks for steering me in the right direction with this one. I only just got round to trying out your advice.
For the record, the following line lead to the following error:

me = Mesh.MeshPrimitives.UVsphere(10,10,2)

AttributeError: ‘module’ object has no attribute ‘MeshPrimitives’

The example given in the documentation did work:

me = Mesh.Primitives.UVsphere(10,10,2)

see:
http://www.blender.org/documentation/242PythonDoc/MeshPrimitives-module.html

All the above was tested in Blender 2.42. Perhaps in the upcoming 2.43 release it will work the way you suggested.

The example provided on http://www.blender.org/documentation/242PythonDoc/MeshPrimitives-module.html worked flawlessly here both on 2.42a and recent CVS.