PDA

View Full Version : Accessing per-particle attributes with MEL



goomba5
05-03-2011, 01:56 PM
I would like to access a per- particle attribute using MEL. For example, I would like to write an expression that say, takes a certain particle's velocity to drive the translate for an object. Anybody know how to do this?

I was using `xform -q -t -a -ws nParticle1.pt[0]`; to find a particle's world space. But that method won't get me velocity or any custom rotational values for example.

Help please!

Also:
I am using the afore mentioned xform line to link a sphere's translate to a particle's world space. The problem is it jumps around as the other particles are being born (snapping to new particles) seemingly not listening to the particle it's supposed to be linked to. Here's the weird part: If I stop the animation and go to my sphere's expression editor, when I click "edit" on the expression editor (absolutely no changes to the expression itself) the sphere jumps into the correct place. Here's the expression I was using on the sphere's translate:

float $particlePosition[] = `xform -q -t -a -ws nParticle1.pt[0]`;
translateX = $particlePosition[0];
translateY = $particlePosition[1];
translateZ = $particlePosition[2];

Any thoughts on that? What am I doing wrong??
Is there some way I can force it to update every frame instead of having to click the "edit" button in the expression editor??

Thanks for your input!

chrisg
05-09-2011, 11:48 AM
Hi goomba5,

I just re-created the scene with your expression and it seems to work in Maya 2011. Are any of your particles dieing? If your particles die, a new particle gets assigned into position 0. Can you go into a bit more detail on what the simulation looks like?

goomba5
05-10-2011, 10:49 AM
The animation is a salt crystal dissolving in water. It is at the molecular level so it's just a bunch of spheres. First the Na and Cl atoms form a regular cube pattern. Then the water molecules bond with both each the Cl and Na atoms (6 water molecules per atom) and essentially "peel" them off of the cube pattern.

I have a lattice I am using as a goal for the particles to create the perfect cube pattern (goal weight of 0 after I set initial state). The Na and Cl atoms are the (NURBS) spheres that get the expression. The expression links the Na or Cl sphere to a corresponding particle in the afore mentioned particle object. The particles are set to live forever and I triple checked their particle Id to make sure the numbers were correct. I need to use actual NURBS spheres because I am instancing the water molecules and that way, I can use goalU and V to attach them to the Na and Cl, and animate them a bit after they are attached.

The problem is in the expression. The particles are all good. They don't move. They're all in the right place, and I have them on numeric display so I can see that their id numbers are correct. The problem is, the first sphere jumps around and I need to click "edit" in the expression editor to get it to go where it should. I have deleted the expression on the sphere that is (was) linked to nParticle1.pt[0] and when I do that, the sphere linked to nParticle1.pt[1] jumps instead.

I think this problem could be solved if I could force maya to calculate the expression instead of me hitting "edit" in the expression editor. On the other hand, the fact that all I need to do to fix it is click "edit" makes me think there is something buggy about my setup.

Weird, right? Any thoughts?
Thanks for your time,
-G

chrisg
05-10-2011, 11:32 AM
Hi goomba5,

Hrm, I remember some trick to forcing an expression to update every frame.

Try this expression:


float $particlePosition[] = `xform -q -t -a -ws nParticle1.pt[0]`;
translateX = frame;
translateX = $particlePosition[0];
translateY = $particlePosition[1];
translateZ = $particlePosition[2];

bravo2zero
05-10-2011, 12:24 PM
I think you will need to wrap the whole function in a for loop so you can increment on a per particle basis .

maybe something like this.



// select the first logical particle so we have a particle selected.
select nParticle1.pt[0];
// select all particles in scene at present time
string $particleSelection[] = `ls -type particle`;

// check particle selection is not equal to nothing.
if(!($particleSelection[0]==""))
{
select -r $particleSelection;
for($i=0;$i<size($particleSelection);$i++)
{
//get the particle xyz position
float $particlePosition[] = `xform -q -t -a -ws nParticle1.pt[i]`;
translateX = $particlePosition[0];
translateY = $particlePosition[1];
translateZ = $particlePosition[2];

//also print particle name and the xyz position for debugging
print nParticle1.pt[i];
print $particlePosition[0];
print $particlePosition[1];
print $particlePosition[2];

//do anything else you want for each particle here
}
}

//end of sample program.


i have not tested or debugged this but it might be handy for you .

but you might be able to call this script at each frame so you dont miss any particles using a scriptnode etc.