import bpy
import mathutils

class SaveMusclesAbsoluteOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.save_muscles_absolute_operator"
    bl_label = "Save muscles absolute positions"
    filepath = bpy.props.StringProperty(subtype="FILE_PATH")

    @classmethod
    def poll(cls, context):
        return context.active_object is not None and context.active_object.type == 'MESH'

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

    def execute(self, context):
        muscles = [obj for obj in bpy.data.objects if obj.type == 'MUSCLE' and obj.parent is None and obj.data.type == 'LINEAR']
        muscles2 = [obj for obj in bpy.data.objects if obj.type == 'MUSCLE' and obj.data.type == 'PARALLEL']
        muscles3 = [obj for obj in bpy.data.objects if obj.type == 'MUSCLE' and obj.data.type == 'SPHINCTER']
        obj = context.active_object
        f = open(self.filepath, 'w')
        f.write("%d\n" % (len(muscles) + len(muscles2) + len(muscles3)))
        # linear
        for muscle in muscles:
            origin = muscle.location.copy()
            children = muscle.children
            if len(children) != 1:
                continue
            insertion = children[0].location.copy()
            insertion += origin
            f.write("%s\n" % muscle.name)
            md = muscle.data
            f.write("%s %f %f %f %f %f %f %f %f %f %f\n" % (md.type, origin[0], origin[1], origin[2], insertion[0], insertion[1], insertion[2], md.omega, md.fs, md.ff, md.k))
        for muscle in muscles2:
            children = muscle.children
            if len(children) != 2:
                continue
            cp1 = children[0].location.copy()
            cp2 = children[1].location.copy()
            origin = muscle.location.copy()
            cp1 += origin
            cp2 += origin
            f.write("%s\n" % muscle.name)
            md = muscle.data
            f.write("%s %f %f %f %f %f %f %f %f %f %f %f %f %f\n" % (md.type, origin[0], origin[1], origin[2], cp1[0], cp1[1], cp1[2], cp2[0], cp2[1], cp2[2], md.height, md.fs, md.ff, md.k))
        for muscle in muscles3:
            children = muscle.children
            if len(children) != 2:
                continue
            cp1 = children[0].location.copy()
            cp2 = children[1].location.copy()
            origin = muscle.location.copy()
            cp1 += origin
            cp2 += origin
            f.write("%s\n" % muscle.name)
            md = muscle.data
            f.write("%s %f %f %f %f %f %f %f %f %f %f %f %f %f\n" % (md.type, origin[0], origin[1], origin[2], cp1[0], cp1[1], cp1[2], cp2[0], cp2[1], cp2[2], md.fs, md.ff, md.r, md.h))

        f.close()
        #print(bpy.data.muscles)
        #context.mesh
        return {'FINISHED'}

class SaveKeypointsOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.save_keypoints_operator"
    bl_label = "Save keypoints"
    filepath = bpy.props.StringProperty(subtype="FILE_PATH")

    @classmethod
    def poll(cls, context):
        return context.active_object is not None and context.active_object.type == 'MESH'

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}

    def execute(self, context):
        obj = context.active_object
        gi = obj.vertex_groups['keypoints'].index
        result = []
        for v in obj.data.vertices:
            for g in v.groups:
                if g.group == gi:
                    result.append(v)
        f = open(self.filepath, 'w')
        f.write("%d\n" % len(result))
        for v in result:
            f.write("%d %f %f %f\n" % (v.index, v.co[0], v.co[1], v.co[2]))
        f.close()
        return {'FINISHED'}