[MEL] Select camera and ungroup

[MEL] Select camera and ungroup

nikola.trajchevski
Explorer Explorer
726 Views
6 Replies
Message 1 of 7

[MEL] Select camera and ungroup

nikola.trajchevski
Explorer
Explorer

Ok so I need some way to select a camera with a random name, that isn't one of the 4 default cameras.

For example I might have Cam123 somewhere in the scene in a group or whatever, I need to select it and move it out of the group "parent -world"

 

any ideas?

 

PS: the only consistent variable is that the Camera will always have an imagePlane attached to it. Everything else is random. If that helps narrow it down.

0 Likes
Accepted solutions (1)
727 Views
6 Replies
Replies (6)
Message 2 of 7

Kahylan
Advisor
Advisor

I'm not sure what you mean with  "camera with a random name". Do we know the name?

 

If we know the name, all you need to do is:

string $cam = "Whatever_the_name_is";
parent -w $cam;
select -r $cam;

 

If you don't know the name of your camera, you don't have enough information to do anything really useful.

This script looks at all the cameras and unparents the first one that has an imageplane attached:

// Get all Cameras and all cameras that can not be renamed
string $cameras[] = `ls -ca`;
string $perCameras[] = ls( "-ud", "-type", "camera" );
// Remove the Cameras that can not be renamed from the camera List
string $userCameras[] = stringArrayRemove($perCameras, $cameras);
// Loop through camera list
for ($c in $userCameras){
    // check if camera has imagePlane attachted
    string $conn[] = `listConnections ($c + ".imagePlane")`;
    if (size($conn) != 0 ){
        //parent camera to world and then select it
        string $cT[] = `listRelatives -p $c`;
        parent -w $cT[0];
        select -r $cT[0];
        break;
    }

}

 

I don't really get how this is useful, but I hope it helps!

0 Likes
Message 3 of 7

nikola.trajchevski
Explorer
Explorer
yes by "random name" i mean it can be anything. Based on user preference. Camerasdf, camera124... etc.

I tried your script in a test scene, but it didn't do anything. Nothing changed and no errors.
0 Likes
Message 4 of 7

Kahylan
Advisor
Advisor

But, I assume that this camera is created by a script of you, right? So why aren't you tagging it?

 

If the script did nothing and there were no errors, then you either didn't have a camera with an imageplane in your scene, or it found a camera that was already parented under root and it just selected that camera. As I said, it just randomly picks the first camera it finds that fit the criteria...

0 Likes
Message 5 of 7

nikola.trajchevski
Explorer
Explorer

No no. The camera is sourced from users. Let me explain it better.
User creates a scene using their template.
Usually it contains 1 or more cameras in a group and a few meshes. The cameras have reference images. For environment building or whatever.
But every name in the scene is different, depending on how the user named it. For example "Tom" wanted his camera to be named "sceneTrees" and "Bob" wanted his camera to be named "asdfghjk".
I have no control over that

So the only thing that I can be certain of is that those cameras will be in a group and will contain an imagePlane with some jpg. I need this script to Clean-up the scene so it doesn't look like a tornado went through the outliner.

-----
Your script didn't do anything as I said. I don't know why. No cameras were selected and no errors were shown. I made sure that those cameras were grouped as usual. And yea they had an ImagePlane tab with a jpg in it.

Maybe your script only works in a specific Maya version? I know they changed how Maya processes code in 2022.

I'm using Maya 2020.

----

I suppose if there's nothing that can be done to Find and Move the camera out of every group... I could try plan B and make it "Selection" based. Select the camera(s), click the script, put the cameras in a string and parent to world.

But again that's plan B. It would require the User to click the correct camera, which creates room for User error.

0 Likes
Message 6 of 7

Kahylan
Advisor
Advisor
Accepted solution

Ah ok, that explains it. Some of the flags for the ls command don't do quite the same since Maya 2022.

This code should work in 2020:

// Get all Cameras and all cameras that can not be renamed
string $cameras[] = `ls -ca`;
string $perCameras[] = ls( "-pn", "-type", "camera" );
string $selArray[];

// Remove the Cameras that can not be renamed from the camera List
string $userCameras[] = stringArrayRemove($perCameras, $cameras);
// Loop through camera list
for ($c in $userCameras){
    // check if camera has imagePlane attachted
    string $conn[] = `listConnections ($c + ".imagePlane")`;
    if (size($conn) != 0 ){
        //parent camera to world and then select it
        string $cT[] = `listRelatives -p $c`;
        parent -w $cT[0];
        $selArray[size($selArray)] = $cT[0];
        
    }

}

select -r $selArray;

 

Now the thing just is, that you can either unparent one camera that fits your criteria or all of them. Since you don't have any specific attribute, nodestate or tag on the camera that differenciates your template camera from any other camera that was created by the user you will inevitably do the same thing to cameras you probably don't want to unparent.

Message 7 of 7

niktr96
Contributor
Contributor
Alright. This script does what I need it to do. I modified it a bit for some unusual scenarios.
Thank you.
0 Likes