1. spike and Ball: 這個範例是示範連接二個物件的 attribute
from pymel.core import * # create cone "spike" and sphere "ball" # coneObj is actually a list of "transform" and "shape" coneObj = cone(name="spike") # same as sphereObj sphereObj = sphere(name="ball") # get the transform coneTrans = coneObj[0] sphereTrans = sphereObj[0] # move the ball to (0,0,5) general.move(sphereObj,0,0,5) # 連接二個物件的 attribute, PyMel 定義了這個好用的 macro sphereTrans.translateX >> coneTrans.translateY
若要 disconnect 的話,則使用 // 這個 macro 就可以了,最大的好處就是可以少打幾個字 (connectAttr,dis七onnectAttr)
sphereTrans.translateX // coneTrans.translateY
2, 第二個範例是建一個 emitter 並發射 directional 的粒子,噴射的方向會和 emitter 的位置連接在一起,執行 script 後,對 emitter 做個簡單的 animation ,還蠻好玩的
from pymel.core import *
# create an emitter, set the spread to 0.3 and set the emitter type to direction
emitterObj = emitter(name="myEmitter", spread=0.3, type="direction")
# create a particle
particleObj = particle(name="myParticle")
# connect particle and emitter
# effects.connectDynamic('myParticle', emitters='myEmitter')
effects.connectDynamic(particleObj, emitters=emitterObj)
# connect emitter's translation attribute to direction attribute
emitterObj.translateX >> emitterObj.directionX
emitterObj.translateY >> emitterObj.directionY
emitterObj.translateZ >> emitterObj.directionZ 3. 其實 Maya 一個很強大的功能就是可以讓使用者自訂 attribute 跟 Max 中的 extra data 一樣,給了使用者非常大的彈性,可以自行擴充,第三個範例就是加入自訂的 attribute 到物件上
from pymel.core import *
# create a locator and name it as "posController"
locatorObj = spaceLocator(name="posController")
# add 4 attributes
locatorObj.addAttr("posX", keyable=True, shortName="pX")
locatorObj.addAttr("posY", keyable=True, shortName="pY")
locatorObj.addAttr("posZ", keyable=True, shortName="pZ")
# you won't able to see "extra" attribute from chanel box because it's not keyable
locatorObj.addAttr("extra", keyable=False, shortName="ext")
# connect attributes
locatorObj.posX >> emitterObj.translateX
locatorObj.posY >> emitterObj.translateY
locatorObj.posZ >> emitterObj.translateZ
要設定物件的 attribute 可以使用 setAttr
# set "extra" attribute as keyable
loacatorObj.setAttr("extra", keyable=False)
在刪除物件 attribute 時,我不清楚為什麼 object.deletAttr("attrName")行不通,只能使用 general.deleteAttr ... 這不是肯德雞(物件導向)!!?
# delete "extra" attribute general.deleteAttr(loacatorObj.extra)
由這幾個例子可以看到由 Mel 轉到 Python (PyMel) 是一件多麼輕鬆寫意的工作,script 看起來也簡潔有力,Learning Maya MEL Fundamental 第二章就介紹了 attribute,可見 attribute 在 Maya 中是多麼的重要,而搭配 PyMel 後更是如虎添翼,對我們開發者而言真是太幸福了。
No comments:
Post a Comment