Ongoing problem (2.79b) : convertViewVec: called in an invalid context

Hello,
I have an ongoing problem (2.79b) that I think many others have too.
I’ve searched extensively and tried all available solutions but nothing works.
What I need to do, is move vertices individually WITH proportional enabled. I’m basically trying to script deformation of a mesh ‘cage’ that is bound to an underlying object.

I have a script working perfectly, except the bpy.ops.trasform.translate function which gives the error:

"convertViewVec: called in an invalid context"

I know this is because I’m running the script from the text editor instead of the 3D view. I’ve tried the various solutions where people have added a panel and button to the 3D view, but none of them work. I’ve tried switching contexts but then Blender just crashes hard.

Here is a code snippet; everything works fine up to the last line bpy.ops…translate.
The error occurs at the bpy…translate function (of course) AND I don’t get any translation.

Thanks for any help. I’m on a deadline and am getting pretty desperate. Can’t find a working solution. Maybe it’s possible to move vertices proportionally using bmesh but can’t figure that out either.

import bpy
import bmesh
from mathutils import Vector

#for now you need to be in Object Mode and have the target curve selected before running script.
#also you need to have already built your cage and set your cage contour vertex group.
#the vertex group must be named 'cagefacepts' as per below.

obj = bpy.context.active_object
objdata = bpy.context.active_object.data

vertices = objdata.vertices
vertstarg = [obj.matrix_world * vert.co for vert in vertices] 
vertstargorig = [vert.co for vert in vertices] 


#have to make the object active before selecting. makes sense now.
bpy.context.scene.objects.active = bpy.data.objects['crvface']  
bpy.data.objects['crvface'].select = True

obcage = bpy.context.object
obcagedata = bpy.context.object.data
        
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.object.vertex_group_set_active(group='cagefacepts')
bpy.ops.object.vertex_group_select()
#vgVerts = [ v for v in obcage.data.vertices if v.select ]

bpy.ops.object.mode_set(mode='OBJECT')

#raise Exception()
vcount = 0
#bm = bmesh.from_edit_mesh(obcage.data)
#print(bm.verts)

for v in obcagedata.vertices:
    if v.select:
        #set the tranformation scale factor (vtsf) to make the inner mgon deform more.
        vtsf = 1.
        #v.co = (v.co.x * vtsf, v.co.y * vtsf, v.co.z * vtsf)
        vtrans = vertstarg[vcount] - v.co
        vtrans.x = vtrans.x * vtsf
        vtrans.y = vtrans.y * vtsf
        vtrans.z = vtrans.z * vtsf #* .95  #reduce z by 5% just for example
        print(vcount, v.co, vertstarg[vcount], vtrans)
        #raise Exception()
        #bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        #bmesh.ops.translate(bm, verts=v, vec=vtrans)
        bpy.ops.transform.translate(value = vtrans, constraint_axis=(False, False, False), constraint_orientation='LOCAL', proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=10., release_confirm=True, use_accurate=False)
        print('On vertex -> ', vcount)
        #raise Exception()

I did not try your code but turn it into a real addon and run in a proper context, that might be the issue. See the sample Python templates for view context related examples