作業一:
import maya.OpenMaya as OpenMaya
import pymel.core as pm
###########################################
########## 作業一 ###################
###########################################
iterator = OpenMaya.MItDag()
iterator.reset(iterator.root(), OpenMaya.MItDag.kBreadthFirst, OpenMaya.MFn.kMesh ) # 指定一個起始點,返回指定起始點的所有層級 ,指定迭代的類型kMesh
jointSelectionList = OpenMaya.MSelectionList()
while not iterator.isDone():
print(iterator.partialPathName())
jointSelectionList.add(iterator.partialPathName())
iterator.next()
OpenMaya.MGlobal.setActiveSelectionList(jointSelectionList)
作業二:
###########################################
########## 作業二 ###################
###########################################
import maya.OpenMaya as OpenMaya
import pymel.core as pm
obj1 = 'pSphere1'
obj2 = 'pSphere2'
def distance(x1, y1, z1, x2, y2, z2):
dis = (x1-x2)**2 + (y1-y2)**2 +(z1-z2)**2
return dis
def walk_point(obj):
a = []
iterator = OpenMaya.MItMeshVertex(pm.PyNode(obj).__apimobject__())
MSelectionList = OpenMaya.MSelectionList()
while not iterator.isDone():
point = iterator.position()
print(point.x, point.y, point.z)
dis = distance(point.x, point.y, point.z,ball_1_center.x,ball_1_center.y,ball_1_center.z)
if dis < 1:
MSelection.add(iterator.currentItem()) #將距離小於半徑的平方的點放入MSelection中,
#然後使用OpenMaya.MGlobal.setActiveSelectionList(jointSelectionList)選中
iterator.next()
OpenMaya.MGlobal.setActiveSelectionList(jointSelectionList)
ball_pml_node = pm.PyNode(obj1)
ball_1_center = ball_pml_node.translate.get()
ball_pml_node = pm.PyNode(obj2)
ball_2_center = ball_pml_node.translate.get()
walk_point(obj2)
作業三:
###########################################
########## 作業三 ###################
###########################################
import pymel.core as pm
import maya.OpenMaya as OpenMaya
#導入pymel、OpenMaya
obj = 'pSphereShape1'
s_util = OpenMaya.MScriptUtil() # 定義一個MScriptUtil實例,每個腳本中定義一個即可
uv_ptr = s_util.asFloat2Ptr() # 定義一個UV的float2的指針,回一次往這個指針裏面寫入數據,改變它的值,我們在最後訪問時,只能訪問到它最後一個值,所以寫入一次就要訪問一次
mfnMesh = OpenMaya.MFnMesh(pm.PyNode(obj).__apimobject__())
point = OpenMaya.MPoint()
mfnMesh.getPoint(1, point)
mfnMesh.getUVAtpoint(point, uv_ptr)
u_value = s_util.getFloat2ArrayItem(uv_ptr, 0, 0)
v_value = s_util.getFloat2ArrayItem(uv_ptr, 0, 1)
作業四:
###########################################
########## 作業四 ###################
###########################################
import maya.OpenMaya as OpenMaya
import pymel.core as pm
def func(*args):
global newnamespace,ns
ns_new = maya.cmds.namespaceInfo(listOnlyNamespaces=True,recurse=True)
for i in ns_new:
if i not in ns:
pm.namespace(rename=[i,'test'])
print 1,ns
newnamespace = 'test'
ns = maya.cmds.namespaceInfo(listOnlyNamespaces=True,recurse=True)
print(ns)
callback_id = OpenMaya.MSceneMessage.addCallback(OpenMaya.MSceneMessage.kAfterReference, func())
OpenMaya.MSceneMessage.removeCallback(callback_id) #移除當前事件