/*___________________________________________________________________________
|
| BSpiritCorrectiveShape v1.2
|
| Copyright (C) 2004 Christian Breitling
| Last modified: Oktober 13, 2004 Germany
|
| Email: christian.breitling@gmx.de
|
|
| This program is free software; you can redistribute it and/or
| modify it under the terms of the GNU General Public License
| as published by the Free Software Foundation; either version 2
| of the License, or (at your option) any later version.
|
| This program is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with this program; if not, write to the Free Software
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| The GNU GPL is also available at http://www.gnu.org/licenses/gpl.html
|
|____________________________________________________________________________

What CorrectiveShape is for:
----------------------------
Create a corrective blend shape for your binded mesh.

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。


How is it working:
------------------
In general vertices in Maya behave different than normal if they are binded.
The rotation of a joint also affects the local space of the binded vertices.
Moving them in the viewport causes very funny effects. But for creating
corrective blend shapes normal behavior would be best. Duplicating the binded
mesh and applying it afterwards as blend shape causes wrong vertex behavior.
So the world position of the blend shape has to be translated to the local
space of the skinned mesh.

This is done by the following script wich uses an equitation to get the right
motion vector.


How to use it:
--------------

1. Installation:
----------------

- Copy this mel script in one of your script directories of Maya.

 

2. Normal Usage:
----------------

1. Move the skinned mesh to the desired pose. You might key all important
handles and joints for recalling them later.

2. Duplicate it without any connections.
The duplicate can be translated freely [you`ll have to unlock the transformation].
The original mesh mustn`t change [except you keyed the pose -> restore before calculation].

3. Model the appropriate corrections for this pose.

4. Select the newly created corrective shape and then the original mesh [in this order].

5. run "BSpiritCorrectiveShape" in the command line or script editor.

 

3. Advanced Usage:
------------------

To calculate and move only specific vertices:

1. - 3. Same procedure as above

4. Select the newly created corrective shape in "Object Mode".
Via right mouse button change to "Vertex Mode" and select the appropriate vertices.
Add the original mesh to your selection.

5. run "BSpiritCorrectiveShape" in the command line or script editor.

 


You will receive a perfect blend shape mesh. Assigning it is your task.
Don`t forget to change the input connection order. Blend shapes have to be
first (bottom) in the stack.

If anything could be better, nothing is functioning or you just want to write
me - don`t bother

Christian Breitling

christian.breitling@gmx.de

_____________________________________________________________________________
*/

 

// __________________________________________________________________________________________
// VertexPosition procedure
// __________________________________________________________________________________________


proc BSpiritCorrectiveShapeVectorMove( string $TweakVertexName, string $BaseModelVertexName,
float $WorldPosX, float $WorldPosY, float $WorldPosZ,
float $targetPosX, float $targetPosY, float $targetPosZ,
float $relPosX, float $relPosY, float $relPosZ ) {

float $tempPos[3];
float $Matrix[12];

setAttr ($TweakVertexName + "].xVertex") ($relPosX + 1);
$tempPos = `pointPosition -w $BaseModelVertexName`;
$Matrix[0] = $tempPos[0]-$WorldPosX;
$Matrix[4] = $tempPos[1]-$WorldPosY;
$Matrix[8] = $tempPos[2]-$WorldPosZ;
$Matrix[3] = $targetPosX;

setAttr ($TweakVertexName + "].xVertex") ($relPosX);
setAttr ($TweakVertexName + "].yVertex") ($relPosY + 1);
$tempPos = `pointPosition -w $BaseModelVertexName`;
$Matrix[1] = $tempPos[0]-$WorldPosX;
$Matrix[5] = $tempPos[1]-$WorldPosY;
$Matrix[9] = $tempPos[2]-$WorldPosZ;
$Matrix[7] = $targetPosY;


setAttr ($TweakVertexName + "].yVertex") ($relPosY);
setAttr ($TweakVertexName + "].zVertex") ($relPosZ + 1);
$tempPos = `pointPosition -w $BaseModelVertexName`;
$Matrix[2] = $tempPos[0]-$WorldPosX;
$Matrix[6] = $tempPos[1]-$WorldPosY;
$Matrix[10] = $tempPos[2]-$WorldPosZ;
$Matrix[11] = $targetPosZ;

float $return[3];
float $Denominator =

($Matrix[0] * (($Matrix[5]*$Matrix[10]) - ($Matrix[6]*$Matrix[9]))) -
($Matrix[1] * (($Matrix[4]*$Matrix[10]) - ($Matrix[6]*$Matrix[8]))) +
($Matrix[2] * (($Matrix[4]*$Matrix[9] ) - ($Matrix[5]*$Matrix[8])));

if ($Denominator != 0) {

$return[0] =

(
($Matrix[3] * (($Matrix[5]*$Matrix[10]) - ($Matrix[6]*$Matrix[9] ))) -
($Matrix[1] * (($Matrix[7]*$Matrix[10]) - ($Matrix[6]*$Matrix[11] ))) +
($Matrix[2] * (($Matrix[7]*$Matrix[9] ) - ($Matrix[5]*$Matrix[11] )))
) / $Denominator;

$return[1] =

(
($Matrix[0] * (($Matrix[7]*$Matrix[10]) - ($Matrix[6]*$Matrix[11] ))) -
($Matrix[3] * (($Matrix[4]*$Matrix[10]) - ($Matrix[6]*$Matrix[8] ))) +
($Matrix[2] * (($Matrix[4]*$Matrix[11]) - ($Matrix[7]*$Matrix[8] )))
) / $Denominator;

$return[2] =

(
($Matrix[0] * (($Matrix[5]*$Matrix[11]) - ($Matrix[7]*$Matrix[9] ))) -
($Matrix[1] * (($Matrix[4]*$Matrix[11]) - ($Matrix[7]*$Matrix[8] ))) +
($Matrix[3] * (($Matrix[4]*$Matrix[9] ) - ($Matrix[5]*$Matrix[8] )))
) / $Denominator;

setAttr ($TweakVertexName + "].xVertex") ($relPosX + $return[0]);
setAttr ($TweakVertexName + "].yVertex") ($relPosY + $return[1]);
setAttr ($TweakVertexName + "].zVertex") ($relPosX + $return[2]);

}
}


