Here's a variation of a tool I used in games production.
Works with multi-selections and will apply the color found in the polygon
cluster’s PPG. This allows you to choose your colors at will and as often
as you like. Just copy n’ paste this code into a file called
“ML_VertexColorFromPolygonCluster.js” and drop into the ‘plugins’ folder of any
workgroup to start using. Will appear in Model/Animate/Render > [Get]
Property > Map Paint Tools > ML VertexColor from PolygonCluster. Can
also be called from scripting.
You don’t want to use rendermap because it’ll have precision errors and
result in color bleed in some tight spots or when there are polygon
islands.
Matt
//-------------------start of script
(JScript)-----------------------------------------------
//========================================================================================
//
ML_VertexColorFromPolygonCluster()
//
// Transfers color from polygons in polygon
cluster to vertex color property using cluster color.
//========================================================================================
//===================================================================
// Constants()
//===================================================================
function Constants()
{
this.PRG =
"ML_VertexColorFromPolygonCluster";
this.MENU_LABEL = "ML
VertexColor from PolygonCluster";
}
//===================================================================
// XSILoadPlugin() - Registers plugin with
XSI
//
//===================================================================
function XSILoadPlugin( oPluginRegistrar
)
{
var oConstants = new
Constants();
oPluginRegistrar.Author
= "Matt Lind";
oPluginRegistrar.Name = oConstants.PRG;
oPluginRegistrar.Email = "";
oPluginRegistrar.URL = "";
oPluginRegistrar.Major = 1;
oPluginRegistrar.Minor = 0;
oPluginRegistrar.RegisterCommand( oConstants.PRG, oConstants.PRG );
oPluginRegistrar.RegisterMenu( siMenuTbGetPropertyMapPaintID, oConstants.PRG +
"Menu", false, false ); // Model > [Modify]
Component
return( true
);
}
//===================================================================
// XSIUnloadPlugin() - Removes plugin from
XSI
//
//===================================================================
function XSIUnloadPlugin( oPluginRegistrar
)
{
return( true
);
}
//===================================================================
// Menu_Init()
//
//===================================================================
function
ML_VertexColorFromPolygonClusterMenu_Init( oContext )
{
var oConstants = new
Constants();
var
oMenu = oContext.Source;
// Event to call when
user chooses tool from the interface
oMenu.AddCallbackItem(
oConstants.MENU_LABEL, oConstants.PRG + "Menu_OnClicked" );
}
//===================================================================
// Menu_OnClicked()
//
//===================================================================
function
ML_VertexColorFromPolygonClusterMenu_OnClicked( oContext )
{
var oConstants = new
Constants();
// Prompt user to pick
inputs and outputs
var aArguments = new
Array();
aArguments[0] =
Selection;
if ( aArguments == null
) {
LogMessage( oConstants.PRG + " cancelled" );
return( true );
}
// Launch the
command
var oCustomOperator =
Application.ExecuteCommand( oConstants.PRG, aArguments );
return( true
);
}
//===================================================================
// Init() - Executed when command invoked
1st time after loading in XSI.
//
//===================================================================
function
ML_VertexColorFromPolygonCluster_Init( oContext )
{
var oCommand =
oContext.Source;
oCommand.Tooltip = "Transfers color from polygons in
polygon cluster to vertex color property.";
oCommand.Description =
oCommand.Tooltip + " Colors derived from the polygon cluster
property.";
oCommand.ReturnValue =
true;
// Add
arguments
var oArguments =
oCommand.Arguments;
oArguments.AddWithHandler( "oSourceObjects", siArgHandlerCollection
);
return( true
);
}
//===================================================================
// Term() - Executed when
terminated.
//
//===================================================================
function
ML_VertexColorFromPolygonCluster_Term( oContext )
{
var oCommand =
oContext.Source;
}
//==========================================================================
// main()
//
//==========================================================================
function
ML_VertexColorFromPolygonCluster_Execute( oSourceObjects )
{
var oPolygonMeshes =
SIFilter( oSourceObjects, siPolyMeshFilter, true, siQuickSearch );
if ( !oPolygonMeshes ||
oPolygonMeshes.Count <= 0 ) {
LogMessage( "Invalid selection", siError );
return(-1);
}
for ( var i = 0; i <
oPolygonMeshes.Count; i++ ) {
var oPolygonMesh = oPolygonMeshes(i);
WriteColor( oPolygonMesh );
}
return(0);
}
//=========================================================
// WriteColor()
//
//=========================================================
function WriteColor( oPolygonMesh
)
{
var
oPolygons =
oPolygonMesh.ActivePrimitive.Geometry.Polygons;
var oSampleClusters =
oPolygonMesh.ActivePrimitive.Geometry.Clusters.Filter( "sample" );
var
oVertexColor = null;
// Look for existing
vertex color clusterproperty
for ( var i = 0; i <
oSampleClusters.Count && !oVertexColor; i++ ) {
var oSampleCluster = oSampleClusters(i);
var oVertexColors = oSampleCluster.Properties.Filter( "vertexcolor"
);
if ( oVertexColors.Count > 0 ) {
var oVertexColor = oVertexColors(0);
}
}
// Verify
if ( !oVertexColor )
{
if ( !oSampleClusters || oSampleClusters.Count <= 0 ) {
var oSampleCluster = oPolygonMesh.ActivePrimitive.Geometry.AddCluster(
siSampledPointCluster );
oSampleCluster.Name = "SampleCluster";
}
// create vertex color clusterproperty
var oVertexColor = oSampleCluster.AddProperty( "Vertex color"
);
oVertexColor.Name = "PolygonClusterColors";
}
var
oVertexColorData =
XSIFactory.CreateGridData();
oVertexColorData.RowCount =
oPolygonMesh.ActivePrimitive.Geometry.Nodes.Count;
oVertexColorData.ColumnCount = 4;
//------------------------------------
// Get polygons from
polygon clusters
//------------------------------------
var oPolygonClusters =
oPolygonMesh.ActivePrimitive.Geometry.Clusters.Filter( "poly" );
for ( var i = 0; i <
oPolygonClusters.Count; i++ ) {
var oPolygonCluster = oPolygonClusters(i);
// get polygon cluster color
var aColor = new Array(
( oPolygonCluster.Parameters( "Red" ).value / 255.0 ),
( oPolygonCluster.Parameters( "Green" ).value / 255.0 ),
( oPolygonCluster.Parameters( "Blue" ).value / 255.0 ),
1.0
);
var aPolygonIndices = ( oPolygonCluster.Elements.Array ).toArray();
for ( var j = 0; j < aPolygonIndices.length; j++ ) {
var PolygonIndex = aPolygonIndices[j];
var oPolygon = oPolygons( PolygonIndex );
var oNodes = oPolygon.Nodes;
for ( var k = 0; k < oNodes.Count; k++ ) {
oVertexColorData.SetRowValues( oNodes(k).Index, aColor );
}
}
}
oVertexColor.Elements.Array = oVertexColorData.Data;
return(0);
}
//------------------------ end of script (JScript)
---------------------------------
Date: Tue, 4 Aug 2015 01:33:51 -0300
Subject: Color at Vertices from Clusters