scanDagCmd

scanDag命令以depth first(深度优先)或breadth first(广度优先)来迭代整个DAG,输出每个节点的名称和深度等信息,这个命令插件例子中只对相机、灯光、NURBS surfaces进行支持。
在maya的Plug-in Manager中加载scanDagCmd.py,然后你可以随便创建一些相机、灯光、NURBS surfaces,并对它们进行位移等属性调整,然后分别执行

// -d 为深度优先,-b 为广度优先
// 相机
scanDag -d 1 -c 1;
// 灯光
scanDag -d 1 -l 1;
// NURBS surfaces
scanDag -d 1 -l 1;
// 或全部一起
scanDag -d 1 -c 1 -l 1 -n 1;

 或

import maya.cmds as cmds
# 相机
cmds.scanDag(d=1, c=1)
# 灯光
cmds.scanDag(d=1, l=1)

 你会得到类似的输出结果

Output
Starting Depth First scan of the Dag: Filtering for Cameras
perspShape: camera
dagPath: |persp|perspShape
translation: 28.000000 21.000000 28.000000
rotation: [-0.487616, 0.785398, 0.000000]
scale: [1.000000, 1.000000, 1.000000]
eyePoint: 28.000000 21.000000 28.000000
upDirection: -0.331295 0.883452 -0.331295
viewDirection: -0.624695 -0.468521 -0.624695
aspectRatio: 1.500000
horizontalFilmAperture: 1.417320
verticalFilmAperture: 0.944880
topShape: camera
dagPath: |top|topShape
translation: 0.000000 100.100000 0.000000
rotation: [-1.570796, 0.000000, 0.000000]
scale: [1.000000, 1.000000, 1.000000]
eyePoint: 0.000000 100.100000 0.000000
upDirection: 0.000000 0.000000 -1.000000
viewDirection: 0.000000 -1.000000 -0.000000
aspectRatio: 1.500000
horizontalFilmAperture: 1.417320
verticalFilmAperture: 0.944880
frontShape: camera
dagPath: |front|frontShape
translation: 0.000000 0.000000 100.100000
rotation: [0.000000, -0.000000, 0.000000]
scale: [1.000000, 1.000000, 1.000000]
eyePoint: 0.000000 0.000000 100.100000
upDirection: 0.000000 1.000000 0.000000
viewDirection: 0.000000 0.000000 -1.000000
aspectRatio: 1.500000
horizontalFilmAperture: 1.417320
verticalFilmAperture: 0.944880
sideShape: camera
dagPath: |side|sideShape
translation: 100.100000 0.000000 0.000000
rotation: [-0.000000, 1.570796, 0.000000]
scale: [1.000000, 1.000000, 1.000000]
eyePoint: 100.100000 0.000000 0.000000
upDirection: 0.000000 1.000000 0.000000
viewDirection: -1.000000 0.000000 -0.000000
aspectRatio: 1.500000
horizontalFilmAperture: 1.417320
verticalFilmAperture: 0.944880
// Result: 4 //

 scanDagCmd.py

# -*- coding: UTF-8 -*-

import sys
import maya.OpenMaya as om
import maya.OpenMayaMPx as ompx

# 命令名称
kCmdName = 'scanDag'

# 命令参数
kBreadthFlag = '-b'
kBreadthFlagLong = 'breadthFirst'
kDepthFlag = '-d'
kDepthFlagLong = '-depthFirst'
kCameraFlag = '-c'
kCameraFlagLong = '-cameras'
kLightFlag = '-l'
kLightFlagLong = '-lights'
kNurbsSurfaceFlag = '-n'
kNurbsSurfaceFlagLong = '-nurbsSurfaces'
kQuietFlag = '-q'
kQuietFlagLong = '-quiet'

