I have a scene where I have lots of objects with duplicate names, but some of them I want to automatically enumerate by adding a number at the end so when I export they appear as separate meshes. I found a script that almost did what I wanted here: http://kyticka.webzdarma.cz/3d/moi/#SeparateObjectName but this would go through all objects in the scene, and it didn't zero pad the numbers. I've updated the script so it does exactly what I wanted:
Enumerate Object names (by adding a zero padded enumeration index to matching names):
(1) if no objects are selected then all objects are searched for matching names
(2) if there is a selection then if multiple objects of the same name are selected then only those objects that are selected are updated, if only one object of a name is selected then all objects with that matching name are updated. The multiple or single modes can both be done at once for different object names
Currently this operation if mapped to a key has no undo. Does anyone know how I can add undo functionality?
code:
script: /* Enumerate object names */
var selected_objects = moi.geometryDatabase.getSelectedObjects();
var all_objects = moi.geometryDatabase.getObjects();
var matched_names = new Object();
var selected_names = new Object ();
// if no objects are selected then update all objects
if (selected_objects.length == 0)
{
for ( var i = 0; i < all_objects.length; i++ )
{
var obj = all_objects.item(i);
if ( obj.name == '' ) { continue; }
if ( !matched_names[obj.name] )
{
matched_names[obj.name] = new Array();
}
matched_names[obj.name].push( obj );
}
}
// if there is a selection then use that to dictate which names to search for and update
else
{
for ( var j = 0; j < selected_objects.length; j++ )
{
var name = selected_objects.item(j).name;
if ( name == '') { continue; }
if (! selected_names[name] )
{
selected_names[name] = 0
}
selected_names[name]++;
}
for ( var name in selected_names)
{
var search_objects;
if (selected_names[name] == 1)
{
// if there is only one name selected then update all objects with this name
search_objects = all_objects;
}
else
{
// if there are multiple of the same name selected then only update those in the selection
search_objects = selected_objects;
}
matched_names[name] = new Array ();
for ( var i = 0; i < search_objects.length; i++ )
{
var obj = search_objects.item(i);
if ( obj.name == '' ) { continue; }
if ( obj.name == name )
{
matched_names[obj.name].push( obj );
}
}
}
}
// update the matched objects
for ( var name in matched_names )
{
if ( matched_names[name].length == 1 ) { continue; }
var num_names = matched_names[name].length;
var digits = Math.floor (Math.log(num_names)/Math.log(10))+1;
var zeros = "0000000000000000";
for ( var i = 0; i < num_names; i++ )
{
var padded_num = zeros + (i+1).toString ();
matched_names[name][i].name = name + ' ' + padded_num.slice (-digits);
}
}
(edit: updated [code][/code] to <code></code> version as suggested)
|