Page 1 of 2

Camera focus script in maya

Posted: Sat Sep 08, 2012 7:12 pm
by RickToxik
Hi! Here is a simple script I've made to control the camera focus in maya with a locator. Works with some other renderers too.

Select your camera and run the script!

Re: Camera focus script in maya

Posted: Sat Sep 08, 2012 7:42 pm
by Slimshader
Nice!
Thanks! :)

Re: Camera focus script in maya

Posted: Sat Sep 08, 2012 8:03 pm
by p3taoctane
very cool
Thanks

Re: Camera focus script in maya

Posted: Sat Sep 08, 2012 8:21 pm
by tchoa
Hi RickToxik

Good start/job for a 'first script ever' ;) (your code is clear and well commented)
Now you should use variables to make it versatile, and avoid renaming nodes, ...

Quick hints (as I'm asked to come for diner quickly):

Code: Select all

string $sel[] = `ls -sl`; // this is one of the most used lines when mel scripting
string $node = `createNode distanceBetween`; // basic node creator, result as node's name stored in $node variable
$sel = `listRelavtives -c $sel[0]`; // if your selection was a camera's transform this line will get its childs so you can get its camera node
connectAttr -f ($node + ".distance") ($sel[0] + ".octFocalDepth"); // connection...
To get it more handy create a global proc that has the same name as the mel file so you just have to call myScriptName() to use it

Code: Select all

global proc myScriptName()
{
    //...code...
}
Hope it will help you to improve your tool
++
F

Re: Camera focus script in maya

Posted: Sat Sep 08, 2012 8:26 pm
by RickToxik
Ahhhhhhhhhhh!!!!!!........... Allright then! I'll add this to ze code : )

Thanks mate!

Re: Camera focus script in maya

Posted: Mon Sep 10, 2012 3:44 am
by RickToxik
tchoa,

you gave me a very hard weekend lol

Here is what the script looks like now:

Code: Select all


/*global proc CameraFocus()
{
*/

//camera selected
string $Camera[] = `ls -sl`;

//UI for name selection
string $namequery = `promptDialog  -title "Camera Focus"
                              -message "Please enter a base name for the Camera Focus rig:"
                              -text "CameraFocus"
                              -button "OK"
                              -button "Cancel"
                              -defaultButton "OK"`;

string $name = `promptDialog -q`;


// the distanceBetween Node 
string $Distance = `createNode distanceBetween -n ($name + "_distanceShape#")`;

// the camera locator
createNode locator -n ($name + "_EyeShape#");
string $Campoint[] = `ls -sl`;

// the focus locator in yellow
createNode locator -n ($name + "_FocusShape#");
scale -yz 1.5 1;
setAttr ".overrideEnabled" 1;
setAttr ".overrideColor" 6;
colorIndex 6 1 1 0;
color -ud 6;
string $FocPoint[] = `ls -sl`;
move -rpr 0 0 -2 ;

// distance connections
connectAttr -f ($Campoint[0] + ".worldPosition") ($Distance + ".point1");
connectAttr -f ($FocPoint[0] + ".worldPosition") ($Distance + ".point2");

// bug fixed to prepare for the xform
select -r $Camera;
rename $name;
string $newCamera[] = `ls -sl`;

// moves the camera locator at the camera position
float $position[3] = `xform -worldSpace -query -translation $newCamera`;
select -r $Campoint;
move -p -ws $position[0] $position[1] $position[2];

// freezes, parents and hides the camera locator
select $Campoint;
select -add $newCamera;
parent;
setAttr -lock true ($Campoint[1] + ".tx");
setAttr -lock true ($Campoint[1] + ".ty");
setAttr -lock true ($Campoint[1] + ".tz");
setAttr -lock true ($Campoint[1] + ".rx");
setAttr -lock true ($Campoint[1] + ".ry");
setAttr -lock true ($Campoint[1] + ".rz");
setAttr -lock true ($Campoint[1] + ".sx");
setAttr -lock true ($Campoint[1] + ".sy");
setAttr -lock true ($Campoint[1] + ".sz");
HideSelectedObjects;

// connect to your favorite renderer
connectAttr -f ($Distance + ".distance") ($newCamera[0] + ".focusDistance");
connectAttr -f ($Distance + ".distance") ($newCamera[0] + ".octFocalDepth");

/*
}
However, I have no way to output the distanceBetween node infos in the viewport. I think it is a problem that there is no line between the focus and the camera: it can be hard to find the focus point quickly in (shaded) scenes. I also thought that the numbers flying out of the distance tool were really cool.

I was not able to figure out a way to rename all the components of the distance tool accordingly to the name chosen at the beginning of the script (given the possibility that the locator1 and locator2 might be already used in the scene and that a mel creation of the distance tool leaves only one locator selected). Anyway, I thought I would die learning these commands... I don't fully understand my script, or all the commands, but it works!! haha thanks to you I found the courage to build my first UI. héhéhé

I'll unleash the global proc when the script is 1.0 : )

Re: Camera focus script in maya

Posted: Mon Sep 10, 2012 4:32 pm
by RickToxik
Allright! All problems solved! You can download the script and use it if you want.

I have replaced the distanceBetween with a distanceDimShape, which the distance Tool is based on.

Re: Camera focus script in maya

Posted: Mon Sep 10, 2012 5:19 pm
by tchoa
:)
Glad you solved your problems... and had a hard week-end! ;)
Maybe you can test if attribute exists:

Code: Select all

if (`attributeExists ".octFocalDepth" $newCamera[0]`)
One other thing, try to do changes without doing selections.
Pass the node names to the commands.

Hard way for learning is often (always?) the best way.
Good job!

F

Re: Camera focus script in maya

Posted: Mon Sep 10, 2012 5:42 pm
by RickToxik
tchoa wrote: Pass the node names to the commands.
I don't know, I had so many problems with invalid strings when connecting the attributes. It seems the distanceDimension, the cameras and the locators don't really return the same number of array values. I am not sure I use the right terms to describe the problem, but... Anyway, I always had to try more or less randomly variable recalls with a [], [0] or [somethingelse] for my lines to work. So what I was doing was just to recreate a new selection or string that outputs what I needed. I'll sort this out one day or another.

Thanks for your help boss

Re: Camera focus script in maya

Posted: Wed Apr 08, 2015 4:03 pm
by abeoctane
Hi, I been using this script to get focus of the objects in maya, but it seems It doesn't work anymore with the latest octane 2.22.2 - 7.0 Win, do you know how can I fix it?, thank you.