disappearing skin_vertices after assigning new mesh to object This has me stumped.

import bpy

meshes = bpy.data.meshes
objects = bpy.data.objects
scene = bpy.context.scene

def new_mesh(name):
    verts = [(3, 3, -3), (3, -3, -3), (-3, -3, -3)]
    edges = [(0, 1),(1,2),(2,0)]
    mesh_data = meshes.new(name)
    mesh_data.from_pydata(verts, edges, [])
    mesh_data.update()
    return mesh_data

mesh = new_mesh('some_mesh')
obj = objects.new('example_obj', mesh)
scene.objects.link(obj)
obj.modifiers.new(type='SKIN', name='sv_skin')

print(len(obj.data.skin_vertices))


# now switch the mesh to a new one.
obj.data = new_mesh('some_other_mesh')

print(len(obj.data.skin_vertices))


This snippet demonstrates a problem I'm having. Once i've added a Skin Modifier to the object, and then change the mesh data by assigning a new mesh to .data, then skin_vertices seems to be empty.

Yes there are workarounds.

@stacker below points out that I can remove and add the modifier to make skin_vertices again. This is a solution I use already, but it results in a lot of extra code to make sure other modifiers stay in the right place. In viewer_skin.py (linked to) I am hardcoding the modifiers, but you should imagine that I don't want to make any assumptions about which modifiers are on that stack at all (except for the skin modifier)

I'm trying to not use bpy.ops.object.modifier_(move_up, move_down, add) because that requires an active object.
When scripting updates to obj.data there currently (Jan 2016) does not seem to be a way to refresh data.skin_vertices without removing and adding the modifier. 

For example if the "SKIN" modifier is named "sv_skin"

    # if modifier present, remove
    if 'sv_skin' in obj.modifiers:
        sk = obj.modifiers['sv_skin']
        obj.modifiers.remove(sk)

    # make new modifier and get reference to it.
    a = obj.modifiers.new(type='SKIN', name='sv_skin')


If you have a few other items on that object's modifier stack then there's a bit more bookkeeping to do.

Here's an example of a working scripted update to generate a lattice mesh with a skin modifier in Sverchok.



The code: https://gist.github.com/zeffii/e556ebcb33737a3fd58b

  &lt;-- includes the script, but does require Sverchok.