def updateConstraints(self, context, mesh):
        if (context.mode != "OBJECT"):
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        for marker in mesh.generic_landmarks:
            vertex1 = mesh.data.vertices[marker.v_indices[0]].co
            vertex2 = mesh.data.vertices[marker.v_indices[1]].co
            vertex3 = mesh.data.vertices[marker.v_indices[2]].co

            location = getGeneralCartesianFromBarycentre(
                marker.v_ratios, [vertex1, vertex2, vertex3])
            marker.location = location
            bmarker = None

            try:
                bmarker = bpy.data.objects[mesh.name + "_marker_" +
                                           str(marker.id)]
                bmarker.parent = None
                bmarker.location = location
            except KeyError:
                pass

            if (context.mode != "OBJECT"):
                bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

            bpy.ops.object.select_all(action='DESELECT')  #deselect all object
            if (bmarker):
                bmarker.parent = mesh
def deformWithMapping(context, owner_mesh, map_to, apply_on_mesh, mapped_points):
    print('DEFORM WITH MAPPING (UPLIFTING)');
    c = context;        
    constraint_positions = [];
    constraint_ids = [];
    invalid_indices = [];
    
    for vid, mapped_point in enumerate(mapped_points):
        if(mapped_point.is_valid):
            co = getGeneralCartesianFromBarycentre(mapped_point.bary_ratios, [map_to.data.vertices[m_vid].co for m_vid in mapped_point.bary_indices]);
            constraint_ids.append(vid);
            constraint_positions.append(co.to_tuple());
        else:
            invalid_indices.append(vid);
    
    constraint_positions = np.array(constraint_positions, dtype=np.float);
    constraint_ids = np.array(constraint_ids, dtype=np.int);
    invalid_indices = np.array(invalid_indices, dtype=np.int);
    vpos = getMeshVPos(apply_on_mesh);
    print('TOTAL VERTICES : #%s, INVALID INDICES : #%s'%(vpos.shape[0], invalid_indices.shape[0], ));
                
    if(invalid_indices.shape[0] > 0 and invalid_indices.shape[0] != vpos.shape[0]):       
        print('SOLVE WITH LSE FOR INVALID MAPPING INDICES');   
        v_group_name='Mapping-LSE-deformer';

        if(None == apply_on_mesh.vertex_groups.get(v_group_name)):
            apply_on_mesh.vertex_groups.new(name=v_group_name);
        
        group_ind = apply_on_mesh.vertex_groups[v_group_name].index;
        vertex_group = apply_on_mesh.vertex_groups[group_ind];    
        vertex_group.remove([v.index for v in apply_on_mesh.data.vertices]);
        apply_on_mesh.modifiers.clear();
        vertex_group.add(constraint_ids.tolist(), 1.0, 'REPLACE');
        
        lap_mod = apply_on_mesh.modifiers.new(name=vertex_group.name, type='LAPLACIANDEFORM');
        lap_mod.vertex_group = vertex_group.name;
        lap_mod.iterations = 3;#its was 1 before
        
        bpy.ops.object.select_all(action="DESELECT");
        apply_on_mesh.select = True;
        c.scene.objects.active = apply_on_mesh;
        
        bpy.ops.object.laplaciandeform_bind(modifier=lap_mod.name);
        
        bm = getBMMesh(c, apply_on_mesh, useeditmode=False);
        ensurelookuptable(bm);
        for i in range(constraint_ids.shape[0]):
            vid = constraint_ids[i];
            bm.verts[vid].co = constraint_positions[i];
        
        bm.to_mesh(apply_on_mesh.data);
        bm.free();
        bpy.ops.object.modifier_apply(modifier=v_group_name);
    
    elif(invalid_indices.shape[0] == vpos.shape[0]):
        print('NO SOLUTION AVAILABLE FROM THIS MAPPING');
    else:
        setMeshVPOS(apply_on_mesh, constraint_positions);
            
    return getMeshVPos(apply_on_mesh);
    def createConstraintsVisual(self, context, mesh):
        useprimitive = False
        referencemesh = None

        if (context.scene.landmarks_use_selection != ''):
            referencemesh = bpy.data.objects[
                context.scene.landmarks_use_selection]
        elif (self.markersource != ''
              and not self.markersource in '~PRIMITIVE~'):
            referencemesh = bpy.data.objects[self.markersource]
        else:
            useprimitive = True

        material = changeMarkerColor(mesh)
        unlinkedmaterial = changeUnlinkedMarkerColor(mesh)

        if (not context.mode == "OBJECT"):
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        deleteObjectWithMarkers(context, mesh)

        temp = -1

        mesh_loops = mesh.data.loops
        mesh_faces = mesh.data.polygons

        for index, marker in enumerate(mesh.generic_landmarks):
            markername = mesh.name + "_marker_" + str(marker.id)
            try:
                markerobj = context.data.objects[markername]
                createmarker = False
            except:
                createmarker = True

            if (marker.v_indices[0] == -1 and marker.v_indices[1] == -1
                    and marker.v_indices[-2] == -1):
                marker.v_indices[0], marker.v_indices[1], marker.v_indices[
                    2] = [
                        mesh_loops[lid].vertex_index
                        for lid in mesh_faces[marker.faceindex].loop_indices
                    ]

            vertex1 = mesh.data.vertices[marker.v_indices[0]].co
            vertex2 = mesh.data.vertices[marker.v_indices[1]].co
            vertex3 = mesh.data.vertices[marker.v_indices[2]].co

            location = getGeneralCartesianFromBarycentre(
                marker.v_ratios, [vertex1, vertex2, vertex3])
            marker.location = location

            if (useprimitive):
                bpy.ops.mesh.primitive_cube_add(location=location, radius=0.15)
            else:
                mk_mesh = bpy.data.meshes.new(mesh.name + "_marker_" +
                                              str(marker.id))
                # Create new object associated with the mesh
                ob_new = bpy.data.objects.new(
                    mesh.name + "_marker_" + str(marker.id), mk_mesh)
                ob_new.data = referencemesh.data.copy()
                ob_new.scale = referencemesh.scale
                # Link new object to the given scene and select it
                context.scene.objects.link(ob_new)
                bpy.ops.object.select_all(
                    action='DESELECT')  #deselect all object
                ob_new.select = True
                ob_new.location = location
                bpy.context.scene.objects.active = ob_new