# 定义命令
class ScanDag(ompx.MPxCommand):
    
    def __init__(self):
        super(ScanDag, self).__init__()
        
    def doIt(self, args):
        # 初始化默认变量,防止无参数命令出现
        self._traversalType = om.MItDag.kDepthFirst
        self._filter = om.MFn.kInvalid
        self._quiet = False
        
        self.__parseArgs(args)
        
        return self.__doScan()
    
    def __parseArgs(self, args):
        '''
        检查命令所给的参数,并设置相应的变量
        '''
        argData = om.MArgDatabase(self.syntax(), args)
        if argData.isFlagSet(kBreadthFlag) or argData.isFlagSet(kBreadthFlagLong):
            self._traversalType = om.MItDag.kBreadthFirst
        if argData.isFlagSet(kDepthFlag) or argData.isFlagSet(kDepthFlagLong):
            self._traversalType = om.MItDag.kDepthFirst
        if argData.isFlagSet(kCameraFlag) or argData.isFlagSet(kCameraFlagLong):
            self._filter = om.MFn.kCamera
        if argData.isFlagSet(kLightFlag) or argData.isFlagSet(kLightFlagLong):
            self._filter = om.MFn.kLight
        if argData.isFlagSet(kNurbsSurfaceFlag) or argData.isFlagSet(kNurbsSurfaceFlagLong):
            self._filter = om.MFn.kNurbsSurface
        if argData.isFlagSet(kQuietFlag) or argData.isFlagSet(kQuietFlagLong):
            self._quiet = True
        
        return True
    
    def __doScan(self):
        '''
        完成实际的工作
        '''
        # 创建dag迭代器
        dagIterator = om.MItDag(self._traversalType, self._filter)
        
        # Scan the entire DAG and output the name and depth of each node
        # 扫描整个DAG,输出每个节点的名称和深度
        if self._traversalType == om.MItDag.kBreadthFirst:
            if not self._quiet:
                sys.stdout.write('Starting Breadth First scan of the Dag')
        else:
            if not self._quiet:
                sys.stdout.write('Starting Depth First scan of the Dag')
                
        if self._filter == om.MFn.kCamera:
            if not self._quiet:
                sys.stdout.write(': Filtering for Cameras\n')
        elif self._filter == om.MFn.kLight:
            if not self._quiet:
                sys.stdout.write(': Filtering for Lights\n')
        elif self._filter == om.MFn.kNurbsSurface:
            if not self._quiet:
                sys.stdout.write(': Filtering for Nurbs Surfaces\n')
        else:
            sys.stdout.write('\n')
            
        # 对DAG里的每个节点进行迭代
        objectCount = 0
        while not dagIterator.isDone():
            dagPath = om.MDagPath()
            # 获取节点的DAG路径
            dagIterator.getPath(dagPath)
            # 使用方法集来操作节点
            dagNode = om.MFnDagNode(dagPath)
            
            # 输出节点的名称,类型,全路径
            if not self._quiet:
                sys.stdout.write('%s: %s\n' % (dagNode.name(), dagNode.typeName()))
                
            if not self._quiet:
                sys.stdout.write('  dagPath: %s\n' % dagPath.fullPathName())
                
            # 判别节点的类型,这里只对相机、灯光、NURBS surfaces进行操作
            # 实际上是可以对任意类型,进行相应的操作
            objectCount += 1
            if dagPath.hasFn(om.MFn.kCamera):
                camera = om.MFnCamera(dagPath)
                
                # Get the translation/rotation/scale data
                # 获取translation/rotation/scale等数据
                self.__printTransformData(dagPath)
                
                # Extract some interesting Camera data
                # 分离一些有趣的相机数据
                if not self._quiet:
                    eyePoint = camera.eyePoint(om.MSpace.kWorld)
                    upDirection = camera.upDirection(om.MSpace.kWorld)
                    viewDirection = camera.viewDirection(om.MSpace.kWorld)
                    aspectRatio = camera.aspectRatio()
                    hfa = camera.horizontalFilmAperture()
                    vfa = camera.verticalFilmAperture()
                    
                    sys.stdout.write("  eyePoint: %f %f %f\n" % (eyePoint.x, eyePoint.y, eyePoint.z))
                    sys.stdout.write("  upDirection: %f %f %f\n" % (upDirection.x, upDirection.y, upDirection.z))
                    sys.stdout.write("  viewDirection: %f %f %f\n" % (viewDirection.x, viewDirection.y, viewDirection.z))
                    sys.stdout.write("  aspectRatio: %f\n" % aspectRatio)
                    sys.stdout.write("  horizontalFilmAperture: %f\n" % hfa)
                    sys.stdout.write("  verticalFilmAperture: %f\n" % vfa)
            elif dagPath.hasFn(om.MFn.kLight):
                light = om.MFnLight(dagPath)
                
                # Get the translation/rotation/scale data
                # 获取translation/rotation/scale等数据
                self.__printTransformData(dagPath)
                
                # Extract some interesting Light data
                # 分离一些有趣的灯光数据
                color = om.MColor()
                
                color = light.color()
                if not self._quiet:
                    sys.stdout.write("  color: [%f, %f, %f]\n" % (color.r, color.g, color.b))
                    
                color = light.shadowColor()
                if not self._quiet:
                    sys.stdout.write("  shadowColor: [%f, %f, %f]\n" % (color.r, color.g, color.b))
                    sys.stdout.write("  intensity: %f\n" % light.intensity())
            elif dagPath.hasFn(om.MFn.kNurbsSurface):
                surface = om.MFnNurbsSurface(dagPath)
                
                # Get the translation/rotation/scale data
                # 获取translation/rotation/scale等数据
                self.__printTransformData(dagPath)
                
                # Extract some interesting Surface data
                # 分离一些有趣的NURBS surfaces数据
                if not self._quiet:
                    sys.stdout.write("  numCVs: %d * %d\n" % (surface.numCVsInU(), surface.numCVsInV()))
                    sys.stdout.write("  numKnots: %d * %d\n" % (surface.numKnotsInU(), surface.numKnotsInV()))
                    sys.stdout.write("  numSpans: %d * %d\n" % (surface.numSpansInU(), surface.numSpansInV()))
            else:
                # Get the translation/rotation/scale data
                # 获取translation/rotation/scale等数据
                self.__printTransformData(dagPath)
                
            dagIterator.next()
            
        # 返回命令结果,这个结果为所操作的节点的数量值
        self.setResult(objectCount)
        return om.MStatus.kSuccess
    
    def __printTransformData(self, dagPath):
        '''
        获取translation/rotation/scale等数据
        '''
        # 获取节点的Transform节点
        transformNode = dagPath.transform()
        transform = om.MFnDagNode(transformNode)
        # 获取Transform的Transformation matrix
        matrix = om.MTransformationMatrix(transform.transformationMatrix())
        
        if not self._quiet:
            # 获取节点的位移数值,并输出
            translation = matrix.translation(om.MSpace.kWorld)
            sys.stdout.write('  translation: %f %f %f\n' % (translation.x, translation.y, translation.z))
            
        #rOrder = om.MTransformationMatrix.kXYZ
        #matrix.getRotation(ptr, rOrder)
        
        # 获取节点的旋转数值,并输出
        rotateOrder = 0
        mquat = matrix.rotation()
        rot = mquat.asEulerRotation()
        rot.reorderIt(rotateOrder)
        
        if not self._quiet:
            sys.stdout.write("  rotation: [%f, %f, %f]\n" % (rot.x, rot.y, rot.z))
            
        # 获取节点的缩放数值,并输出
        # 这里需要使用MScriptUtil
        # 因为MTransformationMatrix.getScale()
        # 的第一个参数是C++里的指针
        util = om.MScriptUtil()
        util.createFromDouble(0.0, 0.0, 0.0)
        ptr = util.asDoublePtr()

        matrix.getScale(ptr, om.MSpace.kWorld)
        
        scaleX = util.getDoubleArrayItem(ptr, 0)
        scaleY = util.getDoubleArrayItem(ptr, 1)
        scaleZ = util.getDoubleArrayItem(ptr, 2)
        if not self._quiet:
            sys.stdout.write('  scale: [%f, %f, %f]\n' % (scaleX, scaleY, scaleZ))
            
    
