SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.


SM Forums / Maya Questions / Programming / Automatically rename objects and their children

Programming MEL Scripts, Expressions & Programming Related Topics

Join the conversation. Reply to post
 
Thread Tools
27-07-2018, 01:02 PM   #1
lauris47
Registered User
 
Join Date: Sep 2011
Posts: 109
Automatically rename objects and their children
Hi, I have an issue,
I have 2000 objects and some of them have children.
At max each object has two more children.
At minimum, objects has one child.

Could someone help in renaming them with Mel or Python?

I want them named for example:
Object1_LOD0
Object1_LOD1
Object1_LOD2

Object2_LOD0
Object2_LOD1

etc.

Thank you.
Attached Images
File Type: jpg Rename children.jpg (71.2 KB, 3 views)
__________________
www.laurynaslubys.com
  Reply with quote
30-07-2018, 11:19 AM   #2
lauris47
Registered User
 
Join Date: Sep 2011
Posts: 109
My wonderful colleague created this script in Python for me. It does exactly what I have described above.
I have attached it.


import maya.cmds as mc
import os

def rename_lod(path, name, n):
if "LOD" in name:
name = name[:-5]
print "Renaming", path, "to", name
rels = mc.listRelatives(path, children=True, fullPath=True)
print rels
if len(rels) == 1:
mc.rename(path, name+'_LOD'+str(n))
return
elif len(rels) > 2:
print "Too many children in one layer"
os.exit(1)
else:
child = rels[1]
rename_lod(child, name, n+1)

mc.rename(path, name+'_LOD'+str(n))


sel=mc.ls(selection=True)

for each in sel:
try:
name = each.split('|')[1]
except:
name = each
rename_lod(each, name, 0)
Attached Files
File Type: zip Unity_LOD.zip (489 Bytes, 1 views)
__________________
www.laurynaslubys.com

Last edited by lauris47; 30-07-2018 at 11:22 AM.
  Reply with quote