PDA

View Full Version : SmartReplaceGN



faulknermano
12-24-2008, 07:49 AM
this script toggles between the null and the actual object. it will remember across invocations (e.g. the script tags it).

(i'm currently improving this to toggle between two different meshes, like a low-res vs a high-res mesh.)



@warnings 0
getLayerIndex: item
{
// code by Dan Dulberger (evenflow (evenflcw))
mesh = Mesh( item.id );
if( !mesh )
return nil;

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

index = 0;
//if layer doesn't have an assigned name.
if(layername) // if() check put in by me
{
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;
}

generic
{
//replace with null
s = Scene().getSelect();


for(i=1;i<=s.size();i++)
{
repnullfile = nil;
obj = s[i];
SelectItem(obj.id);
myLayer = getLayerIndex(obj);
objfilename = getRelativePath(obj.filename);

repnullfile = PEGN_findTag(obj,"FM_RepNullFile");


if(repnullfile == nil) // means not tagged previously
{
if(obj.null == false)
{
if(myLayer)
PEGN_tagThis(obj,"FM_RepNullFile",objfilename + "." + myLayer + ".");
else
PEGN_tagThis(obj,"FM_RepNullFile",objfilename);
}

}

if(obj.null == false)
{
CommandInput(string("ReplaceWithNull ",objfilename));
if(myLayer)
PEGN_tagThis(obj,"FM_RepNullFile",objfilename + "." + myLayer + ".");
else
PEGN_tagThis(obj,"FM_RepNullFile",objfilename);
}
else // means it is a null
{
lineTimes = 0;
if(repnullfile)
{
if(endsWith(repnullfile,"."))
{
pfile = parse(".",repnullfile);
layernum = pfile[pfile.size()];
// modify repnullfile to remove the layer number
stripSize = layernum.size() + 2;
finrepnullfile = strleft(repnullfile,repnullfile.size() - stripSize);
cmdstr = string("ReplaceObjectLayer ",layernum," ",getdir("Content") + getsep() + finrepnullfile);
}
else
{
cmdstr = string("ReplaceWithObject ",getdir("Content") + getsep() + repnullfile);
}
CommandInput(cmdstr);
}

}

}

}
getRelativePath: fn
{
// if the filename is in the content dir then provide the relative path; if not return the absoluate path
contentDir = getdir("Content");
if(startsWith(fn,contentDir) == true)
{
csize = contentDir.size()+2;
return(strsub(fn,csize,fn.size() - csize + 1));

}
else
return(fn);


}
PEGN_tagThis: myItem, TAG, TAGVAL
{
ti = 0;
tagged = false;
while(true)
{
// look for same tag first and overwrite
tag = myItem.getTag(++ti);
if(tagged == false)
{
if(tag.contains(TAG))
{
tagged = true;
myItem.setTag(ti,TAG+" "+TAGVAL);
continue;
}
if(tag == nil || tag == "")
{
tagged = true;
myItem.setTag(ti,TAG+" "+TAGVAL);
continue;
}
if(tag == nil)
break;
}
// if tag has already been written
if(tagged == true)
{
// empty the slot
if(tag.contains(TAG))
myItem.setTag(ti,"");
if(tag == nil)
break;
}
}

}
PEGN_findTag: myItem, TAG
{
ti = 0;
while(true)
{
myTag = myItem.getTag(++ti);
if(myTag == nil)
break;
if(startsWith(myTag,TAG))
{
finTag = stripPrefix(myTag, TAG);
return(finTag);
}
}
return(nil);
}
startsWith: line, str
{
if(!line)
return(false);
if(!str)
return(false);
line = line.asStr();
str = str.asStr();

ss = str.size();
if(strleft(line,ss) == str)
return(true);
else
return(false);
}
stripPrefix: str, stripStr
{
if(startsWith(str,stripStr))
{
a = strright(str,str.size() - stripStr.size() - 1);
return(a);
}
else
return(str);
}

endsWith: line, str
{
if(!line)
return(false);
if(!str)
return(false);
line = line.asStr();
str = str.asStr();
ss = str.size();
if(strright(line,ss) == str)
return(true);
else
return(false);
}