PDA

View Full Version : Expression Editor, covert python to MEL


DrWeeny
06-08-2012, 11:11 AM
Hi,
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 :

myPlanRotate
sceneCam = cmds.ls(cameras = True)
i = sceneCam.__len__()
x = 0

for test in sceneCam:
renderCam = cmds.getAttr(".renderable")
if renderCam = 0:
x +=1
else:
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`;
$x=0;
for ($x=0; $eachCam in $cam; $x++)
{
getAttr ($cam[x].renderable);

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

Thanks a lot !

NaughtyNathan
06-08-2012, 11: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");
}

DrWeeny
06-08-2012, 11: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");
}

DrWeeny
06-08-2012, 03:28 PM
Nevermind, it is just a stupid idea.....

earlyworm
06-08-2012, 07: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.

DrWeeny
06-11-2012, 01: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 !

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

DrWeeny
06-13-2012, 08: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 ! :)

DrWeeny
06-15-2012, 01: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 !

----EDIT----
PS : thanks for help and encouragements !

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

#store cameras
sceneCam = cmds.ls(cameras = 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
else:
x+=1

print currentrcam #print the renderCam

sel_candleflame = cmds.ls("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"])

DrWeeny
06-15-2012, 03: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 = cmds.ls("FlammeBougie*", fl=True, tr= True)
x=1
if sel_candleflame.__len__() == 0:
while sel_candleflame ==0:
y = "*:"
test = ((str(y)*x)+"*FlammeBougie*")
print test
sel_candleflame = cmds.ls(test, fl=True, tr= True)
x+=1

CGTalk Moderation
06-15-2012, 03: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.