简介:
示例:
首先导入头文件
import maya.cmds as cmds
- 列出所有对象
# list all objects
cmds.ls()
- 列出所有选择的对象
# List all selected objects
cmds.ls( selection=True )
- 列表中所有隐藏对象
# List all hilited objects
cmds.ls( hilite=True )
- 列出最后选择的对象
# List last selected object
cmds.ls( selection=True, tail=1 )
- 列出所有名为“sphere1”的对象。注意,由于sphere1是实例化的,下面的命令只列出第一个实例。
# List all objects named "sphere1". Note that since sphere1 is
# instanced, the command below lists only the first instance.
cmds.ls( 'sphere1' )
- 列出所有Sphere1的实例,使用全路径标签
# To list all instances of sphere1, use the -ap/allPaths flag.
cmds.ls( 'sphere1', ap=True )
- 列出所有选中的对象所命名的“group*”
# List all selected objects named "group*"
cmds.ls( 'group*', sl=True )
- 列出所有的几何体和灯光和摄像机
# List all geometry, lights and cameras in the DAG.
cmds.ls( geometry=True, lights=True, cameras=True )
- 列出所有的形状
# List all shapes in the dag.
cmds.ls( shapes=True )
- 需要注意的一点是,在列出没有任何过滤器的节点时,最好总是使用-L/Long flgg。这是因为可能有两个具有相同名称的节点(在本例中为circlel)。Ls将列出场景中所有对象的名称,具有相同名称的对象需要一个限定路径名来唯一标识对象。一个命令选择所有对象,例如“select is”将失败,因为对象查找无法解析要查找的是哪个“circler”对象。要选择所有对象,您需要以下步骤:
cmds.select(cmds.ls(sl=True))
- 当我们尝试在一个列表中查找一种特殊的对象类型时,一种方法是获取所有的对象然后再使用nodetype命令去筛选列表。示例:
# When trying to find a list of all objects of a specific
# type, one approach might be to list all objects and then
# use the nodeType command to then filter the list. As in:
allObjects = cmds.ls(l=True)
for obj in allObjects:
if cmds.nodeType(obj) == 'surfaceShape':
print obj
- 这里的问题是“nodetype”返回大多数的类型,在这个案例中“surfaceShape”是nurbsSurface的基本类型,因此不会打印任何内容。针对这个问题,这里的 -typ/type标签应该用于列出特定类型的对象,如:``
# The problem with this is that 'nodeType' returns the
# most derived type of the node. In this example, "surfaceShape"
# is a base type for nurbsSurface so nothing will be printed.
# To do this properly, the -typ/type flag should be used
# to list objects of a specific type as in:
allObjects = cmds.ls(type='surfaceShape')
for obj in allObjects:
print obj
*列出所有几何体形状和它们的类型
# List all geometry shapes and their types
cmds.ls( type='geometryShape', showType=True )
- 列出DAG中所有叶节点的所有路径
# List all paths to all leaf nodes in the DAG
cmds.ls( dag=True, lf=True, ap=True )
- 列出所选节点下面的所有节点
# List all nodes below the selected node
cmds.ls( dag=True, ap=True, sl=True )
- 列出所有ghosting对象
# List all ghosting objects
cmds.ls( ghost=True )
有空继续
# List all dag nodes that are read-only (i.e. referenced nodes)
cmds.ls( dag=True, ro=True )
# List reference nodes associated with specific files
cmds.ls( references=True )
# List all reference nodes, including unknown and shared reference nodes
cmds.ls( type='reference' )
# Select some components and then get the list in both selected and numeric order
obj1 = cmds.polySphere( sx=20, sy=20 )
cmds.select( clear=True )
cmds.selectPref( trackSelectionOrder=1 )
cmds.select( obj1[0]+".f[100]" )
cmds.select( (obj1[0]+".f[50:55]"), add=True )
cmds.select( (obj1[0]+".f[0]"), add=True )
cmds.select( (obj1[0]+".f[56:60]"), add=True )
# regular -selection flag returns the components in compacted numeric order.
cmds.ls( selection=True )
# Result:_ [u'pSphere1.f[0]', u'pSphere1.f[50:60]', u'pSphere1.f[100]'] #
# -orderedSelection flag returns the components in the order that we selected them.
cmds.ls( orderedSelection=True )
# Result:_ [u'pSphere1.f[100]', u'pSphere1.f[50:55]', u'pSphere1.f[0]', u'pSphere1.f[56:60]'] #
# turn off tracking when we are done
cmds.selectPref( trackSelectionOrder=0 )
# init some namespace
cmds.namespace( add="A:B:C" )
# add object into namespace
cmds.namespace( set=":A:B" )
cmds.polySphere( name="obj1" )
cmds.namespace( set=":A:B:C" )
cmds.polySphere( name="obj1" )
cmds.polySphere( name="obj2" )
# The current Namespace is ":A:B:C" and relative mode is off
# List all objects and their namespace in the scene
# If the object is in the root namespace, then return root ":"
# Note that the results shown below have been elided (...) for documentation purposes.
cmds.ls( showNamespace=True )
# Result: [u'time1', u':', u'sequenceManager1', u':', u'renderPartition', u':', (...), u'A:B:obj1', u'A:B', u'A:B:C:obj1', u'A:B:C', u'A:B:C:obj2', u'A:B:C'] #
cmds.select( ":A:B:obj1", r=True )
cmds.select( ":A:B:C:obj2", add=True)
# List namespace of all objects named "obj1"
cmds.ls( "obj1", showNamespace=True, recursive=True )
# Result: [u'A:B:obj1', u'A:B', u'A:B:C:obj1', u'A:B:C'] #
# List both name and namespace of each selected object
cmds.ls( showNamespace=True, selection=True )
# Result: [u'A:B:obj1', u'A:B', u'A:B:C:obj2', u'A:B:C'] #
# set current Namespace
cmds.namespace( set=":A:B" )
# enable relative mode
cmds.namespae( relativeNames=True )
# Now the current Namespace is ":A:B" and relative mode is on
# Note that the name of the current namespace is "" in relative mode
# List both name and namespace of each selected objects
cmds.ls( showNamespace=True, selection=True )
# Result: [u'obj1', u'', u'C:obj2', u'C'] #
#make a new scene modify the transform of the camera perspective, play with the timeline and modified the camera's shape
cmds.file(force=True, new=True)
cmds.setAttr('persp.translateX', 10)
cmds.currentTime(8)
cmds.setAttr('perspShape.horizontalFilmAperture', 16)
#list all modified objects of type camera and type time
allObjects=cmds.ls(type=['camera','time'], modified=True)
print allObjects
# Result: [u'perspShape', u'time1']
cmds.ls(modified=True)
# Result: [u'persp', u'perspShape', u'time1']
cmds.ls(modified=True, excludeType='camera')
# Result: [u'persp', u'time1']