[英]How to collect all cameras in Maya using Python?
How can I collect all the cameras in a Maya scene excluding the default 'perspective, top, front, side' cameras? 如何收集Maya场景中的所有摄像机,但不包括默认的“透视,顶部,正面,侧面”摄像机?
I only want to collect the user created cameras. 我只想收集用户创建的相机。
cameras = cmds.ls(type=('camera'), l=True) or []
print cameras
Do I need to do listRelatives
for any reason? 我出于某种原因需要做
listRelatives
吗? I eventually just want to print the world Matrix of each camera. 我最终只想打印每个摄像机的世界矩阵。
To weed out default cameras, you could query for startupCamera
using the cmds.camera
. 剔除掉默认的摄像头,可以查询
startupCamera
使用cmds.camera
。 Here is some code (with comments) to explain. 这是一些代码(带有注释)来解释。
# Let's use Pymel for fun
import pymel.core as pm
# Get all cameras first
cameras = pm.ls(type=('camera'), l=True)
# Let's filter all startup / default cameras
startup_cameras = [camera for camera in cameras if pm.camera(camera.parent(0), startupCamera=True, q=True)]
# non-default cameras are easy to find now. Please note that these are all PyNodes
non_startup_cameras_pynodes = list(set(cameras) - set(startup_cameras))
# Let's get their respective transform names, just in-case
non_startup_cameras_transform_pynodes = map(lambda x: x.parent(0), non_startup_cameras_pynodes)
# Now we can have a non-PyNode, regular string names list of them
non_startup_cameras = map(str, non_startup_cameras_pynodes)
non_startup_cameras_transforms = map(str, non_startup_cameras_transform_pynodes)
import maya.cmds as cmds
# Get all cameras first
cameras = cmds.ls(type=('camera'), l=True)
# Let's filter all startup / default cameras
startup_cameras = [camera for camera in cameras if cmds.camera(cmds.listRelatives(camera, parent=True)[0], startupCamera=True, q=True)]
# non-default cameras are easy to find now.
non_startup_cameras = list(set(cameras) - set(startup_cameras))
# Let's get their respective transform names, just in-case
non_startup_cameras_transforms = map(lambda x: cmds.listRelatives(x, parent=True)[0], non_startup_cameras)
Some further reading: http://ewertb.soundlinker.com/mel/mel.082.php 进一步阅读: http : //ewertb.soundlinker.com/mel/mel.082.php
You probably need to depend on listCamera command which you can list only persp camera or other. 您可能需要依赖listCamera命令,该命令只能列出persp摄像机或其他摄像机。 eg :
例如:
perspCameras = cmds.listCameras( p=True ) perspCameras = cmds.listCameras(p = True)
cameras = cmds.ls(type='camera') or []
exclude = ['persp', 'top', 'front', 'side']
cameras = list(set(cameras)-set(exclude))
go through all cameras in scene and print there transform ws matrix: 遍历场景中的所有摄像机并打印在那里变换ws矩阵:
for cam in cmds.listRelatives(cmds.ls(type="camera"), p=True):
if cmds.objectType(cam, isType='transform'): # True
print cam, cmds.xform(cam, q=True, ws=True, matrix=True)
front [1.0, 0.0, 0.0, 0.0....
persp [0.707106781186547....
shot_cam [0.9999999999999997, 0.0, .....
side [2.220446049250313e-16, 0.0 .....
top [1.0, 0.0, 0.0, 0.0, 0.0......
lets try it without relative connection, 让我们在没有相对联系的情况下尝试一下
for cam in cmds.ls(type="camera"):
print cmds.objectType(cam, isAType='transform') # False
print cam, cmds.xform(cam, q=True, ws=True, matrix=True)
RuntimeError : No valid objects supplied to 'xform' command RuntimeError :没有有效的对象提供给'xform'命令
In case anyone stumbles upon this, here are some easy methods that cover a few extra use cases: 万一有人偶然发现,这里有一些简单的方法可以涵盖一些额外的用例:
import maya.cmds as cmds
def all_user_cameras(return_transforms=False):
# Get all user created / non-startup cameras in the scene
all_user_cameras = [c for c in cmds.ls(type='camera') if not cmds.camera(c, q=True, sc=True)]
if return_transforms:
return [cmds.listRelatives(c, p=True)[0] for c in all_user_cameras]
return all_user_cameras
def all_selected_user_cameras(check_nested=True, return_transforms=False):
# Get the user created / non-startup cameras from the selection, check_nested looks inside groups when set to True
selected_cameras = cmds.ls(sl=True, type='camera')
selected_camera_from_shapes = cmds.listRelatives(cmds.ls(sl=True), ad=check_nested, c=True, type='camera')
if selected_camera_from_shapes:
selected_cameras.extend(selected_camera_from_shapes)
if return_transforms:
return [cmds.listRelatives(c, p=True)[0] for c in selected_cameras]
return selected_cameras
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.