2017-12-15 8 views
2

人。私はこの単純なスクリプトを書いて、選択したオブジェクトにバックフェールのカリングを設定しました。しかし、選択したグループにもこれを適用する方法を理解できません。属性をグループにも設定しますか?

import maya.cmds as cmds 

sel = cmds.ls(sl = True) 
for i in range(len(sel)): 
    cmds.setAttr((sel[i] + '.backfaceCulling'), 0) 

答えて

2
import maya.cmds as cmds 
# dag flag is used to find everything below (like shapes) 
# so we specify we want only tansform nodes 
sel = cmds.ls(sl = True, dag=True, type='transform') 
for i in sel: 
    # we create a string attr for the current object 'i' because we will 
    # use it multiple times 
    attr = '{0}.backfaceCulling'.format(i) 
    # We check if this transform node has an attribute backfaceCulling 
    if cmds.ls(attr): 
     # if yes, let's set the Attr 
     cmds.setAttr(attr, 0) 

EDIT ---複数の属性の一つの例: `採用

import maya.cmds as cmds 
# dag flag is used to find everything below (like shapes) 
# so we specify we want only tansform nodes 
sel = cmds.ls(sl = True, dag=True, type='transform') 
# Multiple choices for multiple attr to change : 
# attrs = ['backfaceCulling', 'visibility'] 
# OR if you have different values 
# attrs = [['backfaceCulling', 0], 
#   ['visibility', 1]] 
# OR better go with dictionnaries if you have multiple values : 
attr_dic = {'backfaceCulling' : 0, 
      'visibility': 1} 
# you may add type if you need to 
# attr_dic = {'translate' : (0,1,3), 
#    'visibility': 1, 
#    'type_translate' : 'double3', 
#    'backfaceCulling':1} 


for a in attr_dic.keys(): 
    # keys return : backfaceCulling and visibility 
    fullAttr = ['{0}.{1}'.format(i, a) for i in sel if cmds.ls('{0}.{1}'.format(i, a)) if cmds.ls('{0}.{1}'.format(i, a))] 
    # This list comprehension return :['pSphere1.visibility','pSphere2.visibility','pSphere3.visibility','pSphere4.visibility','pSphere5.visibility'] 
    # syntax of list comprehension : [i for i in list if condition], it is used instead of normal for loop because it is really fast 
    for attr in fullAttr: 
     #for each obj, set value 
     cmds.setAttr(attr, attr_dic[a]) 
     # if you are using type : 
     # if not attr_dic.get('type_{0}'.format(a)): 
     #  cmds.setAttr(attr, attr_dic[a]) 
     # else: 
     #  t = attr_dic['type_{0}'.format(a)] 
     #  cmds.setAttr(attr, *attr_dic[a], type=t) 
+0

最終的にいくつかのMayaの開発者 "" .format()':D – user1767754

+0

はAHAH、私はこれを撮影しましたちょうどマヤの場合のhabbitsはpython 3.0に切り替える予定ですが、今はもっと読みやすくなっています。 – DrWeeny

+0

ありがとうございました。 それに加えて他の属性を追加設定したいのですが? forループで複数のattrsを作成するにはどうすればよいですか? – user8972552

関連する問題