// Copyright (C) 1997-2004 Alias Systems Corp.
// 
// The information in this file is provided for the exclusive use of the
// licensees of Alias.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias license agreement, without fee.
// 
// ALIAS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

//
//  Alias Script File
//
//  Creation Date:	June 30, 1997
//
//  Procedure Name:
//      cameraImagePlaneUpdate
//
//  Description:
//      Initialize an image plane for a camera.
//
//  Input Value:
//      camera - name of the camera to attach image plane to
//      imagePlane - name of the image plane
//
//  Output Value:
//      None
//

//
//  Procedure Name:
//      updatePort
//

proc updatePort( string $cameraShape )
{
    string $panels[] = `getPanel -type "modelPanel"`;

    for ($modelPanel in $panels) {
        string $modelCamera = `modelPanel -q -camera $modelPanel`;

        if ($modelCamera != "") {
            string $shapes[] = `listRelatives -shapes $modelCamera`;

            for ($shape in $shapes) {
                if ($shape == $cameraShape) {
                    string $editor = `modelPanel -q -modelEditor $modelPanel`;
                    modelEditor -e -updateColorMode $editor;
                    break;
                }
            }
        }
    }
}

global proc cameraImagePlaneUpdate( string $camera, string $imagePlane )
{
	connectAttr -nextAvailable
        ($imagePlane+".message") ($camera+".imagePlane");

	setAttr
        ($imagePlane+".lockedToCamera") (!`getAttr ($camera+".orthographic")`);

	setAttr
        ($imagePlane+".sizeX") `getAttr ($camera+".horizontalFilmAperture")`;
	setAttr
        ($imagePlane+".sizeY") `getAttr ($camera+".verticalFilmAperture")`;

	setAttr
        ($imagePlane+".width") `getAttr ($camera+".orthographicWidth")`;
	setAttr
        ($imagePlane+".height") `getAttr ($camera+".orthographicWidth")`;

	float $coi[] = `camera -q -worldCenterOfInterest $camera`;
    setAttr ($imagePlane+".center") -type "double3" $coi[0] $coi[1] $coi[2];

    // Ensure the image plane outline is drawn and selectable.
    string $parents[] = `listRelatives -parent $camera`;
    for ($item in $parents) showHidden -below $item;

    updatePort( $camera );
}