// __________________________________________________________________________________________
// VertexMatrix procedure
// __________________________________________________________________________________________

proc float[] BSpiritCorrectiveShapePosition( string $BlendShapeVertexName,
string $BaseModelVertexName,
float $OffsetX, float $OffsetY, float $OffsetZ) {

float $targetPos[3];
float $Pos[3];
float $relVertexPos[3];

$targetPos = `pointPosition -w $BlendShapeVertexName`;
$Pos = `pointPosition -w $BaseModelVertexName`;
$relVertexPos = `getAttr $BaseModelVertexName`;

$targetPos[0] -= ($Pos[0]+$OffsetX);
$targetPos[1] -= ($Pos[1]+$OffsetY);
$targetPos[2] -= ($Pos[2]+$OffsetZ);

if (($targetPos[0]>0.001) || ($targetPos[0]<-0.001) ||
($targetPos[1]>0.001) || ($targetPos[1]<-0.001) ||
($targetPos[2]>0.001) || ($targetPos[2]<-0.001))

return { 1.0,
$Pos[0], $Pos[1], $Pos[2],
$targetPos[0], $targetPos[1], $targetPos[2],
$relVertexPos[0], $relVertexPos[1], $relVertexPos[2] };

else

return { -1.0 };

}

// __________________________________________________________________________________________
// Main Procedure
// __________________________________________________________________________________________