#             markerobj = context.object;
            markerobj = context.active_object
            markerobj.is_visual_landmark = True
            markerobj.landmark_id = marker.id
            markerobj.name = mesh.name + "_marker_" + str(marker.id)
            markerobj.belongs_to = mesh.name

            markerobj.data.materials.clear()

            if (marker.is_linked):
                markerobj.data.materials.append(material)
            else:
                markerobj.data.materials.append(unlinkedmaterial)

            bpy.ops.object.select_all(action='DESELECT')  #deselect all object
            markerobj.parent = mesh

            if (marker.id > temp):
                temp = marker.id

#         for area in context.screen.areas: # iterate through areas in current screen
#             if area.type == 'VIEW_3D':
#                 for space in area.spaces: # iterate through spaces in current VIEW_3D area
#                     if space.type == 'VIEW_3D': # check if space is a 3D view
#                         space.viewport_shade = 'SOLID' # set the viewport shading to rendered

        context.scene.objects.active = mesh
    def createConstraintsVisual(self, context, mesh):
        useprimitive = False;
        referencemesh = None;
        
        if(context.scene.landmarks_use_selection != ''):
            referencemesh = bpy.data.objects[context.scene.landmarks_use_selection];
        elif(self.markersource != '' and not self.markersource in '~PRIMITIVE~'):
            referencemesh = bpy.data.objects[self.markersource];    
        else:
            useprimitive = True;
        
        
        material = changeMarkerColor(mesh);
        unlinkedmaterial = changeUnlinkedMarkerColor(mesh);
        
        if(not context.mode == "OBJECT"):
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False);
        
        deleteObjectWithMarkers(context, mesh);
        
        temp = -1;
        
        for index, marker in enumerate(mesh.generic_landmarks):            
            markername = mesh.name + "_marker_"+str(marker.id);
            try:
                markerobj = context.data.objects[markername];
                createmarker = False;
            except:
                createmarker = True;
            
            vertex1 = mesh.data.vertices[marker.v_indices[0]].co;
            vertex2 = mesh.data.vertices[marker.v_indices[1]].co;
            vertex3 = mesh.data.vertices[marker.v_indices[2]].co;
            
            location = getGeneralCartesianFromBarycentre(marker.v_ratios, [vertex1, vertex2, vertex3]);
            marker.location = location;
            
            if(useprimitive):
                bpy.ops.mesh.primitive_cube_add(location=location, radius = 0.15);
            else:
                mk_mesh = bpy.data.meshes.new(mesh.name + "_marker_"+str(marker.id));
                # Create new object associated with the mesh
                ob_new = bpy.data.objects.new(mesh.name + "_marker_"+str(marker.id), mk_mesh);
                ob_new.data = referencemesh.data.copy();
                ob_new.scale = referencemesh.scale;
                # Link new object to the given scene and select it
                context.scene.objects.link(ob_new);
                bpy.ops.object.select_all(action='DESELECT') #deselect all object
                ob_new.select = True;
                ob_new.location = location;
                bpy.context.scene.objects.active = ob_new;
                
#             markerobj = context.object;
            markerobj = context.active_object;
            markerobj.is_visual_landmark = True;
            markerobj.landmark_id = marker.id;
            markerobj.name = mesh.name + "_marker_"+str(marker.id);
            markerobj.belongs_to = mesh.name;
            
            markerobj.data.materials.clear();
            
            if(marker.is_linked):
                markerobj.data.materials.append(material);
            else:
                markerobj.data.materials.append(unlinkedmaterial);
                
            bpy.ops.object.select_all(action='DESELECT') #deselect all object            
            markerobj.parent = mesh;
            
            if(marker.id > temp):
                temp = marker.id;
            
        for area in context.screen.areas: # iterate through areas in current screen
            if area.type == 'VIEW_3D':
                for space in area.spaces: # iterate through spaces in current VIEW_3D area
                    if space.type == 'VIEW_3D': # check if space is a 3D view
                        space.viewport_shade = 'SOLID' # set the viewport shading to rendered
        
        context.scene.objects.active = mesh;