Maya Python:使用 for 循环重命名对象 [英] Maya Python: Renaming object with for loops

查看:111
本文介绍了Maya Python:使用 for 循环重命名对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在看 Josh Sobel Python Scripting 教程,我一直在尝试让 For 循环在某些功能上工作:就像当你使用 nHair 使用毛囊创建一个带状脊柱时,你需要毛囊重新- 命名.我的目标是让 For 循环查找以某个名称前缀开头的名称,列出它们,然后重新命名列表中的对象.我确实有一些编码方法可以满足我的要求:但与 For 循环不同,它们很长、很笨拙,并且占用大量代码.这是我到目前为止所得到的:

so I've been watching the Josh Sobel Python Scripting tutorial and I've been trying to get For loops to work on certain functions: Like when you use nHair to create a ribbon spine using hair follicles and you need the follicles re-named. My goal is to get the For loop to look for names starting with a certain name preifx, list them, then reName the objects in the list. I've do have coding methods that do what I ask for: but unlike For loops they are long, clunky, and take up alot of code. Here is what I've got so far:

'''
import DS_jointRename_Demo
reload (DS_jointRename_Demo)
DS_jointRename_Demo.gui()
'''

import re
import maya.cmds as cmds
import maya.mel as mel

if cmds.window("renameWin", exists =True):
    cmds.deleteUI("renameWin", window = True)

myWindow = cmds.window("renameWin",t='DS_jointRename_Demo',w=200, h=500, toolbox=True)
column = cmds.columnLayout(adj=True)

def gui():

    cmds.button( label="Build Examples", c = buildExamples)
    cmds.separator( w=200, h=3)
    cmds.button( label="renameHeiarchy", c = renameHeiarchy)
    cmds.separator( w=200, h=9)
    cmds.button( label="renameGroupCont", c = renameGroupCont)
    cmds.separator( w=200, h=9)

    cmds.setParent('..')
    cmds.showWindow(myWindow)

def buildExamples(*args):
    cmds.joint(n='exampleGarbage_name11293')
    cmds.joint(n='exampleGarbage_name11397')
    cmds.joint(n='exampleGarbage_name15763')
    cmds.joint(n='exampleGarbage_name11548')
    cmds.joint(n='exampleGarbage_name11837')
    cmds.group(n='exampleGroup1',world=True,empty=True)
    cmds.parent('exampleGarbage_name11293','exampleGroup1')

    cmds.group(n='exampleGroup2',world=True,empty=True)
    cmds.joint(n='exampleWaste_name11293')

    cmds.joint(n='exampleWaste_name12973')
    cmds.parent('exampleWaste_name12973','exampleGroup2')

    cmds.joint(n='exampleWaste_name94563')
    cmds.parent('exampleWaste_name94563','exampleGroup2')

    cmds.joint(n='exampleWaste_name96397')
    cmds.parent('exampleWaste_name96397','exampleGroup2')

    cmds.joint(n='exampleWaste_name49456')
    cmds.parent('exampleWaste_name49456','exampleGroup2')

def renameHeiarchy(*args):
    garbageList = cmds.ls('exampleGarbage_name*')
    for i in garbageList:
        print garbageList
        cmds.rename(garbageList,'exampleGroup1_joint')

def renameGroupCont(*args):
    wasteList = cmds.ls('exampleWaste_name*')
    for i in wasteList:
        print wasteList
        cmds.rename(wasteList,'exampleGroup2_joint')

我想让这个脚本做的是在你按下构建示例"之后我想 renameHeiarchy 将 exampleGroup1 中的关节层次重命名为exampleGroup1_joint plus number increment"

What I would like to have this script do is after you press "Build Examples" I would like renameHeiarchy to rename the joint hierarchy in exampleGroup1 to "exampleGroup1_joint plus number increment"

exampleGroup2,与其他人不同的是,它的关节不在一个层次结构中:而是在组之下.无论哪种方式,当我运行构建示例"下的任一按钮时,我都会收到# 错误:对象或值太多."

exampleGroup2, unlike one has it's joints not parented in a heiarchy: but just under the group. Either way when I run either button under "Build Examples" I get "# Error: Too many objects or values."

我只是想让 for 循环重命名每个组的内容.如果您的 For 循环对它们都适用,则奖励

I just want the for loops to rename the contents of each group. Bonus if your For Loops works on both of them

推荐答案

rename 命令需要两个字符串,但您给它一个列表作为第一个参数.试试这个:cmds.rename(i,'exampleGroup2_joint')

The rename command expects two strings, but you give it a list as first argument. Try this one: cmds.rename(i,'exampleGroup2_joint')

这篇关于Maya Python:使用 for 循环重命名对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