global proc BSpiritCorrectiveShape() {

// __________________________________________________________________________________________
// Declaring main variables & Query names and selections
// __________________________________________________________________________________________

string $SelectedVertexArray[] = `filterExpand -sm 31`; // Vertex
string $SelectedItemArray[] = `ls -selection -long`; // All - with hierarchy
string $SelectedMeshArray[] = `filterExpand -sm 12`; // Polygon
string $BaseModel;
string $BlendShape;

int $SelectedVertexNumberArray[];
int $SelectionTrue = 0;
int $BREAK = 0;
int $FailureList = 0; // [0: Everything OK] [1: Selection is false] [2: Skin is missing] [3: User abort] [4: PolyCount not identical] [5: Nothing to Move]

int $VertexCount = `size $SelectedVertexArray`;
int $ItemCount = `size $SelectedItemArray`;
int $MeshCount = `size $SelectedMeshArray`;

if (($VertexCount > 0) && ($MeshCount == 1)) {

$SelectionTrue = 1;
string $Temp[];
$BaseModel = $SelectedItemArray[($ItemCount - 1)];
tokenize $SelectedItemArray[0] "." $Temp;
$BlendShape = $Temp[0];
tokenize $SelectedVertexArray[0] "[" $Temp;
int $TempPrefixSize = `size $Temp[0]` + 2;

for ($i = 0; $i < ($VertexCount); $i++) {

string $TempString = $SelectedVertexArray[$i];
int $TempSuffixSize = `size $TempString` - 1;
$SelectedVertexNumberArray[$i] = `substring $TempString $TempPrefixSize $TempSuffixSize`;

}
}

else if (($MeshCount == 2) && ($ItemCount == 2)) {

$BlendShape = $SelectedItemArray[0];
$BaseModel = $SelectedItemArray[1];

}

else {

$BREAK = 1;
$FailureList = 1;

}

// __________________________________________________________________________________________
// Finding appropriate skinClusters & tweakNode
// __________________________________________________________________________________________


string $TweakNode;
string $Skin[];

float $Progress = 0.0;

if (! $BREAK) {

string $BaseHistory[] = `listHistory`;

int $HistorySize = `size $BaseHistory`;

for ($i = 0; $i < $HistorySize; $i++) {

string $TempHistory = `nodeType $BaseHistory[$i]`;

if ($TempHistory == "tweak")
$TweakNode = $BaseHistory[$i];
else if ($TempHistory == "skinCluster")
$Skin[`size $Skin`] = $BaseHistory[$i];

}

if ($TweakNode == "" || $Skin[0] == "") {

$BREAK = 1;
$FailureList = 2;

}
}

// __________________________________________________________________________________________
// Initializing & PolyCount
// __________________________________________________________________________________________

float $Offset[3];

int $BaseVertexCount[];

string $vertexNameArray[];
string $TweakVertexArray[];
string $BaseModellVertexNameArray[];


if (! $BREAK) {

progressWindow

-title "BSpirit Corrective Shape"
-progress $Progress
-status "Calculating VertexMovements"
-isInterruptable true
-min 0
-max 100;

float $BaseTempOffset[3];
float $BlendTempOffset[3];

$BaseVertexCount = `polyEvaluate -v $BaseModel`;
$BaseTempOffset = `getAttr ($BaseModel+".translate")`;
$BlendTempOffset = `getAttr ($BlendShape+".translate")`;

$Offset[0] = $BlendTempOffset[0] - $BaseTempOffset[0];
$Offset[1] = $BlendTempOffset[1] - $BaseTempOffset[1];
$Offset[2] = $BlendTempOffset[2] - $BaseTempOffset[2];


int $BlendVertexCount[] = `polyEvaluate -v $BlendShape`;

string $ShapeNode[] = `pickWalk -d down $BaseModel`;

if (! `connectionInfo -isDestination ($ShapeNode[0] + ".tweakLocation")`)
connectAttr ($TweakNode + ".vlist[0].vertex[0]") ($ShapeNode[0] + ".tweakLocation");


if ($BaseVertexCount[0] != $BlendVertexCount[0]) {

$BREAK = 1;
$FailureList = 4;

}
}

// __________________________________________________________________________________________
// Query VertexPosition
// __________________________________________________________________________________________

float $targetPosArray[];
float $absPosArray[];
float $relPosArray[];

if (! $BREAK) {

if (! $SelectionTrue) {

$VertexCount = $BaseVertexCount[0];

}

for ($i = 0; $i < $VertexCount; $i++) {

if ( `progressWindow -query -isCancelled` ) {

$BREAK = 1;
$FailureList = 3;
break;

}

int $LastProgress = $Progress;
$Progress = `trunc (20.0 * $i / $VertexCount + 0.5)`;

if ($LastProgress != $Progress)
progressWindow -e -progress $Progress;

string $VertexAppendix;

if (! $SelectionTrue)
$SelectedVertexNumberArray[$i] = $i;

$VertexAppendix = ".vtx[" + $SelectedVertexNumberArray[$i] + "]";

string $BaseModelVertexName = $BaseModel + $VertexAppendix;
string $BlendShapeVertexName = $BlendShape + $VertexAppendix;

float $PositionArray[10];

$PositionArray = `BSpiritCorrectiveShapePosition $BlendShapeVertexName $BaseModelVertexName $Offset[0] $Offset[1] $Offset[2]`;

if ($PositionArray[0] == 1) {

int $vertexArraySize = `size $vertexNameArray` * 3;

$vertexNameArray[$vertexArraySize / 3] = $BaseModelVertexName;
$TweakVertexArray[$vertexArraySize / 3] = $TweakNode + ".vlist[0].vertex[" + $SelectedVertexNumberArray[$i];

$absPosArray[$vertexArraySize] = $PositionArray[1];
$absPosArray[$vertexArraySize + 1] = $PositionArray[2];
$absPosArray[$vertexArraySize + 2] = $PositionArray[3];

$targetPosArray[$vertexArraySize] = $PositionArray[4];
$targetPosArray[$vertexArraySize + 1] = $PositionArray[5];
$targetPosArray[$vertexArraySize + 2] = $PositionArray[6];

$relPosArray[$vertexArraySize] = $PositionArray[7];
$relPosArray[$vertexArraySize + 1] = $PositionArray[8];
$relPosArray[$vertexArraySize + 2] = $PositionArray[9];

}
}
}

// __________________________________________________________________________________________
// Ask user about high VertexMovements
// __________________________________________________________________________________________

int $vertexNameArraySize = `size $vertexNameArray`;
int $PercentMoved;

if ($vertexNameArraySize > 0)
$PercentMoved = `trunc (0.5 + $vertexNameArraySize / $VertexCount * 100)`;

else {

$BREAK = 1;
$FailureList = 5;

}


if ($PercentMoved > 50) {

if (`confirmDialog

-title "Moving Vertex"
-message ("About " + $PercentMoved +"% vertices will be moved. Proceed?")
-button "Proceed" -button "Abort"
-defaultButton "Proceed" -cancelButton "Abort"
-dismissString "Abort"`

== "Abort") {
$BREAK = 1;
$FailureList = 3;

}
}


// __________________________________________________________________________________________
// Calculating vertexMatrix & moving
// __________________________________________________________________________________________


if (! $BREAK) {

progressWindow -e -status ("Moving " + $vertexNameArraySize + " vertices");


for ($i = 0; $i < $vertexNameArraySize; $i++) {

if ( `progressWindow -query -isCancelled` ) {

$BREAK = 1;
$FailureList = 3;
break;

}


int $LastProgress = $Progress;
$Progress = `trunc (20.0 + 60.0 * $i / $vertexNameArraySize + 0.5)`;

if ($LastProgress != $Progress)
progressWindow -e -progress $Progress;

int $ArrayPos = $i * 3;

BSpiritCorrectiveShapeVectorMove $TweakVertexArray[$ArrayPos / 3] $vertexNameArray[$ArrayPos / 3]
$absPosArray[$ArrayPos] $absPosArray[$ArrayPos + 1] $absPosArray[$ArrayPos + 2]
$targetPosArray[$ArrayPos] $targetPosArray[$ArrayPos + 1] $targetPosArray[$ArrayPos + 2]
$relPosArray[$ArrayPos] $relPosArray[$ArrayPos + 1] $relPosArray[$ArrayPos + 2];

}
}


// __________________________________________________________________________________________
// Duplicating baseMesh
// __________________________________________________________________________________________


string $AllInputs[] = `listHistory $BaseModel`;
string $MutedTypes[] = {"skinCluster", "blendShape"};


if (! $BREAK) {

for ($i = 0; $i < `size $AllInputs`; $i++){
for ($z = 0; $z < `size $MutedTypes`; $z++){

if (`nodeType $AllInputs[$i]` == $MutedTypes[$z])
setAttr ($AllInputs[$i] + ".nodeState") 1;

}
}

string $BlendDuplicate[] = `duplicate -rc -rr $BaseModel`;
string $UnlockAttributes[] = {".tx",".ty",".tz",".rx",".ry",".rz",".sx",".sy",".sz"};

for ($i = 0; $i < 9; $i++) {

setAttr -lock 0 ($BlendDuplicate[0] + $UnlockAttributes[$i]);

}

select -r $BlendDuplicate[0];

}


// __________________________________________________________________________________________
// Reseting baseMesh
// __________________________________________________________________________________________

progressWindow -e -st "Reseting BaseMesh";

for ($i = 0; $i < $vertexNameArraySize; $i++) {

int $LastProgress = $Progress;
$Progress = `trunc (80.0 + 20.0 * $i / $vertexNameArraySize + 0.5)`;

if ($LastProgress != $Progress)
progressWindow -e -progress $Progress;

int $ArrayPos = $i * 3;

setAttr ($TweakVertexArray[$ArrayPos / 3] + "].xVertex") $relPosArray[$ArrayPos];
setAttr ($TweakVertexArray[$ArrayPos / 3] + "].yVertex") $relPosArray[$ArrayPos + 1];
setAttr ($TweakVertexArray[$ArrayPos / 3] + "].zVertex") $relPosArray[$ArrayPos + 2];

}


for ($i = 0; $i < `size $AllInputs`; $i++){
for ($z = 0; $z < `size $MutedTypes`; $z++){

if (`nodeType $AllInputs[$i]` == $MutedTypes[$z])
setAttr ($AllInputs[$i] + ".nodeState") 0;

}
}

progressWindow -endProgress;

// __________________________________________________________________________________________
// Feedback to user
// __________________________________________________________________________________________

if ($FailureList != 0) {

string $Feedback;
switch ($FailureList) {

case 1:
$Feedback = "Wrong number of objects selected!";
break;
case 2:
$Feedback = "Second mesh has no skinCluster!";
break;
case 3:
$Feedback = "Procedure aborted by user!";
break;
case 4:
$Feedback = "Not identical amount of vertices!";
break;
case 5:
$Feedback = "Nothing to move!";
break;
}

confirmDialog -title "Failure - Procedure aborted"
-message $Feedback
-button "OK";

}
}
BSpiritCorrectiveShape;

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