更具体地讲,我想弄清楚如何: name.replace XX,其中y如果x存在,如果没有则只需更换Xname.replace XX,其中y如果x存在
我已经现在搜索这个论坛一个小时,把它变成两个,我发现的是如何用另一个替换一个东西,现在很容易。
/A
更具体地讲,我想弄清楚如何: name.replace XX,其中y如果x存在,如果没有则只需更换Xname.replace XX,其中y如果x存在
我已经现在搜索这个论坛一个小时,把它变成两个,我发现的是如何用另一个替换一个东西,现在很容易。
/A
听起来像是正则表达式x?X
工作:
>>> import re
>>> text = " test xX blabla"
>>> re.sub('x?X', 'y', text)
' test y blabla'
>>> text = " test X blabla"
>>> re.sub('x?X', 'y', text)
' test y blabla'
?
报价从docs约?
马克:
问号字符,匹配一次或零次;你可以认为它是标记为可选项。例如, home-?brew匹配自制或自酿啤酒。
从你上面的例子来看,这是一个稍微更复杂的问题。您必须确保在根名称空间中执行重命名,否则事情会变得恶劣。您还有可能在孩子面前重新命名父母,这将使得只有一次打电话给ls的孩子很难。所以:
def replace_with_any_namespace(src, tgt):
cmds.namespace(set=":")
results = {}
xforms = cmds.ls(r=True, tr=True, l=True) # use long paths and recursive to get all namespaces
xforms = [i for i in xforms if src in i] # only work on items with your target pattern
xforms.sort()
xforms.reverse() # sort and reverse means children get renamed before parents
for item in xforms:
path, sep, shortname = item.rpartition("|") # gets only the last name
newname = shortname.replace(src, tgt) # this should be fine even if the namespace is there
results[item] = cmds.ls(cmds.rename (item, newname), l=True)[0]
# the paths and returns are all long paths so there are no ambiguities
return results
你是否试图用这个东西移出他们的命名空间?这很容易:
cmds.namespace(mv = ("R", ":"), force=True)
它将R:*中的所有内容都移动到基本名称空间。但是,这可能会导致一些重命名。在你调用这个函数之前,你可能想把重要的节点放到一个集合中,这样你就可以找到它们。
你有写过任何代码吗? – jsalonen
你有没有输入和期望输出的例子? – thegrinner
好我有这样的:
'脚本(X,Y)
名称列表= cmds.ls( “%S *” %X,TR = TRUE)
\t在名称列表名称:
\t \t baseName的= name.replace('%s'%x,'')
' 我希望python和maya采用R:bn和bn节点,用eng_替换* bn - 之前所有的bn和之前的一些原因是文件中有R:bn,其他文件只有bn。这意味着脚本崩溃,如果它是其中一个脚本被设置为另一个脚本的话。 我目前正在清理我的脚本,我希望它是多才多艺的。 – andreasng