Tuesday 30 August 2016

split mesh based on per face shader assignments

SPLIT meshes based on per face shader assignments.

/* This file downloaded from Highend3d.com
''
''  Highend3d.com File Information:
''
''    Script Name: tt_splitGeoFromMultiShaderAssignments
''    Author: Tylney
''    Last Updated: Feb 02, 2007
''    Update/Change this file at:
''    http://Highend3d.com/maya/downloads/mel_scripts/modeling/poly_tools/1071.html
''
''  Please do not alter any information above this line
''  it is generated dynamically by Highend3d.com and will
''  be changed automatically on any updates.
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// written by Tylney Taylor, copyright 2005-2007 bold vfx pty ltd
//
// use: 1) to select splittable geo globally in scene,
// use command: tt_selectObjectsWithMultiShaderAssignments
//
// 2) then execute tt_splitGeoFromMultiShaderAssignments
//
// probably best do a shelf button or two :)
//
// This software is shareware and I'd appreciate any comments and/or feedback
// from use with this software.  I would also appreciate information on projects
// this siftware was used in
//
// Many thanks Tylney Taylor, tylney@bold-vfx.com
//
// version history:
//
// 1.0 - worked pretty much straight from the bat, and havent had to change it
// since extended use in production yet.
//
////////////////////////////////////////////////////////////////////////////////////////////////////

global proc tt_splitGeoFromMultiShaderAssignments()
{
string $selection[] = `ls -sl`;
string $shadingEngineNodes[0];
string $shadingEngineNode;
string $node;
select -clear;

print $selection;

if (size($selection)){
// get associated shaders then associated/connected fileNodes
//


for ($node in $selection){
// find the related Shape Nodes
//
string $shapeNodes[] = `listRelatives -s -path $node`;
string $shape;
select -r $node;


for ($shape in $shapeNodes){

// find connections
//
string $connections[] = `listConnections $shape`;
string $connection;

for ($connection in $connections){
if (`objectType $connection` == "shadingEngine"){
string $sourceConnections[] = `listConnections -d 0 $connection`; // find connections to upstream Nodes
string $connection;

for ($connection in $sourceConnections){
if ((`objectType $connection` != "mesh") && (`objectType $connection` != "transform")&& (`objectType $connection` != "groupId")){
$shadingEngineNodes[size($shadingEngineNodes)] = $connection;
}
}
}
}

$shadingEngineNodes = `stringArrayRemoveDuplicates($shadingEngineNodes)`;
$shadingEngineNodes = `sort $shadingEngineNodes`;
//print $shadingEngineNodes;
}

// ok, now extract the faces for each shading group
//
// so first find only the faces that are assigned to the shader on the current object
// we have to assume that the shader is also assigned to other geometry, so we will filter
// for only the current object
string $shadingEngineNode;

if (size($shadingEngineNodes) <= 1) break;

for ($shadingEngineNode in $shadingEngineNodes){
string $assignedObjects[];
string $assignedObject;
string $faces[];
clear $faces;

hyperShade -objects $shadingEngineNode;
$assignedObjects = `ls -sl`;

//print ($node + " ");

for ($assignedObject in $assignedObjects){
if ((`gmatch $assignedObject ($node + ".f*")`) && (`objExists $assignedObject`)){
//print ($assignedObject + " ");
$faces[size($faces)] = $assignedObject;
}
}
//print "\n";

// select faces and polyChipOff (extract keepFacesToghether)
//polyChipOff -ch 1 -kft 1 -dup 0 -pvx 0.4755284797 -pvy -0.07101976871 -pvz -0.6240176838 -tx 0 -ty 0 -tz 0 -rx 0 -ry 0 -rz 0 -sx 1 -sy 1 -sz 1 -ran 0 -off 0 -ltz 0 -ws 0 -ltx 0 -lty 0 -lrx 0 -lry 0 -lrz 0 -lsx 1 -lsy 1 -lsz 1 -ldx 1 -ldy 0 -ldz 0 -w 0 -gx 0 -gy -1 -gz 0 -att 0 -mx 0 -my 0 -mz 0 pSphere6.f[20:23] pSphere6.f[40:42] pSphere6.f[59:62] pSphere6.f[79:82] pSphere6.f[100:101] pSphere6.f[120:121] pSphere6.f[140:141] pSphere6.f[160:161] pSphere6.f[180:181] pSphere6.f[200:201] pSphere6.f[220:221] pSphere6.f[240:241] pSphere6.f[260:261] pSphere6.f[280:281] pSphere6.f[299];
select -r $faces;
if (size($faces)) polyChipOff -ch 0 -kft 1 -dup 0 $faces;
select -clear;
}

// ok now separate the object and re-assign the shader to all objects (not faces)
// - node is now only a transform
//
string $polySplitObjects[] = `polySeparate -ch 0 $node`;
string $polySplitObject;


for ($polySplitObject in $polySplitObjects){
//hyperShade -smn ""; Select the asigned Shaders
//select -cl; select -r -ne blinn1 anisotropic2 anisotropic1 phongE1 phong1;x
//print ("selected Split: " + $polySplitObject + "\n");

for ($shadingEngineNode in $shadingEngineNodes){
string $assignedObjects[0];
string $assignedObject;
string $faces[0];

hyperShade -objects $shadingEngineNode;
$assignedObjects = `ls -sl`;
$assignedObjects = `stringArrayRemoveDuplicates($assignedObjects)`;
$assignedObjects = `sort $assignedObjects`;

//print ("assigned Objects:\n");
//print $assignedObjects;


for ($assignedObject in $assignedObjects){
if (`gmatch $assignedObject ($polySplitObject + ".f*")`){
print ("assigning " + $shadingEngineNode + " to " + $polySplitObject + "\n");
select -r $polySplitObject;
hyperShade -assign $shadingEngineNode;

break;
}
}
}
}
}
}
select -clear;
}

global proc tt_selectObjectsWithMultiShaderAssignments()
{
hyperShadePanelMenuCommand("hyperShadePanel1", "selectShadingGroupsAndMaterials");
string $selection[] = `ls -sl`;
string $objects[0];
string $node;
select -clear;

if (size($selection)){
// get associated shaders then associated/connected fileNodes
//


for ($node in $selection){
string $assignedObjects[0];
string $assignedObject;
string $faces[0];

hyperShade -objects $node;
$assignedObjects = `ls -sl`;

for ($assignedObject in $assignedObjects){
if (`gmatch $assignedObject "*.f*"`){
$objects[size($objects)] = `match "^[^\.]*" $assignedObject`;
}
}

}
$objects = `stringArrayRemoveDuplicates($objects)`;
$objects = `sort $objects`;
//print ((size($objects)) + " Objects with multiple Shaders:\n");
//print $objects;
select -r $objects;

}
}