PDA

View Full Version : findCurrentLayer function



faulknermano
12-13-2008, 03:22 AM
i don't know if this is old news or an existing better code than this... but if you want a workaround to get the layer number of an item in Layout, just feed the item's object agent into this function.


findCurrentLayer: item
{
itemPolyCount = item.polygonCount();
for(i=1;i<=item.totallayers;i++)
{
thisLayerCount = item.polygonCount(i);
if(thisLayerCount == itemPolyCount)
return(i);
}
return(nil);
}

the main the obvious problem of this is that it relies on polygon counts to differentiate between layers. so if two layers have identical poly counts, it won't work. heh.:neener:

Dodgy
12-13-2008, 03:51 AM
Yeah the stupid thing is you need the layer id for other lscript functions, like bounding box etc, but you can't get it in any way which guarantees it's the right one.

evenflcw
12-13-2008, 08:37 AM
Nice initiative. My version. Should work unless NT changes how unnamed layers are handled in layout. The code could certainly be prettier.


//Get index for selected layer
getLayerIndex: item
{
mesh = Mesh( item.id );
if( !mesh )
return nil;

( objname, layername ) = parse( ":", item.name );

index = 0;
//if layer doesn't have an assigned name.
if( strleft( layername, 5 ) == "Layer" ) {
index = (layername - "Layer").asInt();
}

i = 0;
//if layer does have an assigned name.
while( !index ) {
if( layername == mesh.layerName( ++i ) ) {
index = i;
}
}

return index;
}

faulknermano
12-13-2008, 08:23 PM
great! will check this out and apply it to my own stuff here. :)

what hoops we go through eh?

faulknermano
12-24-2008, 07:46 AM
added an if() check in case there there is no layername (e.g. single mesh object)



...
if(layername) // if() check put in by me
{
if( strleft( layername, 5 ) == "Layer" )
{
index = (layername - "Layer").asInt();
}
}

...