Sep 06, 2011
Post id: 308707 Report Item

Hi, I'm out of my league when it comes to expressions, but I found a script that performs exactly how I want it to after a few modifications. However it only seems to update once and does not continually evaluate when I move the camera..

Is there a way to get the code to continually evaluate when moving the camera around the viewport?


//set aim axis to X 
$aimAxis = <<1,0,0>>;
$rotAxis = <<0,1,0>>;
//get worldpos of locator and discard y ->set to 0
vector $myLoc = `xform -q -ws -t persp`;
$myLoc = <<$myLoc.x,0,$myLoc.z>>;
//get worldpos of particle and discard y ->set to 0
vector $pPosition = pPlane1ParticlesShape.worldPosition;
$pPosition = <<$pPosition.x, 0, $pPosition.z>>;
// I think I need to take the particle position (offset from the origin) into account vector
$aim = unit($myLoc - $pPosition);
//calculate angle and assign to custom rotation attribute float
$myDegAngle = angle( $aimAxis,$aim);
float $myAngle = `rad_to_deg($myDegAngle)`;
//check if angle is larger than 180 and use the inverse angle vector
$testRotate = `rot $aimAxis $rotAxis $myDegAngle`;
float $isFlipped = angle($testRotate,$aim);
if ($isFlipped > 0.0001) $myAngle = (180 - $myAngle) + 180 ;
//finally assign the value to the custom attribute
pPlane1ParticlesShape.aim = <<0,$myAngle,0>>;

Sep 06, 2011
Post id: 308713 Report Item

as far as i know, expressions dont work that way, and they shouldnt be written of used the same way as scripts.

i recommend compressing this into something the expression editor CAN use well, and connecting the output to your custom attribute.

otherwise, in order to use the script you already have, in a nasty way, tie it into a global proc and call it with a scriptJob (sorry guys! just my personal opinion approaching this!) as long as you keep on top of it, ie create a window which has the scriptjob parented to it. delete the window, delete the scriptJob, BUT i do recommend the creating a working expression.

Sep 06, 2011
Post id: 308715 Report Item

Thanks, ldunham1. I thnk I'll go the working expression, although sounds like a challenge!

Sep 07, 2011
Post id: 308716 Report Item

ldunham1 explanation is bad, and it contains very bad advice. It has nothing to do with global procs, and that is not a issue in any case. ScriptJobs WILL NOT WORK, because scripJjobs get disabled on update, and using one would be  suicide anyway.

The problem is:

vector $myLoc = `xform -q -ws -t persp`;

That is a query, nodes are explicitly assumed not to query anything outside themselves. Nodes are assumed to bring nothing from the surroundings to themselves, and all their data is based on things they have inside themselves: the mechanism used to make this workable is connections, connections bring in info to the node, the nodes itself has nothing to do with the transport mechanism. Expressions are for all intents and purposes just that custom evaluation nodes. Now maya does not ever enforce said rules, because it MIGHT be useful to break the rules for some corner cases. Imagine a law that stated that all stealing is wrong, well it turns out that courts all around the world allow for corner cases where it is in fact totally legal to steal property of others if the stealing is for the greater good such as saving a persons life from a a imminent threat. This does not mean that the rule is not beneficial 99.99% of cases, and this also applies here.

Queries do not know when the value changes, they just know how to fetch the value. Connections on the other hand do 3 things:

  • They inform the downstream nodes when their values are out of date

  • Prune off calculations that do NOT need to do anything because they fail affect anything meaningful in the program

  • Ensure things get executed in the right order, when the values are updated (along with ensuring maya does NOT go into a infinite loop)

So then how do you do the same thing as xform -q -ws -t? Well you either point multiplicate persp.t  with persp.worldMatrix. As a easier alternative just parent constraint a world space transform to persp and use that as input (if you mark the transform as intermediate then maya will hide the node from outliner reducing clutter). Other alternatives exits as well.


This expression is largely unnecessary and does the wrong thing for 90% of purposes. Only if you really use a a ray tracing lense shader does it really work out, or because you need some secondary ray effects (if tough you need both and this problem is visible in the reflections, which is generally not the case you need 2 particle systems). Because the 3d camera cheats a bit causing the direction to be planar instead of curved like in areal world, this means that to get things pointing your way its enough to just use a orient node at the instanced shape and no need for the per particle expression.

Sep 07, 2011
Post id: 308720 Report Item

joojaa lol i feel like a naughty schoolboy,  will take on board :)

Sep 07, 2011
Post id: 308721 Report Item

Lol :) Now if only you could get that same feeling of excitement and wonder back...