我正在制作一个脚本,在python中为maya程序制定一个命名约定。我将使用它来命名由我的脚本创建的所有对象。
例如,让我们采取一个左膝关节。脚本将这样的内容(“绑定”、“肩”、“左”、“联合”)传递到变量(前缀、名称、侧、obj_type)中。然后,此输入将通过用户字典运行,以检查现有值并更改为新值,如果没有找到,则返回原始值。例如,“联合”将变成"jnt“。
用户将输入类似的内容(prefix_、name_、SIDE_、obj_type,01)
我需要它,以便它将检查用户输入中是否存在变量名中的任何内容。例如,如果它在用户输入中找到任何变量名(例如“前缀”),那么在变量前缀所在位置的索引处替换包含在变量前缀中的任何内容。此外,字符串中没有找到的任何内容都将被单独保留,比如"01",它将简单地添加到每个名称中。
例如,上面的内容将返回这个"bn_shoulder_L_jnt01“
此外,如果某物是大写的,如第一个字母,或所有字母,我希望它自动大写所传递的字母。这就是为什么输入SIDE_会将"L“转换为"l”的原因。
我希望这是尽可能灵活的,但我目前的问题是,如果它找到了值,就会将变量替换到现有的字符串中。我试着想了几件事,但没有想出多少。这是我的代码:
此外,我目前还将用户输入传递给init中的类。它能在整个模块中工作并使用吗?我仍然不能100%确定init应该如何使用,但我希望当用户输入命名约定时,它将在需要时在内存中可用。
编辑守则:
from string import Template
class Name:
def __init__(self, user_conv):
self.user_conv = user_conv
def user_dict(self, word):
"""Matches given word with a dictionary, and returns converted abbreviated word.
Keyword Arguments:
string -- given string to be converted
example: joint > jnt
"""
# prefixes
user_library = {
'bind' : 'bn',
'driver' : 'drv',
# side
'back' : 'b',
'down' : 'd',
'front' : 'f',
'left' : 'l',
'right' : 'r',
'up' : 'u',
# obj_type
'cluster' : 'clstr',
'control' : 'ctrl',
'curve' : 'crv',
'effector' : 'efctr',
'group' : 'grp',
'ikHandle' : 'ikH',
'joint' : 'jnt',
'locator' : 'loc',
'nurbs' : 'geo',
'orientConstraint' : 'orCnstr',
'parentConstraint' : 'prntCnstr',
'pointConstraint' : 'ptCnstr',
'polyMesh' : 'geo',
# utilities
'addDoubleLinear' : 'adl',
'blendColors' : 'blndClr',
'BlendTwoAttr' : 'b2a',
'chooser' : 'chsr',
'clamp' : 'clmp',
'condition' : 'cn',
'curveInfo' : 'crvI',
'diffuse' : 'diffuse',
'displacement' : 'displ',
'multiplyDivide' : 'mdv',
'normal' : 'normal',
'place2d' : 'p2d',
'plusMinusAverage' : 'pma',
'reverse' : 'rv',
'setRange' : 'sr',
'shader' : 'shdr',
'shadingGroup' : 'SG',
'specular' : 'spec',
'transparency' : 'trans',
# sequential bones
'arm' : 'arm',
'fingser' : 'finger',
'index' : 'index',
'leg' : 'leg',
'limb' : 'limb',
'middle' : 'middle',
'pinky' : 'pinky',
'ring' : 'ring',
'spine' : 'spine',
'toe' : 'toe',
'thumb' : 'thumb',
#
'ankle' : 'ankle',
'ball' : 'ball',
'breast' : 'breast',
'chest' : 'chest',
'clavicle' : ' clavicle',
'elbow' : 'elbow',
'end' : 'e',
'head' : 'head',
'hair' : 'hair',
'knee' : 'knee',
'neck' : 'neck',
'pelvis' : 'pelvis',
'root' : 'root',
'shoulder' : 'shoulder',
'tail' : 'tail',
'thigh' : 'thigh',
'wrist' : 'wrist'
}
if word in user_library:
abbrevWord = user_library[word]
else:
abbrevWord = word
return [abbrevWord]
def convert(self, prefix, name, side, obj_type):
"""Converts given information about object into user specified naming 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'
"""
self.prefix = self.user_dict(prefix)
self.name = self.user_dict(name)
self.side = self.user_dict(side)
self.obj_type = self.user_dict(obj_type)
print '%s, %s, %s, %s' %(prefix, name, side, obj_type)
self.new_string = Template (self.user_conv.lower())
self.subs = {'prefix': prefix, 'name': name, 'side': side, 'obj_type': obj_type}
self.new_string.substitute(**self.subs)
print new_string
return new_string
test code:
# test file to test naming convention to see if its functioning properly
import neo_name
reload(neo_name)
def ui_test():
"""types user can input
#prefix
#name
#side
#type
constants (such as 01, present in ALL objects/nodes/etc.)
"""
user_conv = '${prefix}_${name}_${side}_${obj_type}${01}'
name = neo_name.Name(user_conv)
name.convert('bind', 'shoulder', 'left', 'joint')
ui_test()现在得到这个错误,不知道如何处理它:绑定、肩、左、关节追溯(最近一次调用):文件“C:\Users\Gregory\Documents\Gregory的文件夹\ not \3d Scripts_MyScripts\neoAutoRig\scripts\test_neo_name.py",第19行,在ui_test()文件”C:\Users\Gregory\Documents\Gregory的文件夹\Artwork\3d Scripts_MyScripts\neoAutoRig\scripts\test_neo_name.py",行17,在ui_test name.convert中(“绑定”、“肩”、“左”、“关节”)
文件“C:\Users\Gregory\ Files\Autodesk\Maya2014\bin\python27.zip\string.py",\Gregory的文件夹\ 133 \3d Files\Autodesk\Maya2014\bin\python27.zip\string.py",第133行,在self.new_string.substitute(前缀=前缀,名称=名称,side = side,obj_type = obj_type)文件”C:程序Files\Autodesk\Maya2014\bin\python27.zip\string.py“,第172行,替代文件C:\Files\Autodesk\Maya2014\bin\python27.zip\string.py”,行169,在“转换文件”C:\ Files\Autodesk\Maya2014\bin\python27.zip\string.py",第146行中,在_invalid ValueError中:字符串中的无效占位符:第1行,col 38
发布于 2014-09-25 23:08:28
对于python string.Template类来说,这也是一个很好的应用程序:
从字符串导入模板
t = Template ("${prefix}_${side}_${limb}")
t.substitute(prefix ='character', side='R', limb='hand')
>>> 'character_R_hand'您可以通过将分隔符视为条件(默认为'')、强制大写规则等来获得更好的效果。Template.substitute使用标志(如上面的示例)或字典:
t = Template ("${prefix}_${side}_${limb}")
subs = {'prefix':'dict', 'side':'L', 'limb':'bicep'}
t.substitute(**subs)
>>> 'dict_L_hand'发布于 2014-09-25 20:27:51
您必须创建一个字典,将字符串映射到实际的命令。
d = {'bind':'bn', 'left':'L', 'joint':'jnt'}
l = ['bind', 'shoulder', 'left', 'joint', '01']然后使用get,它接受第二个参数,如果它找不到那个键,就可以传递原始参数。然后将所有内容与'_'连接在一起。
'_'.join(d.get(i,i) for i in l)输出
'bn_shoulder_L_jnt_01'在你的情况下这将是
'_'.join(userLibrary.get(word,word) for word in prefixes)https://stackoverflow.com/questions/26047435
复制相似问题