2017-02-18 70 views
0

試圖使我創建的腳本能夠多次運行。如果我運行它,一旦它的工作原理,但我第二次獲得:如果存在對象在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 ++)。如果我可以在這個位置上做同樣的事情,那麼每次腳本運行時鏈的連續增長都是非常棒的。

回答

0

,追加#的名稱會自動遞增名稱:

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'] 

但是瑪雅人不會讓您在層次結構的同一級別有兩個具有相同名稱的項目,因此它可能決定更改您的編號方案以避免名稱衝突。運行上述代碼兩次將生成更多變換名爲example_11example_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_1top_node_2但他們倆都必須通過child10命名child1孩子。我已經手動增加了名字,因爲我確信在top_node_#之下不會發生衝突,但只有在您確實知道同一層級中的任何內容都沒有相同名稱的情況下才可靠。

因此,對於您的示例,您需要創建帶有#後綴的頂級組,以便他們可以預測後綴,然後在創建子組時手動增加子組的名稱。在你的情況下,你不會有直接在頂層組下創建節點的選擇 - 你必須在創建和養育之後重新命名它們。您還可以通過使用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) 
0

你必須設置你的命名約定:當您在瑪雅對象

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) 
相關問題