第九周 -- 作业

作业一:

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) #移除当前事件

猜你喜欢

转载自blog.csdn.net/weixin_41363156/article/details/104597573