View Full Version : Expression Editor, covert python to MEL

06 June 2012, 10:11 AM
I am working on my graduating film and I have some candle flames on planes that I want that the planes to face the render cam of the shot.

As I know only python... I can't find how to do that in MEL
So I want the ".rotateY" always checking which camera is ".renderable" and make plane.rotateY = rendercam.rotateY

In python, it will give something like :

sceneCam = = True)
i = sceneCam.__len__()
x = 0

for test in sceneCam:
renderCam = cmds.getAttr(".renderable")
if renderCam = 0:
x +=1
myPlanRotate = "sceneCam[x].rotateY"

Can you help me to translate it in MEL x.x

I attempt this but it is not working

string $cam[] = `ls -ca`;
for ($x=0; $eachCam in $cam; $x++)
getAttr ($cam[x].renderable);

// myPlaneRotate.rotateY = $cam[x].rotateY

Thanks a lot !

06 June 2012, 10:19 AM
I'm not entirely sure what you're actually doing, but something like this?
string $cam[] = `ls -ca`;
for ($eachCam in $cam)
int $isRender = `getAttr ($eachCam + ".renderable")`;
if ($isRender) myPlaneRotate.rotateY = ($eachCam + ".rotateY");

06 June 2012, 10:56 AM
Thanks for your reply,

I want to create like an auto aim constraint on my planes.
In each shot, I want my planes to find the renderable camera and having the same rotate Y.

I try your piece of code and it doesn't seem to work.

For the moment, I can achieve a script in python that connect the renderable camera rotate Y in my planes but I have to do it over 70 shots.
So I wanted to find a way in Mel to make through the expression editor, an expression that autocheck every frame which is the render camera and, make my plane rotate equal to the render camera rotate

I found these piece of code that I wanted to mix but I can't find a way....
string $sel[] = `ls -sl`;
for ($eachObj in $sel)
connectAttr persp.rotateY ($eachObj + ".rotateY");

//You got to connect the rotation of the plane to the rotation of the camera you want it to face.

string $sel[] = `ls -sl`;
for ($eachObj in $sel)
connectAttr persp.rotateY ($eachObj + ".rotateY");

06 June 2012, 02:28 PM
Nevermind, it is just a stupid idea.....

06 June 2012, 06:00 PM
It's not a silly idea, but I don't think connecting the rotation of the planes to the rotation of the camera isn't likely to give you the effect you want. For example if you just pan the camera, the candle planes are no longer going to facing the camera.

I suggest looking into the aimConstraint node. For example the command below will set it up so the planes rotate around Y so that they face the camera. To test it out, select your camera then a plane and run...

aimConstraint -offset 0 0 0 -weight 1 -aimVector 0 0 -1 -upVector 0 1 0 -worldUpType "scene" -skip x -skip z;

This assumes your planes are created along the Z axis. Otherwise you'll need to change the aimVector. This also means you don't have to worry about expressions.

As far as connecting this all up, all you need to do is select the candle planes and run something along the lines of the mel below (you could also do this in python easily enough). In the example below it's assumed that the main rendering camera is called renderCam.

string $lssl[] = `ls -sl -fl`;

for ($i in $lssl)
aimConstraint -offset 0 0 0 -weight 1 -aimVector 0 0 -1 -upVector 0 1 0 -worldUpType "scene" -skip x -skip z renderCam $i;

Because it's possible to define multiple "renderable" cameras inside Maya, I'd either hard-code the camera name in the script (as above) or give myself the ability to select which camera I'd like to use as a target, something along the lines of...

string $lssl[] = `ls -sl -fl`;

for ($c = 1; $c < `size($lssl)`; $c++)
aimConstraint -offset 0 0 0 -weight 1 -aimVector 0 0 -1 -upVector 0 1 0 -worldUpType "scene" -skip x -skip z $lssl[0] $lssl[$c];

I hope that helps.

06 June 2012, 12:17 PM
I have been a bit discouraged because I figure this sort of workaround but the artists of my team have rotated them....

Meanwhile, a last hope come with your post, I will check if the rotation haven't been freezed :)
So I think I should be able to put them back at z axis.

Thanks for your time, you are a great help !

06 June 2012, 02:49 AM
Are you aware that there's python() command in mel, which can run python code?
Mel should be totally redundant.

06 June 2012, 07:56 AM
Hum... no... I didn't know...
I am a scripting beginner. ^^'
Thnaks... I didn't find time to test the auto-aim constraint.
But it seems that my colleague haven't rotate them ! :)

06 June 2012, 12:05 PM
I take the time to write it today,
It is not really an auto-aim, but a one click button is pretty a time saver :)
If someone is interrested by taking piece of my code, I pasted it bellow !

If you see something that can be "optimized", I will pick any recommendations !

PS : thanks for help and encouragements !

import maya.cmds as cmds
# set x while looping
x = 0

#store cameras
sceneCam = = True)

#check the numbre of cameras for a range loop
i = sceneCam.__len__()

for test in range(i):
renderCam = sceneCam[x] + ".renderable" #renderable attribute
renderableCam = cmds.getAttr(renderCam) #get attribute value
renderableCam = int(renderableCam) #return a 0-1 value

if renderableCam == 1: #store the cam if condition is True
camTransforms = cmds.listRelatives(sceneCam[x], p=True) #get camera transform node
currentrcam = camTransforms

print currentrcam #print the renderCam

sel_candleflame ="FlammeBougie*", fl=True, tr= True) #get items' transform node which will be constrained

#Apply the constraint on each planes
for constraintor in sel_candleflame:
cmds.aimConstraint ( currentrcam, constraintor, offset = [0, 0, 0], weight = 1, aimVector = [0, 0, -1], upVector = [0, 1, 0], worldUpType = "scene", skip = ["x", "z"])

06 June 2012, 02:01 PM
Hum... something is going wrong through the references and namespaces x.x
I think I should make a loop through the names :

sel_candleflame ="FlammeBougie*", fl=True, tr= True)
if sel_candleflame.__len__() == 0:
while sel_candleflame ==0:
y = "*:"
test = ((str(y)*x)+"*FlammeBougie*")
print test
sel_candleflame =, fl=True, tr= True)

CGTalk Moderation
06 June 2012, 02:01 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.