#!/usr/bin/env python
"""Bisect every edge meeting certain points

Find all the vertices within a threshold distance of given locations, 
and bisect every edge ending in one of these vertices, inserting new 
faces so that it doesn't change the static appearance.

The new vertices in the middle of the edges have the average of the 
values of their 'parents'.

This was invented for cases where giving new bones and weights to meshes 
being imported causes certain locations where vertices are to be 
troublesome, since every vertex there needs to have the same bones and 
weights so that splits do not open up, but the other vertices connected to 
want to be in different submeshes with different bones, reducing the options 
available at this location until there's nothing suitable. In this case, 
having shorter edges and more vertices will allow things to change more 
gradually and possibly solve the problem.

The first argument is the mesh to modify and the second argument is where 
to write the result, this can be the same as the first.

The third reaction is the threshold distance.

Other arguments come in threes, being co-ordinates which we will operate 
for every vertex within the threshold distance.
"""

import sys

import vb

if __name__ == '__main__':
	mesh = vb.Mesh(sys.argv[1])
	thresh = float(sys.argv[3])
	locs = []
	for i in range(4, len(sys.argv), 3):
		locs.append([float(sys.argv[k].strip('[],')) for k in range(i, i+3)])
	if mesh.bisect_around_points(thresh, locs):
		mesh.write(sys.argv[2])
	else:
		print ('No vertices were close enough')

