
//////ReAnchor_Selected_Layers, version 1
////// by CRGreen (dot com)
//  LOOKS FOR SINGLE-VERTEX MASK ON EACH SELECTED LAYER AND CHANGES THE ANCHOR POINT OF THE LAYER TO THAT POINT (PRESERVING PERCEPTUAL POSITION)
// Assumes that there is only one such a mask for this specific purpose (uses first SVM it finds)
// Will produce undesirable results if layers are unevenly scaled and/or parented.
// Prompts user for SVM deletion
// left to do, if I feel like it:
// make scale-aware, see below
mainLayerReAnchor();

function mainLayerReAnchor()
{
	// make sure a comp is selected
	var alertMsg = "";
	var activeItem = app.project.activeItem;
	if (activeItem == null || !(activeItem instanceof CompItem)){
		alert("You need to select some layers first.");
	} else {
		var selectedLayers = activeItem.selectedLayers;
		var selNum = activeItem.selectedLayers.length;
		if (selNum == 0) {
			alert("No layers selected.");
		} else {
			var ess = "";
			if (selNum > 1) {ess = "s" ;}
			amMsg="I ignored at least one layer.";
			var SVMRemove = confirm("Delete Single-Vertex Mask" + ess + " when done?");
			
			//parent/scale check here:
			// loop through all selected layers;
			// check for non-100% scales; (can correct by temp parent) [might want to just check and warn or kill to begin with]
			// check for parent for all; (ok as long as parent scale == layers' scale)
			// check for dissimilar scales; (kill script)
			// check for dissimilar parenting (kill script)
			////////////////////////
			
			app.beginUndoGroup("ReAnchor Layer" + ess);
			/////////////////////////////////////////////////////////////
			for (var la = (selNum-1); la >= 0; la--) {//working backwards here.
				
				// make sure it is not a light or camera; must be solid or footage item
				if (selectedLayers[la].adjustmentLayer == undefined) {
					//ignore, but warn after
					alertMsg = amMsg;
				} else {
					//how many masks?
					currLayer = selectedLayers[la];
					mm = currLayer.Masks.numProperties;
					if (mm < 1) {
						// ignore this one; no masks
						if (alertMsg == "") { alertMsg = amMsg;}
					} else {
						itsMask=null;
						for (m = 1; m <= mm; m++) {
							//cycle through all masks until we find a single-vertex mask
							if (currLayer.mask(m).property("ADBE Mask Shape").value.vertices.length == 1) {
								reanchorLayer(currLayer, m);
								if (SVMRemove) { currLayer.mask(m).remove(); }
								break;
							} else {
								//last mask and still no svm -- so much for this layer
								if ( (m==mm) && (alertMsg == "") ) { alertMsg = amMsg;}
							}
						}
					}
				}
			}
				if (alertMsg != "") {alert(alertMsg);}
			/////////////////////////////////////////////////////////////
			app.endUndoGroup();
		}
	}
}

function getLoc(theLayer, maskIndex) {
	vv = theLayer.mask(maskIndex).property("ADBE Mask Shape").value.vertices;
	thisVert = vv[0];
	return thisVert;// (new center)
}

function reanchorLayer(theL, itsM) {
	// checks for position and anchor point difference, and
	// adds offset for that
	startingPos = theL.position.value;
	startingAP = theL.anchorPoint.value;
	posOffset = [0, 0];
	
	if (startingPos != startingAP) { posOffset = -1 * (startingAP - startingPos); }
	
	newCenter = getLoc(currLayer, itsM);
	theL.anchorPoint.setValue(newCenter);
	theL.position.setValue(newCenter + posOffset);
}

