如果对象存在,则在 Maya 中添加一个新的对象名称 Python
if object exists add a new object name in Maya Python
尝试使我创建的脚本能够 运行 多次。如果我 运行 它一次有效,但第二次我得到:
Error: setAttr: Not enough data was provided. The last 0 items will be skipped.
Traceback (most recent call last):
File "<maya console>", line 8, in <module>
这是我的脚本
import maya.cmds as mc
#Create and place Spiral DNA elements
for x in range (0,20):
strandLName = "strandL" +str(x)
nucleoName = "nucleo" +str(x)
strandRName = "strandR" +str(x)
strandL,strandHistory = mc.polySphere(name=strandLName, ch=1)
nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
mc.setAttr(nucleoName + '.translateX', 5)
mc.setAttr(nucleoName + '.rotateZ', -90)
mc.setAttr(nucleoName + '.scaleX', 0.5)
mc.setAttr(nucleoName + '.scaleY', 5)
mc.setAttr(nucleoName + '.scaleZ', 0.5)
strandR,strandHistory = mc.polySphere(name=strandRName, ch=1)
mc.setAttr(strandRName + '.translateX', 10)
mc.select(deselect=1)
#create empty group
grp = mc.group(n=strandLName + 'NULL', em=1)
mc.select(deselect=1)
#Parent Elements to Group
nucleotide = mc.parent(strandL, nucleo, strandR, grp)[0]
#Move and rotate groups
mc.setAttr(grp + '.translateX', -5.5)
mc.xform(grp, cp=1)
mc.setAttr(grp + ".translateY", x * 2)
mc.setAttr(grp + ".ry", 15 * x)
mc.select(deselect=1)
我有 3 个对象已被分组,然后该组又被重复 19 次以创建螺旋 DNA 链。我想让名称相对,这样如果对象存在,它将根据已经存在的对象创建一个新对象(例如 strandLName 20 ++)。如果我能对位置做同样的事情,这样每次脚本 运行.
时链都会不断增长,那就太好了
您必须设置您的命名约定:
import maya.cmds as mc
class Counter:
iter = 0
def __init__(self):
Counter.iter += 1
iter = Counter().iter
name = "dna_{0:03d}_{{}}".format(iter)
rootDna = mc.group(n=name.format('grp'), em=1)
#Create and place Spiral DNA elements
for x in range (0,20):
strandLName = name.format("strandL" +str(x))
nucleoName = name.format("nucleo" +str(x))
strandRName = name.format("strandR" +str(x))
strandL,strandHistory = mc.polySphere(name=strandLName, ch=1)
nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
mc.setAttr(nucleoName + '.translateX', 5)
mc.setAttr(nucleoName + '.rotateZ', -90)
mc.setAttr(nucleoName + '.scaleX', 0.5)
mc.setAttr(nucleoName + '.scaleY', 5)
mc.setAttr(nucleoName + '.scaleZ', 0.5)
strandR,strandHistory = mc.polySphere(name=strandRName, ch=1)
mc.setAttr(strandRName + '.translateX', 10)
mc.select(deselect=1)
#create empty group
grp = mc.group(n=strandLName + 'NULL', em=1)
mc.select(deselect=1)
#Parent Elements to Group
nucleotide = mc.parent(strandL, nucleo, strandR, grp)[0]
#Move and rotate groups
mc.setAttr(grp + '.translateX', -5.5)
mc.xform(grp, cp=1)
mc.setAttr(grp + ".translateY", x * 2)
mc.setAttr(grp + ".ry", 15 * x)
mc.select(deselect=1)
mc.parent(grp, rootDna)
当您在 Maya 中创建 object 时,将 #
附加到名称将自动递增名称:
examples = [cmds.createNode('transform', n='example_#') for x in range(10)]
print examples
[u'example_1', u'example_2', u'example_3', u'example_4', u'example_5', u'example_6', u'example_7', u'example_8', u'example_9', u'example_10']
但是,maya 不会让您在层次结构的同一级别有两个同名的项目,因此它可能会决定更改您的编号方案以避免名称冲突。 运行 上面的代码两次将生成更多名为 example_11
到 example_20
的转换。
为避免名称冲突,您可以使用命名空间或顶级组:
topnode = cmds.createNode('transform', n= 'top_node_#')
# calling createNode with 'p=' makes the new items as children
for n in range(10):
cmds.createNode('transform', n= 'child%i' % n, p=topnode)
运行 这两次将生成 top_node_1
和 top_node_2
,但它们都将 children 命名为 child1
到 child10
。我已经手动增加了名称,因为我确定 top_node_#
下不会发生冲突,但只有当您确定同一层次结构级别的任何内容都不会具有相同的名称 stem 时,这才是可靠的。
因此,对于您的示例,您需要创建带有 #
后缀的 top-level 组,以便他们可以预见地获得后缀,然后手动增加 children 当您创建它们时。在您的情况下,您将无法选择直接在 top-level 组下创建节点——您必须在创建和父子关系后重命名它们。还可以通过使用xform
命令而不是直接设置属性
来简化代码很多
import maya.cmds as cmds
def dna_strand(number_of_pairs):
top_level = cmds.createNode('transform', n='dna_#')
for n in range(number_of_pairs):
left, _ = cmds.polySphere()
cmds.xform(t = (-5, 0,0 ))
right,_ = cmds.polySphere()
cmds.xform(t = (5, 0,0 ))
nucleo,_ = cmds.polyCylinder(h = 10, r=.5)
cmds.xform(nucleo, ro=(0,0,90))
group = cmds.group(left, right, nucleo)
# now these are under the group, naming is deterministic
cmds.rename(left, 'strandL%i' % n)
cmds.rename(right, 'strandR%i' % n)
cmds.rename(nucleo, 'nucleo%i' % n)
cmds.xform(group, t=(0,n * 2,0), ro = (0, 15 * n, 0))
cmds.parent(group, top_level, r=True)
# group is under `dna_x` so again you can manually rename
cmds.rename(group, "basepair%i" % n)
尝试使我创建的脚本能够 运行 多次。如果我 运行 它一次有效,但第二次我得到:
Error: setAttr: Not enough data was provided. The last 0 items will be skipped.
Traceback (most recent call last):
File "<maya console>", line 8, in <module>
这是我的脚本
import maya.cmds as mc
#Create and place Spiral DNA elements
for x in range (0,20):
strandLName = "strandL" +str(x)
nucleoName = "nucleo" +str(x)
strandRName = "strandR" +str(x)
strandL,strandHistory = mc.polySphere(name=strandLName, ch=1)
nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
mc.setAttr(nucleoName + '.translateX', 5)
mc.setAttr(nucleoName + '.rotateZ', -90)
mc.setAttr(nucleoName + '.scaleX', 0.5)
mc.setAttr(nucleoName + '.scaleY', 5)
mc.setAttr(nucleoName + '.scaleZ', 0.5)
strandR,strandHistory = mc.polySphere(name=strandRName, ch=1)
mc.setAttr(strandRName + '.translateX', 10)
mc.select(deselect=1)
#create empty group
grp = mc.group(n=strandLName + 'NULL', em=1)
mc.select(deselect=1)
#Parent Elements to Group
nucleotide = mc.parent(strandL, nucleo, strandR, grp)[0]
#Move and rotate groups
mc.setAttr(grp + '.translateX', -5.5)
mc.xform(grp, cp=1)
mc.setAttr(grp + ".translateY", x * 2)
mc.setAttr(grp + ".ry", 15 * x)
mc.select(deselect=1)
我有 3 个对象已被分组,然后该组又被重复 19 次以创建螺旋 DNA 链。我想让名称相对,这样如果对象存在,它将根据已经存在的对象创建一个新对象(例如 strandLName 20 ++)。如果我能对位置做同样的事情,这样每次脚本 运行.
时链都会不断增长,那就太好了您必须设置您的命名约定:
import maya.cmds as mc
class Counter:
iter = 0
def __init__(self):
Counter.iter += 1
iter = Counter().iter
name = "dna_{0:03d}_{{}}".format(iter)
rootDna = mc.group(n=name.format('grp'), em=1)
#Create and place Spiral DNA elements
for x in range (0,20):
strandLName = name.format("strandL" +str(x))
nucleoName = name.format("nucleo" +str(x))
strandRName = name.format("strandR" +str(x))
strandL,strandHistory = mc.polySphere(name=strandLName, ch=1)
nucleo,nucleoHistory = mc.polyCylinder(name=nucleoName, ch=1)
mc.setAttr(nucleoName + '.translateX', 5)
mc.setAttr(nucleoName + '.rotateZ', -90)
mc.setAttr(nucleoName + '.scaleX', 0.5)
mc.setAttr(nucleoName + '.scaleY', 5)
mc.setAttr(nucleoName + '.scaleZ', 0.5)
strandR,strandHistory = mc.polySphere(name=strandRName, ch=1)
mc.setAttr(strandRName + '.translateX', 10)
mc.select(deselect=1)
#create empty group
grp = mc.group(n=strandLName + 'NULL', em=1)
mc.select(deselect=1)
#Parent Elements to Group
nucleotide = mc.parent(strandL, nucleo, strandR, grp)[0]
#Move and rotate groups
mc.setAttr(grp + '.translateX', -5.5)
mc.xform(grp, cp=1)
mc.setAttr(grp + ".translateY", x * 2)
mc.setAttr(grp + ".ry", 15 * x)
mc.select(deselect=1)
mc.parent(grp, rootDna)
当您在 Maya 中创建 object 时,将 #
附加到名称将自动递增名称:
examples = [cmds.createNode('transform', n='example_#') for x in range(10)]
print examples
[u'example_1', u'example_2', u'example_3', u'example_4', u'example_5', u'example_6', u'example_7', u'example_8', u'example_9', u'example_10']
但是,maya 不会让您在层次结构的同一级别有两个同名的项目,因此它可能会决定更改您的编号方案以避免名称冲突。 运行 上面的代码两次将生成更多名为 example_11
到 example_20
的转换。
为避免名称冲突,您可以使用命名空间或顶级组:
topnode = cmds.createNode('transform', n= 'top_node_#')
# calling createNode with 'p=' makes the new items as children
for n in range(10):
cmds.createNode('transform', n= 'child%i' % n, p=topnode)
运行 这两次将生成 top_node_1
和 top_node_2
,但它们都将 children 命名为 child1
到 child10
。我已经手动增加了名称,因为我确定 top_node_#
下不会发生冲突,但只有当您确定同一层次结构级别的任何内容都不会具有相同的名称 stem 时,这才是可靠的。
因此,对于您的示例,您需要创建带有 #
后缀的 top-level 组,以便他们可以预见地获得后缀,然后手动增加 children 当您创建它们时。在您的情况下,您将无法选择直接在 top-level 组下创建节点——您必须在创建和父子关系后重命名它们。还可以通过使用xform
命令而不是直接设置属性
import maya.cmds as cmds
def dna_strand(number_of_pairs):
top_level = cmds.createNode('transform', n='dna_#')
for n in range(number_of_pairs):
left, _ = cmds.polySphere()
cmds.xform(t = (-5, 0,0 ))
right,_ = cmds.polySphere()
cmds.xform(t = (5, 0,0 ))
nucleo,_ = cmds.polyCylinder(h = 10, r=.5)
cmds.xform(nucleo, ro=(0,0,90))
group = cmds.group(left, right, nucleo)
# now these are under the group, naming is deterministic
cmds.rename(left, 'strandL%i' % n)
cmds.rename(right, 'strandR%i' % n)
cmds.rename(nucleo, 'nucleo%i' % n)
cmds.xform(group, t=(0,n * 2,0), ro = (0, 15 * n, 0))
cmds.parent(group, top_level, r=True)
# group is under `dna_x` so again you can manually rename
cmds.rename(group, "basepair%i" % n)