我正在使用Autodesk Maya程序。 我已经制定了一个命名约定脚本,它将相应地为某个约定中的每个项目命名。不过,我每次都在场景中列出它,然后检查所选名称是否与场景中的任何当前名称相匹配,然后重命名它并在场景中重新检查一次,如果有重复。在很长的时间内对一个大的unicode列表进行迭代?
但是,当我运行代码时,可能需要长达30秒到一分钟或更长时间才能完成所有操作。起初,我不知道是什么让我的代码运行缓慢,因为它在相对较低的场景中运行良好。但是,当我将打印语句放在检查场景代码中时,我发现检查场景中的所有项目需要很长时间,并检查重复项。
ls()命令提供场景中所有项目的unicode列表。如果场景中的物品数量适中,这些物品可能会相对较大,达到一千或更多,正常情景会比我目前的测试场景大几倍(其中有大约794个物品在此列表中)。
这应该需要这么长时间吗?我用来比较效率低下的方法吗?我不确定这里要做什么,代码需要花费过多的时间,我也想知道它是否可以是代码中的其他任何东西,但这看起来可能就是这样。
以下是一些代码。
class Name(object):
"""A naming convention class that runs passed arguments through user
dictionary, and returns formatted string of users input naming convention.
"""
def __init__(self, user_conv):
self.user_conv = user_conv
# an example of a user convention is '${prefix}_${name}_${side}_${objtype}'
@staticmethod
def abbrev_lib(word):
# a dictionary of abbreviated words is here, takes in a string
# and returns an abbreviated string, if not found return given string
@staticmethod
def check_scene(name):
"""Checks entire scene for same name. If duplicate exists,
Keyword Arguments:
name -- (string) name of object to be checked
"""
scene = ls()
match = [x for x in scene if isinstance(x, collections.Iterable)
and (name in x)]
if not match:
return name
else:
return ''
def convert(self, prefix, name, side, objtype):
"""Converts given information about object into user specified convention.
Keyword Arguments:
prefix -- what is prefixed before the name
name -- name of the object or node
side -- what side the object is on, example 'left' or 'right'
obj_type -- the type of the object, example 'joint' or 'multiplyDivide'
"""
prefix = self.abbrev_lib(prefix)
name = self.abbrev_lib(name)
side = ''.join([self.abbrev_lib(x) for x in side])
objtype = self.abbrev_lib(objtype)
i = 02
checked = ''
subs = {'prefix': prefix, 'name': name, 'side':
side, 'objtype': objtype}
while self.checked == '':
newname = Template (self.user_conv.lower())
newname = newname.safe_substitute(**subs)
newname = newname.strip('_')
newname = newname.replace('__', '_')
checked = self.check_scene(newname)
if checked == '' and i < 100:
subs['objtype'] = '%s%s' %(objtype, i)
i+=1
else:
break
return checked
你有没有做过任何分析,找出时间正在采取? – jonrsharpe 2014-10-31 19:28:07
不知道该怎么做,仍然是一种新的python和使用IDE。 – 2014-10-31 19:33:49
https://docs.python.org/2/library/profile.html – jonrsharpe 2014-10-31 19:42:02