def mat_tris(mesh):
    """Returns dict: mat_name -> list_of_triangle_indices"""

    m_ps = {}

    idx_tri = 0
    for p in mesh.polygons:
        mat = mesh.materials[p.material_index] if mesh.materials else None
        mat_name = mat.name if mat else "__BDX_DEFAULT"
        if not mat_name in m_ps:
            m_ps[mat_name] = []

        m_ps[mat_name].append(idx_tri)
        idx_tri += 1

        if len(p.loop_indices) > 3:
            m_ps[mat_name].append(idx_tri)
            idx_tri += 1

    return m_ps