嵌套命名空间删除

时间:2015-05-04 11:10:15

标签: python namespaces maya

我正在使用脚本来删除命名空间编辑器(嵌套或非嵌套),而不使用/打开命名空间编辑器,假设条件在其中没有内容的情况下得到满足。

在这样做的时候,我遇到了这个问题,我无法使用cmds.namespace(rm="<string of the namespace>")

删除嵌套的命名空间

我找到了一个更长的解决方法,但我被卡住,因为输出是列表中的unicode,我似乎无法将其转换为字符串。

nsLs = cmds.namespaceInfo( lon=True)
# nsLs Result: [u'UI', u'camera01',  u'shared', u'v02', u'v03']

defaultNs = ["UI", "shared", "camera01"]

diffLs = [item for item in nsLs if item not in defaultNs]
# diffLs Result: [u'v02', u'v03']

for ns in diffLs:
    nsNest = cmds.namespaceInfo(ns, lon=True)
    # nsNest Result:    [u'v02:new_run01']
    #                   [u'v03:new_run01']
    cmds.namespace(rm=str(nsNest))

因此,我使用的'remove'标志不起作用,因为遇到以下错误:

# Error: No namespace matches name: '[u'v02:new_run01']'.
# Traceback (most recent call last):
#   File "<maya console>", line 13, in <module>
# RuntimeError: No namespace matches name: '[u'v02:new_run01']'. #

我输入的上述代码仅用于嵌套命名空间,虽然它仍然没有“有”实现结果而且也不是很灵活(假设在场景中只有1个嵌套级别),有没有如何纠正这个问题?

此外,感谢任何人有任何解决方案/方法删除命名空间而不使用命名空间编辑器,当然......

nestedNs

3 个答案:

答案 0 :(得分:0)

namespace命令需要一个字符串(在本例中为'v02:new_run01'),您将向其传递字符串化列表(在本例中为'[u'v02:new_run01']')。由于您有lon=True标志,该命令将始终返回一个列表。您应该确保从中提取元素并将其发送到namespace命令。

您所要做的就是从列表而不是整个列表中传递元素:

for ns in diffLs:
    nsNest = cmds.namespaceInfo(ns, lon=True)
    # nsNest Result:    [u'v02:new_run01']
    #                   [u'v03:new_run01']
    if nsNest:
        cmds.namespace(rm=nsNest[0])

希望有所帮助。

答案 1 :(得分:0)

这是下面的代码,它删除任何级别的嵌套命名空间,假设它持有空内容。

import maya.cmds as mc

defaults = ['UI', 'shared']

def num_children(ns):
    return ns.count(':')

namespaces = [ns for ns in mc.namespaceInfo(lon=True, r=True) if ns not in defaults]
sorted_ns = sorted(namespaces, key=num_children, reversed=True)
for ns in sorted_ns:
    try:
        mc.namespace(rm=ns)
    except RuntimeError as e:
        pass

感谢特别的朋友帮助解决这个问题:)

答案 2 :(得分:0)

要删除所有namespaces,您可能需要使用 recurse mergeNamespaceWithParent 标记。这样它就会删除所有名称空间,并将其作为root用户。

# Gathering and deleting all namespaces
name_space = [item for item in pm.namespaceInfo(lon=True, recurse=True) if item not in ["UI", "shared"]]
# Sort them from child to parent, That's order we need to delete
sorted_ns_namespace = sorted(name_space, key=lambda ns: ns.count(':'), reverse=True)

for ns in sorted_ns_namespace:
    pm.namespace(removeNamespace=ns, mergeNamespaceWithParent=True)