Camera focus script in maya

Autodesk Maya (Plugin developed by JimStar)

Moderator: JimStar

RickToxik
Licensed Customer
Posts: 140
Joined: Fri Jan 13, 2012 9:22 pm
Location: Canada

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!
Attachments
CameraFocus.zip
Camera Focus control for maya & Octane
(3.22 KiB) Downloaded 457 times
Last edited by RickToxik on Sat Oct 19, 2013 12:47 am, edited 4 times in total.
User avatar
Slimshader
Licensed Customer
Posts: 92
Joined: Fri Sep 03, 2010 2:17 am
Location: Canada

Nice!
Thanks! :)
p3taoctane
Licensed Customer
Posts: 1418
Joined: Mon Jan 25, 2010 12:53 am

very cool
Thanks
Windows 7 Pro_SP 1_64 bit_48 GB Ram_Intel Xeon X5660 2.80 GHZ x2_6 580GTX_1 Quadra 4800
tchoa
Licensed Customer
Posts: 189
Joined: Thu Jul 01, 2010 12:33 pm
Location: Montpellier

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
Last edited by tchoa on Sat Sep 08, 2012 9:36 pm, edited 2 times in total.
Win XP x64 | Q6600 2.40GHz | 8Go | GTX470 1.28Go // Win 7 x64 | I7 Q740 1.73Ghz | 6Go | GTX460m 1.5Go
RickToxik
Licensed Customer
Posts: 140
Joined: Fri Jan 13, 2012 9:22 pm
Location: Canada

Ahhhhhhhhhhh!!!!!!........... Allright then! I'll add this to ze code : )

Thanks mate!
RickToxik
Licensed Customer
Posts: 140
Joined: Fri Jan 13, 2012 9:22 pm
Location: Canada

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 : )
RickToxik
Licensed Customer
Posts: 140
Joined: Fri Jan 13, 2012 9:22 pm
Location: Canada

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.
tchoa
Licensed Customer
Posts: 189
Joined: Thu Jul 01, 2010 12:33 pm
Location: Montpellier

:)
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
Win XP x64 | Q6600 2.40GHz | 8Go | GTX470 1.28Go // Win 7 x64 | I7 Q740 1.73Ghz | 6Go | GTX460m 1.5Go
RickToxik
Licensed Customer
Posts: 140
Joined: Fri Jan 13, 2012 9:22 pm
Location: Canada

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
abeoctane
Licensed Customer
Posts: 168
Joined: Thu May 01, 2014 5:44 am

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.
Post Reply

Return to “Autodesk Maya”