# Creator
def cmdCreator():
    return ompx.asMPxPtr(ScanDag())

# Syntax creator
def syntaxCreator():
    syntax = om.MSyntax()
    syntax.addFlag(kBreadthFlag, kBreadthFlagLong, om.MSyntax.kBoolean)
    syntax.addFlag(kDepthFlag, kDepthFlagLong, om.MSyntax.kBoolean)
    syntax.addFlag(kCameraFlag, kCameraFlagLong, om.MSyntax.kBoolean)
    syntax.addFlag(kLightFlag, kLightFlagLong, om.MSyntax.kBoolean)
    syntax.addFlag(kNurbsSurfaceFlag, kNurbsSurfaceFlagLong, om.MSyntax.kBoolean)
    syntax.addFlag(kQuietFlag, kQuietFlagLong, om.MSyntax.kBoolean)
    return syntax

# Initialize the script plug-in
def initializePlugin(mobj):
    mplugin = ompx.MFnPlugin(mobj, 'Mack Stone', '3.0', 'Any')
    try:
        mplugin.registerCommand(kCmdName, cmdCreator, syntaxCreator)
    except:
        sys.stderr.write('Failed to register command: %s\n' % kCmdName)
        raise
    
# Uninitialize the script plug-in
def uninitializePlugin(mobj):
    mplugin = ompx.MFnPlugin(mobj)
    try:
        mplugin.deregisterCommand(kCmdName)
    except:
        sys.stderr.write('Failed to unregister command: %s\n' % kCmdName)
        raise

 你可以在devkit\plug-ins中找到scanDagCmd.cpp
在线版本

http://download.autodesk.com/us/maya/2010help/API/scan_dag_cmd_8cpp-example.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值