# Spin a Mesh from Python API [duplicate]

I am trying to create a simple python script that will spin an edge around a parallel axis to create part of a cylinder. The script should be executed in batch and do the following:

1. Copy an empty template to a new file
2. Open the new file
3. Create the geoemtry
4. Save the file

So far I have the following script:

import bpy
import shutil

shutil.copyfile("./temp/template.blend", "./temp/myfile.blend")

bpy.ops.wm.open_mainfile(filepath="./temp/myfile.blend")

# Create the geometry
coord1 = (1.0, 0.0, 0.0)
coord2 = (1.0, 0.0, 1.0)

Verts = [coord1, coord2]
Edges = [[0,1]]

mesh = bpy.data.meshes.new("Mesh 1")
mesh.from_pydata(Verts, Edges, [])
mesh.update()

# bpy.ops.mesh.spin(steps=9, dupli=False, degrees=90.0, center=(0.0, 0.0, 0.0), axis=(0.0, 0.0, 1.0))

# Create object
obj = bpy.data.objects.new("Object 1", mesh)
obj.data = mesh

scene = bpy.context.scene
obj.select = True

# Save the file
bpy.ops.wm.save_mainfile(filepath="./temp/myfile.blend")


Which is run from the command line with:

blender -b --python temp/test.py


This works OK, but when I try to spin the edge with bpy.ops.mesh.spin (commented out above) then I have an error:

RuntimeError: Operator bpy.ops.mesh.spin.poll() failed, context is incorrect


I understand that I need to be in the 3DView context, but how can I achieve this from python?

Any help is much appreciated.

EDIT

So finally after a bit of digging the following code worked for me. Note that the API docs for the spin also refer to a keyword argument "degrees" which does not work. Using "angle" in radians seems to do the trick.

import bpy
import shutil

shutil.copyfile("template.blend", "myfile.blend")

bpy.ops.wm.open_mainfile(filepath="myfile.blend")

# Create the geometry
coord1 = (1.0, 0.0, 0.0)
coord2 = (1.0, 0.0, 1.0)

Verts = [coord1, coord2]
Edges = [[0,1]]

mesh = bpy.data.meshes.new("Mesh 1")
mesh.from_pydata(Verts, Edges, [])
mesh.update()

# Create object
obj = bpy.data.objects.new("Object 1", mesh)
obj.data = mesh

scene = bpy.context.scene
scene.objects.active = obj

# Enter edit mode
bpy.ops.object.mode_set(mode='EDIT', toggle=False)

# Set correct context for spin
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.mesh.spin(override, steps=30, dupli=False, angle=1.0*3.14, center=(0.0, 0.0, 0.0), axis=(0.0, 0.0, 1.0))
break

# Save the file
bpy.ops.wm.save_mainfile(filepath="myfile.blend")

• Thanks CoDEmanX, that post gave me the basis of what I needed, I also needed to enter edit mode first after setting the current object active. I'll post my working code as an answer below. – JamesE Feb 20 '14 at 21:16