0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

componentTagをメッシュにセットする

Posted at

以前にデフォーマーに対して使用するcomponentTagをセットする方法はわかったんですが

んじゃぁ、メッシュに対してcomponentTagをセットする方法はあるのだろうかと

再観察

image.png

上が名前で、下がコンポーネントなんでしょうね

とりあえずアトリビュートエディタから作ってみます
image.png

とりあえずgetAttr

cmds.getAttr("pSphereShape1.componentTags[0].componentTagName")
# Result: moge # 

こっちはstringっぽいですね

cmds.getAttr("pSphereShape1.componentTags[0].componentTagContents")
# Result: ['vtx[240:259]'] # 

こっちは・・・list?

cmds.getAttr("pSphereShape1.componentTags[0].componentTagName",type = True)
# Result: string # 
cmds.getAttr("pSphereShape1.componentTags[0].componentTagContents",type = True)
# Result: componentList # 

componentList だそうです。

componentList

ちょっと初めての相手ですが、setAttrできるかしら?

まずは素直にsetAttrしてみる

cmds.setAttr("pSphereShape1.componentTags[0].componentTagContents",['vtx[240:259]'])
# Error: RuntimeError: file <maya console> line 1: setAttr: 'pSphereShape1.componentTags[0].componentTagContents' is not a simple numeric attribute.  Its values must be set with a -type flag. # 

え?じゃぁこう?

cmds.setAttr("pSphereShape1.componentTags[0].componentTagContents",['vtx[240:259]'],type = "componentList")
# Error: RuntimeError: file <maya console> line 1: setAttr: Error reading data element number 1: ['vtx[240:259]'] # 

チャンと確認する

コンポーネントの可変長配列
値の構文 int {string}
値の意味 numberOfComponents {componentName}
MEL の例 setAttr node.componentListAttr -type componentList 3 cv[1] cv[12] cv[3];
Python の例 cmds.setAttr('node.componentListAttr',3,'cv[1]','cv[12]','cv[3]',type='componentList')

('A)なるほどなー (わかりづれぇ・・・・)

3 っつーのが多分後に続く引数の数 かな?
そう仮定するとこんな感じか

cmds.setAttr("pSphereShape1.componentTags[0].componentTagContents",1,'vtx[240:259]',type = "componentList")

お 通った。

とーなるとーこんな感じで選択した物を登録でどうだろかー

selects = cmds.ls(sl =True)
target = "pSphere"

components = []
for select in selects:
    components.append(select.split(".")[-1])

cmds.setAttr(target+".componentTags[0].componentTagContents",len(components),*components,type = "componentList")
# Error: RuntimeError: file <maya console> line 8: setAttr: No object matches name: pSphere.componentTags[0].componentTagContents # 

ん?なるほどではこうか

selects = cmds.ls(sl =True)
target = "pSphereShape1"

components = []
for select in selects:
    components.append(select.split(".")[-1])

cmds.setAttr(target+".componentTags[0].componentTagContents",len(components),*components,type = "componentList")

ok 登録できた。

image.png

もうすこしましにしたい
選択した物を追加できるように
tagNameはstringで指定できるように
tagNameが無い場合は新規追加できるように

def setComponentTag(target,tagName,components):
    
    ##target がshapeかどうかチェック
    if "shape" not in cmds.nodeType(target,inherited=True):
        shapes = cmds.listRelatives(target,ad =False,type = "shape")
        
        if shapes == None:
            return
            
        target = shapes[0]

    ##現在のtagNameとindexを取得
    indexes = cmds.getAttr(target +".componentTags", mi = True)    
    tagNames = []
    for i in indexes:
        tagNames.append(cmds.getAttr(target+".componentTags[0].componentTagName"))
    
    ##目的のtagNameが存在しない場合は追加
    if tagName not in tagNames:
        cmds.setAttr(target+".componentTags["+str(indexes[-1]+1)+"].componentTagName",tagName,type = "string")
        tagNames.append(tagName)
        indexes.append(indexes[-1]+1)
    
    ##ほんでsetAttr
    componentsOpt = []
    for component in components:
        componentsOpt.append(component.split(".")[-1])

    index = indexes[tagNames.index(tagName)]
    cmds.setAttr(target+".componentTags["+str(index)+"].componentTagContents",len(components),*componentsOpt,type = "componentList")

tareget = "pSphere1"
tagName = "hoge"
components = cmds.ls(sl =True)
setComponentTag(target,tagName,components)

まぁこんなとこでしょうかね。
あとは

  • componentTagContentsの内容を replace or update とか?

まぁアトリビュートエディタのUI使えばいいんですがね。
一括で色々やるんであればスクリプトで処理したいなぁと

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?