Create camera from view

So all day I have been setting up some cameras and was getting annoyed at starting from the center grid each time I created a new camera. What I usually do if I have to create only a few cameras is to use the camera bookmarks and then transfer them over to the new camera, that way if I accidentally move the new camera before locking it I can easily reset the position back to the bookmark. Then I thought it would be cool to write something that did all this for me automatically so I’m just here to share with you guys what I created. Let me know if you like!


//Create Camera From View

/*
Developed by: Shawn Frueh
Web: www.shawnfrueh.com
Email: sfrueh@shawnfrueh.com
Description:
This script will create a camera from your current view using camera bookmarks. 
The new camera will take the name of your current camera with the addition _NEW to the end. 
UPDATE: Script now works with bookmarks that have changed from their default names. 
        Now closes the bookmark window after creating a bookmark. 
*/

// Select the current viewports camera
string $camera;
string $curView = `getPanel -wf`;
$camera = `modelEditor -q -camera $curView`;
select ($camera+"Shape");
// Create a bookmark
cameraBookmarkEditor $camera;
cameraBookmarkNewCB cameraBookmarkWindow $camera;
// Close bookmark window
deleteUI cameraBookmarkWindow;
// Find all bookmarks on current camera
select -noExpand `listHistory ("|"+$camera+"|"+$camera+"Shape")`;
select -d ($camera+"Shape");
string $sel[] = `ls -sl`;
int $selCount = size($sel);
// Find unNamned Bookmarks
select "cameraView*";
string $sel2[] = `ls -sl`;
int $selCount2 = size($sel2);
//rename bookmark
rename ("cameraView"+$selCount2) "fromView";
// Create and name new camera
CreateCameraOnly;
rename ($camera+"_FromView");
// Add the new camera name as a string
string $cameraFV = ($camera+"_FromView");
//Connect the bookmark to the new camera
connectAttr fromView.message ($cameraFV+".bookmarks[0]");
// Apply the bookmark to the new camera 
cameraView -e -animate (`optionVar -q animateRollGotoBookmark`) -camera $cameraFV -setCamera fromView;
// Remove old connection
disconnectAttr fromView.message ("|"+$camera+"|"+$camera+"Shape.bookmarks["+($selCount-1)+"]");
// Finish by renaming new camera
rename ($camera+"_FromView") ($camera+"_NEW");
rename fromView "NewfromView";

Another way would be to duplicate the camera you’d like to imitate and name it accordingly.

As a quick riff on what you posted, here’s an alternate implementation (in Python) using the method that marcuso alluded to:

The link has a fully fleshed-out script, but here’s the meat and potatoes:


import maya.cmds as cmds

cameraTransform = cmds.modelEditor(cmds.getPanel(withLabel = 'Persp View'), query = True, camera = True)
cameraShape = cmds.listRelatives(cameraTransform, type = 'camera')[0]
newCameraName = '%s_NEW' % cameraTransform

cmds.duplicate(cameraShape, name = newCameraName)
cmds.showHidden(newCameraName)
cmds.select(newCameraName, replace = True)

And, just for fun, here’s the same thing in dusty old MEL:


string $panel = `getPanel -withLabel "Persp View"`;
string $cameraTransform = `modelEditor -q -camera $panel`;
string $cameraShapes[] = `listRelatives -type "camera" $cameraTransform`;
string $cameraShape = $cameraShapes[0];
string $newCameraName = $cameraTransform + "_NEW";

duplicate -name $newCameraName $cameraShape;
showHidden $newCameraName;
select -replace $newCameraName;

Nice and short script awforsythe, I like it! The main idea of what i wrote was to apply a bookmark to the new camera with the current view. Reason behind the bookmarks is for some shots I like to have 2 or 3 angles to choose from. and if I accidentally move the camera before Its animated it can be quickly moved back to the exact position.