/*
naApplyVertexToNewPoly.mel v1.0.0
Last Modified: November 2, 2011
Author: Nathaniel Anozie
ogbonnawork at gmail dot com
nathanielanozie dot blogspot dot com
Purpose: copy one geometry to a new geometry with identical vertex labels.
-source 'naApplyVertexToNewPoly.mel'
-select geometry (the things we want to copy)
-type 'naCopyGeometry({-5.0,0.0,0.0},`ls -sl`,"default_geo");' (first number is x,y,z offset for duped geometry)
(last element name for the model that will be duped and modified to look like selected geos)
*/
/////duplicate geometry to new geometry (application to blendhshape meshes)
//where you want new meshes, things to find vert positions, base mesh that will be converted to new copies
//ex: naCopyGeometry({-5.0,0.0,0.0},`ls -sl`,"default_geo");
/*
in order to edit blendshapes i found that
if blendshapes were made via other deformers that
the editing through tweaks was messing up.
this tool allows blendshapes to be made in any way (maintaining verts)
a copy is made with translations only, and the resulting meshes
should be used for making blendshape deformer
currently supports
no input connections to blendshapes
*/
global proc naCopyGeometry(float $offsetArray[], string $searchArray[], string $replace )
{
string $sel[] = `ls -sl`;
//get items where your search geo is if translation offset not of needed length
float $offset[] = {0.0,0.0,0.0};
if(size($offsetArray) == 3){
$offset = $offsetArray;
}
//make copies of replace for all search geometries
//and positions of search geometries to new geometries
for( $i=0; $i<size($searchArray); $i++){
string $search = $searchArray[$i];
select -cl;
select -r $replace;
string $newPoly = ($search+"_"+$i);
duplicate -rr -n $newPoly;
float $initialPosSearch[] = `xform -q -ws -t $search`;
float $initialPos[];
$initialPos[0] = $initialPosSearch[0] + $offset[0];
$initialPos[1] = $initialPosSearch[1] + $offset[1];
$initialPos[2] = $initialPosSearch[2] + $offset[2];
select -cl;
select -r $newPoly;
move -rpr $initialPos[0] $initialPos[1] $initialPos[2];
//copy vertices position to new poly
naApplyVertexToNewPoly($search, $newPoly);
}
select -r $sel;
}
/////used for blendshapes, when you want to make editing of blendshapes faster and we don't want to limit
/////how blendshapes were created
/*
search: puts its vtx positions on new polgyon
newPoly: is new polygon identical vertex labels as replace and search
*/
global proc naApplyVertexToNewPoly(string $search, string $newPoly)
{
//move vtx of new polygon to the search polygon vtx
string $searchVtx[] = getVtxFromPoly($search);
string $newVtx[] = getVtxFromPoly($newPoly);
string $searchFace[] = getFaceFromVertex($searchVtx);
string $newFace[] = getFaceFromVertex($newVtx);
if( size($searchFace) == size($newFace) ){
for($i=0; $i<size($newFace); $i++){
//go through the face vertices
//while there move the vertices to where they should be
string $searchFaceVtx[] = getVertexFromFace({$searchFace[$i]});
string $newFaceVtx[] = getVertexFromFace({$newFace[$i]});
for($j=0; $j<size($searchFaceVtx); $j++){
float $searchPos[] = `xform -q -os -t $searchFaceVtx[$j]`;
float $translateX = $searchPos[0];
float $translateY = $searchPos[1];
float $translateZ = $searchPos[2];
xform -os -t $translateX $translateY $translateZ $newFaceVtx[$j];
}
clear($searchFaceVtx);
}
}
else{
error("number vertices not equal");
}
}
/*
/////given face array return expanded vertices
*/
global proc string[] getVertexFromFace(string $face[])
{
string $result[];
string $sel[] = `ls -sl`;
string $component[] = `polyListComponentConversion -ff -tv $face`;
select -r $component;
$result = `filterExpand -sm 31 -expand true`;
select -r $sel;
return $result;
}
/*
/////given vertex array return expanded faces
*/
global proc string[] getFaceFromVertex(string $vtx[])
{
string $result[];
string $sel[] = `ls -sl`;
string $component[] = `polyListComponentConversion -fv -tf $vtx`;
select -r $component;
$result = `filterExpand -sm 34 -expand true`;
select -r $sel;
return $result;
}
/////return vertices given a polygon selection
/*
often user may need to user vertices from a polygon selection
this script helps with returning that
*/
global proc string[] getVtxFromPoly(string $poly)
{
string $result[];
string $sel[] = `ls -sl`;
//will be altering user scene
//with constaint command
//so added check necessary
if(`objExists $poly`){
print "hii";
select -cl;
select -r $poly;
polySelectConstraint -type 0x0001 -shell true -m 3;//mode no constraint used
string $expanded[] = `ls -sl`;
$result = `filterExpand -sm 31 -expand true`;
polySelectConstraint -shell false;
select -cl;
select -r $sel;
}
else
{
warning("no poly exists");
}
return $result;
}