changing vertex order

hello. i m actually working on a movie. and i ve done a big mystake. i don t know why but my vertex order had changed. breaking my relative shapes target. is there a way to copy vertex order from one mesh to an other. with uv map or even by hand it s always better than redo everything.
Thanks

If it can help, they got the same uvs?
Is that possible to copy the vertex order to the uv so as to transfer it?

If i was unclear here is what i want to achieve.
I got a A mesh I copy it to a B mesh and transform the B mesh. somewhere in the sculpt process the vertex order change (I don t know where).
So now i can t use my tons of A morph target to the B mesh. but boths meshes still got the same uvs.
Is there a way to repair the topology by transfering the vertex order from A to B. a script, or an external software…
Or a script that unfold the mesh in the 3d view to match the uv view. I could then use a shrink wrap modifier to match the geometry back.

Ok here is a supposed workflow
take the A mesh : unfold with anim
take the b messy mesh : unfold with anim
duplicate the A mesh and add a vertex ket to go from the Basic A mesh to the Bmesh
so i got an unfold mesh that has the a folded form and the b folded form.
use a mesh deform modifier on the A mesh pointing to the good A mesh with vertex key. animate the shape keys so as to get the form of the Bmesh with the A mesh.
dont know if it will work but…

Ok my idea do not work. ther is a script that transfer uv to object with same vertex order and topology. will it be possible to reverse the process?
martin poirier maybe?

As far as I know the vertex indices are read-only. So to change the vertex order you’d have to rebuild the mesh, taking all materials and animations into account. This might be possible with python, but it depends on the state of the mesh (are there drivers, armatures, animation, constraints, modifiers, materials, etc.) and what blender version you’re working on (2.4 or 2.5).
Could you give some additional information on that?

An alternative could be to hack directly in the .blend-file with an external editor, using Jeroen Bakker’s work [1][2]. I imagine this would be pretty difficult though (you might ask Jeroen himself, I believe he’s sometimes at #blendercoders on irc).

A while back I made a script for 2.49 for vertex reorder. It worked some times but I think when I tried it again it didn’t work. So not sure if it will be helpful, but here it is:

You need to have two versions of the model with exactly the same geometry. Doesn’t metter the vertex order but the models should be exactly the same.
If I remember correctly you have to select the object that has the verts order messed up, and then the one that has the right order, and execute the script.

EDIT: pls do back-ups of your work.

import bpy
import time

from math import hypot
from operator import itemgetter

from Blender import Mathutils
from Blender import Mesh
from Blender import Window
import Blender



def distance(a, b):
    def distance_recursion(a, b, dist, step):
        if step < len(a) - 1 and step < len(b) - 1:
            dist += hypot(dist, a[step] - b[step])
            return distance_recursion(a, b, dist, step + 1)
        
        return dist
    return distance_recursion(a, b, hypot(a[0] - b[0], a[1] - b[1]), 2)


def get_nearest_vertex(loc, verts_for_compare, discarded_verts):
    distances = {}
    
    for v in range(0, len(verts_for_compare)):
        if not verts_for_compare[v].index in discarded_verts:
            distances[v] = distance(verts_for_compare[v].co, loc)
        else:
            distances[v] = 10000000000
    
    sorted_distances = sorted(distances.items(), key=itemgetter(1))
    
    x = map(itemgetter(0), sorted_distances)
    
    return verts_for_compare[x[0]].index



one_face_of_verts = {}


scene = bpy.data.scenes.active

if scene.objects.selected[0] == scene.objects.active:
    active = 0
    non_active = 1
else:
    active = 1
    non_active = 0


ob1 = scene.objects.selected[active]
me1 = ob1.getData(mesh=1)

ob2 = scene.objects.selected[non_active]
me2 = ob2.getData(mesh=1)



# Create new mesh
me_new = Mesh.New()
me_new.addUVLayer("MyUV")
me_new.getFromObject(ob2.name)


# Add dummy first vertex
#me_new.verts.extend(1.0,1.0,1.0)


# Create new mesh
#for vertnum in range(0, len(me2.verts)):
#    me_new.verts.extend(me2.verts[vertnum].co)
    
#    print "Created vert: ", vertnum


# Add faces to the new mesh
#for facenum in range(0, len(me2.faces)):
#    face_verts = []
#    
#    for vertnum in range (0, len(me2.faces[facenum].verts)):
#        face_verts.append(me2.faces[facenum].verts[vertnum].index + 1)
        
#    me_new.faces.extend(face_verts)
    
#    print "Created face: ", facenum




for facenum in range(0, len(me_new.faces)):
    verts_orig_face = []
    verts_orig_face = me1.faces[facenum].verts
    
    checked_verts_orig = []
    
    # arrange UVs
    for vertnum in range(0, len(me_new.faces[facenum].verts)):
        # location of actual vert in new object.
        loc = [me_new.faces[facenum].verts[vertnum].co[0], me_new.faces[facenum].verts[vertnum].co[1], me_new.faces[facenum].verts[vertnum].co[2]]
        
        # nearest vert in orig object from that location
        orig_index = get_nearest_vertex(loc, verts_orig_face, checked_verts_orig)
        
        # add that vertex as already checked.
        checked_verts_orig.append(orig_index)
        
        # search order in face of origin object for that vert
        vert_order_in_face_orig = 0
        while me1.faces[facenum].verts[vert_order_in_face_orig].index != orig_index:
            vert_order_in_face_orig += 1
        
        
        me_new.faces[facenum].uv[vertnum][0] = me1.faces[facenum].uv[vert_order_in_face_orig][0]
        me_new.faces[facenum].uv[vertnum][1] = me1.faces[facenum].uv[vert_order_in_face_orig][1]
        
    
    print "Finished UV for face: ", facenum

#me_new.verts.delete(0)
        


ob_new = scene.objects.new(me_new, 'reordered_mesh')
ob1.sel = 0
ob2.sel = 0
ob_new.sel = 1

thanks i will give it a try. I don t know how i messed upthe vertex order. Im woried about that because i do not want that to happen in a future work again.

I will Let you know.