Select and translate vertices with python

I’m stumbling a bit trying write a new operator for my purposes.
What it should do:
Duplicate the whole mesh and then translate each new vertex along its normal.
Thats somehow the same thing as scaling the new vertexes after Shift-D. But in future versions I want to be able to scale each vertex individually depending on additional information.

Now the vertex selecting gives me a lot of hiccups with the mode toggling, but I got it to work somehow.
Only, it works on just some vertices, not all of them.
I get an “convertViewVec: called in an invalid context” on every other iterartion over the verts…
Can someone help me out here?
When I play with the #bpy.ops.object.mode_set(mode=‘OBJECT’) at the beginning or the end of the for loop the error occurs on the even or odd iterations.
The code should run fine from the texteditor when in edit mode with a cube.

import bpy    
def create_hull(verts, move):
    
    bpy.ops.mesh.select_all(action="DESELECT")
    
    for v in verts:  
        bpy.ops.object.mode_set(mode='OBJECT')
        v.select = True
        bpy.ops.object.mode_set(mode='EDIT')
        direction = v.normal.normalized()*move
        bpy.ops.transform.translate(value=(direction), constraint_axis=(False, False, False),constraint_orientation='GLOBAL')
        bpy.ops.mesh.select_all(action="DESELECT")
        #bpy.ops.object.mode_set(mode='OBJECT')
    
     
verts = []
bpy.ops.mesh.duplicate_move()
bpy.ops.object.mode_set(mode='OBJECT')
me = bpy.context.active_object
bpy.ops.object.mode_set(mode='EDIT')
for vert in me.data.vertices:
    if vert.select:
        verts.append(vert)                

create_hull(verts,1.0)

how about this utilizing bmesh module:

import bpy
import bmesh


     
def main(context):
    
    for ob in context.selected_objects:
        edbm = False
        if ob.mode == 'EDIT':
            edbm = True
            
        if edbm:
            bm = bmesh.from_edit_mesh(ob.data)
        else:
            bm = bmesh.new()
            bm.from_mesh(ob.data)
            
        for v in bm.verts:
            if v.select or not edbm:
                # Translate all verts if object is in object mode,
                # selected verts only if in edit mode.
                v.co += v.normal * 0.2
            
        if not edbm:
            bm.to_mesh(ob.data)
        ob.data.update()




class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"


    @classmethod
    def poll(cls, context):
        return context.object is not None


    def execute(self, context):
        main(context)
        return {'FINISHED'}




def register():
    bpy.utils.register_class(SimpleOperator)




def unregister():
    bpy.utils.unregister_class(SimpleOperator)




if __name__ == "__main__":
    register()


    # test call
    bpy.ops.object.simple_operator()

Hey CoDEmanX,

I tried bmesh before, but it crashed blender somehow.
Your solution works like a charm, thanks a lot.

you have to be careful with bmeshes, .e.g:

bm = bmesh.from_edit_mesh(ob.data)
bpy.ops.object.mode_set(mode='OBJECT')
# accesing bm now will crash blender, 'cause it was freed on mode change!

see http://www.blender.org/documentation/blender_python_api_2_64_6/info_gotcha.html#edit-mode-memory-access