PDA

View Full Version : How to insert a variable in a Particle Expression ?


SebKaine
06-23-2011, 05:37 PM
i have 2 particles and 2 spheres the goal is that my 2 particles take radius and position of my two sphere.
i can do this perfectly with this pretty dirty syntax :)

if(nParticleShape1.particleId==0)
{
nParticleShape1.radiusPP = obj_0.scaleX;
nParticleShape1.position = <<obj_0.translateX,obj_0.translateY,obj_0.translateZ>>;
}

if(nParticleShape1.particleId==1)
{
nParticleShape1.radiusPP = obj_1.scaleX;
nParticleShape1.position = <<obj_1.translateX,obj_1.translateY,obj_1.translateZ>>;
}

But i would like to know how to do this in a cleaner fashion, something like :


nParticleShape1.radiusPP = "obj_"+id+".scaleX";
nParticleShape1.position = <<"obj_"+id+".translateX","obj_"+id+".translateX","obj_"+id+".translateX">>;
Of course this one doesn't work but you get the idea
In MEL we easily loop beetween objects with the getAttr("obj_"+$i+".translateX")

i am wondering how to achieve something like this in a particle expression ?

thanks for your help guys ! :)

animatedfox
06-23-2011, 07:45 PM
Don't have time to try this right now...
First suggestion would be to make sure your strings are in ("obj_"+id+".scaleX")
If that doesn't do the trick try defining a new variable first outside the IF statement...then use that variable in the IF statement.

$variableScale = ("obj_"+id+".scaleX");

if(nParticleShape1.particleId==0)
{
nParticleShape1.radiusPP = $variableScale;
}

Maybe...maybe not...
Some things that are fine in scripts can be problematic in particle expressions:
Hope this helps.

SebKaine
06-23-2011, 10:14 PM
Well Benjamin Thanks a LOT it works !!! :thumbsup:

float $scale = ("ob_"+nParticleShape1.particleId+".scaleX");
float $X = ("ob_"+nParticleShape1.particleId+".translateX");
float $Y = ("ob_"+nParticleShape1.particleId+".translateY");
float $Z = ("ob_"+nParticleShape1.particleId+".translateZ");

nParticleShape1.radiusPP = $scale;
nParticleShape1.position = <<$X,$Y,$Z>>;

this syntax works perfectly ! i forgot the parenthesis beetween the expression.

this is a really great feature ! now i have metaballs in Maya, with 5 line of code .

THX again !

animatedfox
06-23-2011, 10:38 PM
Glad it worked!!! :beer:

SebKaine
06-23-2011, 11:55 PM
OUtch i spoke too fast!! i forgot to delete my runtime expression ...

it was this runtime expression, that makes everything OK, so at the end , it doesn't work with this syntax

float $scale = ("obj_"+nParticleShape1.particleId+".scaleX");
nParticleShape1.radiusPP = $scale;

i have an error :
converting string obj_1.scaleX to a float value of 0

so maya doesn't understand that i want to access to a obj_1.scaleX, but i'm not allowed to use a getAttr to help him.

so i'm back to my starting point ... :)

if you have another idea it would be cool !

nfrancisj
06-24-2011, 05:08 PM
not 100% clear on what your asking...but from what I gather you're trying to hook up a geo's attr to a particle value?

if so... this works

float $myVar = pSphere1.scaleX;
particleShape1.radiusPP = $myVar;


Also I think this would work if you change your scale to the scaleX of the object and not the particle.

float $scale = ("ob_"+nParticleShape1.particleId+".scaleX");
float $X = ("ob_"+nParticleShape1.particleId+".translateX");
float $Y = ("ob_"+nParticleShape1.particleId+".translateY");
float $Z = ("ob_"+nParticleShape1.particleId+".translateZ");

nParticleShape1.radiusPP = $scale;
nParticleShape1.position = <<$X,$Y,$Z>>;

Duncan
06-25-2011, 01:42 AM
Inside an expression the parser will replace every direct string of the form "obj.attribute" with a connection to that attribute. The syntax:

obj.tx = 1;
float $v = obj.ty;

is only supported in expressions. Names on the right of "=" become input connections and on the left become output connections to the expression node. This requires that the string be directly specified this way, not built with variables. For example you CANNOT have a loop inside an expression like this:
for( $i = 0; $i < 100; $i++ ){
float $val = ("obj"+$i+".translateX");
}

To work this would require creating 100 node connections to the the expression, but currently this sort of syntax is not allowed. (the number of connections would be undefined if the count was a variable its self) Thus if you want to do something where you construct the object name like above you need to use getAttr. Get/setAttr is less efficient because it does not use dependancy graph connections but it should work all the same.
for( $i = 0; $i < 100; $i++ ){
float $val = getAttr("obj"+$i+".translateX");
}

Duncan

SebKaine
06-26-2011, 07:43 PM
Nicholas
Thanks for your answer !
My goal was to insert a variable in a string of a particle expression sorry if i wasn't clear enough !
float $myVar = pSphere+<varName>+.scaleX;
particleShape1.radiusPP = $myVar;

Duncan
thanks for your very precise answer , you get exactly what i was trying to do. So it's not allowed to insert variables into a string when using expression.

my goal is to make a metaball tool by using nurbs sphere to model the meta ball objects. so on each sphere you have particle that follow the position and the radius of the sphere. Then i nMesh the particles to get the blobby. For this is use the expression of my first post.

it works but if i have 1000 particles if find that having 5000 lines of redundant code was not very elegant. when you have 5000 lines of codes in a particle expression can we consider that it's not very good ? or does maya will manage this easily ?

maybe you could give me some advise on how you would achieve this ?

thanks again for your help

CGTalk Moderation
06-26-2011, 07:43 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.