PDA

View Full Version : Bone Renumbering-- does anybody care?



jeric_synergy
05-18-2014, 03:09 PM
So, once you've used SPLIT BONE a couple times, your neat sequential order of default Bone names is jumbled up.

EG, 15,1,14,2,13, etc.

It'd be nice if one could tell Layout, "Renumber these bones based on their place in the hierarchy. And make it snappy."

OTOH, is that just ridiculous? Something no scripter should waste their time on? :twak:

Kryslin
05-18-2014, 04:18 PM
Or maybe this is something that every scripter has tackled at least once?

I've a renaming lscript that will read a hierarchy, add a prefix, suffix, replace text, strip numbers (the (#) lightwave adds to duplicate item names specifically), enumerate, and how many digit places you want to every bone in the hierarchy...

If you're interested, I'll clean it up, write some instructions, and post it...

jeric_synergy
05-18-2014, 05:01 PM
Thanks, Kryslin, I'm sure we'd all benefit. :)

Kryslin
05-18-2014, 07:01 PM
I'll get on it. If I don't have it up by Tuesday night, it won't be up until later next week (Vacation time, and I got a boatload to do...).

Kryslin
05-19-2014, 06:13 AM
Here we go, my renamer script.


@warnings
@name sp_Renamer
// Steve's Renamer Script
// Version 1.1
// By Steven Pettit, Summer 2013

mycounter;

generic{
prefflg = false;
prefstr = "";
replflg = false;
findstr = "";
replstr = "";
sufflg = false;
suffstr = "";
stripflg = false;
enumflg = false;
mycounter = 1;
mypad = 3;

reqbegin("Renaming Tool V1.1");
reqsize(380,260);
c1 = ctlcheckbox("Add Prefix",prefflg);
ctlposition(c1,10,10);
c2 = ctlstring("Prefix",prefstr);
ctlposition(c2,210,10);
c3 = ctlcheckbox("Find/Replace",replflg);
ctlposition(c3,10,35);
c4 = ctlstring("Find",findstr);
ctlposition(c4,210,35);
c5 = ctlstring("Replace",replstr);
ctlposition(c5,210,60);
c6 = ctlcheckbox("Add Suffix",sufflg);
ctlposition(c6,10,85);
c7 = ctlstring("Suffix",suffstr);
ctlposition(c7,210,85);
c8 = ctlcheckbox("Strip Existing Digits",stripflg);
ctlposition(c8,10,110);
c9 = ctlcheckbox("Enumerate",enumflg);
ctlposition(c9,10,135);
c10 = ctlinteger("Starting From",mycounter);
ctlposition(c10,200,135);
c11 = ctlinteger("How many digits", mypad);
ctlposition(c11,10,160);

return if !reqpost();

prefflg = getvalue(c1);
prefstr = getvalue(c2);
replflg = getvalue(c3);
findstr = getvalue(c4);
replstr = getvalue(c5);
sufflg = getvalue(c6);
suffstr = getvalue(c7);
stripflg = getvalue(c8);
enumflg = getvalue(c9);
mycounter = getvalue(c10);
mypad = getvalue(c11);

reqend();

myarray=Scene().getSelect();
if(!myarray) error("Please select one or more items");

things = 0;
count = myarray.size();

//Set flags from values in requester
flags = (enumflg * 16) + (sufflg * 8) + (replflg * 4) + (prefflg * 2) + stripflg;

if (count == 1)
{
//Recurse Items in Tree
tmp_i = myarray[1];
recursive_rename(tmp_i, flags, findstr, replstr, prefstr, suffstr, mypad);
} else {
i = count;
while (i > 0)
{
tmp_i = myarray[i];
if( isnameable(tmp_i)){
newname = myrenamefunc(tmp_i.name,flags,findstr,replstr,pref str,suffstr, mypad);
SelectItem(tmp_i.id);
Rename(newname);
}
i--;
}
}
}

shiftr : value, right
{
return(floor(value / (2 ^^ right)));
}

isnameable : object
{
retv = true;
if(object.isMesh())
{
if(object.null)
{
//Get Name Data for Nulls
retv= true;
} else {
//Meshes get their file names
retv = false;
}
}
return(retv);
}


myrenamefunc: oldname,flags,findstr,replstr,prefstr,suffstr,padc nt
{
//Init flags;
strip = replace = suffix = prefix = enum = 0;
t_flags = flags;

//Extract Flag Values
//flags = (enumflg << 4) | (sufflg << 3) | (replflg << 2) | (prefflg << 1) | stripflg;
strip = t_flags % 2; t_flags = shiftr(t_flags,1);
prefix = t_flags % 2;t_flags = shiftr(t_flags,1);
replace = t_flags % 2; t_flags = shiftr(t_flags,1);
suffix = t_flags % 2; t_flags = shiftr(t_flags,1);
enum = t_flags % 2;

// Do the grunt work...
// Strip Digits...
if (strip) {
if(oldname == s~\(.+\)~)
{
//If in here, it's been found...
s1 = oldname.indexOf('(');
oldname = strleft(oldname, s1 - 1);
}
}
// Do Search and Replace Stuff
if (replace) {
rexpr = regexp(findstr,replstr);
oldname ~= rexpr;
}

//Clean out Whitespace at beginning and end
oldname.pack();
oldname.trunc();

//Add our Suffix...
if (suffix) oldname = oldname + suffstr;

//And the Prefix...
if (prefix) oldname = prefstr + oldname;

//Add our numbers...
if (enum){
tmpcnt = format("$1",mycounter);
while (tmpcnt.size() < padcnt)
{
tmpcnt = "0" + tmpcnt;
}
mycounter += 1;
tmpcnt = "_" + tmpcnt;
oldname = oldname + tmpcnt;
}
return(oldname);
}

recursive_rename : object, flags, findstr, replstr, prefstr, suffstr, padcnt
{
if( isnameable(object)){
newname = myrenamefunc(object.name,flags,findstr,replstr,pre fstr,suffstr, padcnt);
SelectItem(object.id);
Rename(newname);
}
mychild = object.firstChild();
while(mychild)
{
recursive_rename(mychild, flags, findstr, replstr, prefstr, suffstr, padcnt);
mychild = object.nextChild();
}
}



Installation:
1) Copy and Paste this to a text editor, save the file.
2) Install the script in Lightwave.

License:
Free to use, but at your own risk. If you modify the code, I'd like to see the changes, but won't be particularly heartbroken if I don't.

Notes:
Has worked in every version of Lightwave since 9.6, when the core of this script was written. Requires Regular Expression functionality to work - which should not be a problem with a majority of installations.

Instructions:
1) Select an item or items.
2) Run Script
3) Enable what options you want to perform, and enter in your data.
--If you selected one item, the script will process the entire hierarchy below that item.
--If you selected multiple items, the script will batch process those.

---Order of Operations---
1) Removes (###) that layout adds for items with duplicate names.
2) Does Find and Replace
3) Adds Prefix
4) Adds Suffix
5) Enumerates (with user defined padding).

---Finicky Bits---
Every script has them. In this script's case, it doesn't re-select the items it just processed. Also, it doesn't handle single items.

---Credit---
This was, in large part, inspired by one of Dodgy's lscripts, available from his website. I didn't like how it handled some things, so I rolled my own after dissecting his script.