Don't like ads? PRO users don't see any ads ;-)
Guest

CreateCameraFromView.py

By: a guest on May 29th, 2012  |  syntax: Python  |  size: 2.65 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. '''
  2. CreateCameraFromView.py
  3. Alex Forsythe, 5/29/2012
  4.  
  5. A simple Maya script for creating cameras from existing views.
  6. An example for the tech-artists.org forum. Based on the work on Shawn Freuh.
  7. http://tech-artists.org/forum/showthread.php?2712-Create-camera-from-view
  8. '''
  9.  
  10. import random
  11.  
  12. import maya.cmds as cmds
  13.  
  14. # If False, uses whatever view has focus, including the script editor!
  15. PERSP_VIEW_ONLY = True
  16.  
  17. def CreateCameraFromView(interactive = True, newCameraName = ''):
  18.     '''
  19.    Duplicates the active camera to create a new camera with the specified
  20.    name. If called with interactive enabled, newCameraName will be ignored
  21.    and the user will be asked to supply a new name. If no name is given,
  22.    one will be generated automatically. The new camera will be selected after
  23.    it is created, replacing the previous selection.
  24.    '''
  25.     if interactive:
  26.         result = cmds.promptDialog(title = 'Create Camera From View',
  27.             message = 'New camera name:', button = ['OK', 'Cancel'],
  28.             defaultButton = 'OK', cancelButton = 'Cancel',
  29.             dismissString = 'Cancel', text = newCameraName)
  30.        
  31.         if result == 'OK':
  32.             text = cmds.promptDialog(query = True, text = True)
  33.             if text:
  34.                 newCameraName = text
  35.         else:
  36.             return
  37.  
  38.     newCamera = DuplicateCurrentCamera(newCameraName)
  39.     cmds.select(newCamera, replace = True)
  40.     cmds.showHidden(newCamera)
  41.  
  42. def DuplicateCurrentCamera(newCameraName = ''):
  43.     '''
  44.    Finds the active camera and creates a copy of it with the specified name.
  45.    If no name is given, one will be randomly generated based on the name of
  46.    the original camera.
  47.    '''
  48.     cameraTransform, cameraShape = GetCurrentCamera()
  49.  
  50.     if not newCameraName:
  51.         newCameraName = GeneratePlaceholderName(cameraTransform)
  52.  
  53.     cmds.duplicate(cameraShape, name = newCameraName)
  54.     return newCameraName
  55.  
  56. def GetCurrentCamera():
  57.     '''
  58.    Returns a 2-tuple containing the currently active camera's transform node
  59.    and shape node.
  60.    '''
  61.     if PERSP_VIEW_ONLY:
  62.         panel = cmds.getPanel(withLabel = 'Persp View')
  63.     else:
  64.         panel = cmds.getPanel(withFocus = True)
  65.  
  66.     camera = cmds.modelEditor(panel, query = True, camera = True)
  67.     return camera, cmds.listRelatives(camera, type = 'camera')[0]
  68.  
  69. def GeneratePlaceholderName(baseNodeName):
  70.     '''
  71.    Given an existing node name as a basis, appends a randomly generated hex
  72.    string to create a placeholder name.
  73.    '''
  74.     return '_'.join([baseNodeName, hex(random.randint(0, 16777215))[2:]])
  75.  
  76.  
  77. if __name__ == '__main__':
  78.     CreateCameraFromView()