View Full Version : create poly object based on particle positions
jporter313 04-14-2008, 11:28 PM So I don't know a whole lot of MEL scripting, but I figure this shouldn't be too hard to do and I was wondering if someone could help me out.
What I want to do is create a script that will create a polygon object based on the positions of particles in a particle object. In other words, it will create one point in the polygon object at the same point as each particle in a targeted particle object. Also, the numbers will have to match, so poly point 1 will be at the same place as particle 1.
I don't know MEL syntax very well, but I think the logical structure would go something like this
for (i=0; i<particleObject.numberOfParticles; i++) {
vector $position = position of particle at index [i];
create new point in polygon object at $position;
}
can anyone help me translate this into something that would work in MEL?
|
|
I am not sure I fully understand what you are looking for but I think I can help you out.
Questions-
1) do you want to create a new polygon object for each particle in the system as it is created or edit the shape node of a polygon object and add/remove vertex data as the system runs?
2) do you want the polygon(s) to reposition as the timeline changes?
If you have a test file available it would give me a better understanding of what you are looking to achieve and I may be able to write something up for you.
jporter313
04-15-2008, 12:13 AM
I am not sure I fully understand what you are looking for but I think I can help you out.
Questions-
1) do you want to create a new polygon object for each particle in the system as it is created or edit the shape node of a polygon object and add/remove vertex data as the system runs?
2) do you want the polygon(s) to reposition as the timeline changes?
If you have a test file available it would give me a better understanding of what you are looking to achieve and I may be able to write something up for you.
Hey Eric,
thanks for the reply.
In answer to your questions, all I want it to do is create points in a single polygon object for each particle in a particle object, and no I wouldn't want the polygon object to move as the timeline changes, it should stay static.
Maybe it would help if I stated what I'm trying to do. I want to be able to take a snapshot of a particle system at a given point in it's simulation, and then use that as a goal object to be able to snap the particles into those positions at any time.
The only other thing is, it's very important that the ordering stay the same. In other words, if a given particle was at a given point during the goal object creation, I need the same particle to snap back to the same point when I activate the goal.
jporter313
04-15-2008, 12:24 AM
Sorry, missed the test file thing.
Basically, to test it, you'd just need any group of particles. The idea is that you'd start with only particles and then create a poly object with one vertice at the location of each particle.
jporter313
04-15-2008, 12:48 AM
Ok, I created a very simple sample file. It contains a 10 particle object and an example of the resulting polygon object I would want the script to create.
r4inm4ker
04-15-2008, 01:30 AM
As far as i know, there's no command in MEL to create a polygon from scratch. API would be more suitable for this task.
wrend
04-15-2008, 06:31 AM
you can goal one particle object to another..., just duplicate your particle system at the frames/comformations of interest? As long as your particle system has persistant particles, you're sorted?
Hey Justin,
I just got in and I will take a look at that test file as soon as I get maya up and running. but as a side thought if you just need visual place holders to remember the location of the points in space for each particle you could alternatively use Locators-- then use a custom naming convention to keep them matched up with specific particles.
at the moment my maya licence is out of commision so I might not get to take a look at it til later tonight.
jporter313
04-15-2008, 05:27 PM
As far as i know, there's no command in MEL to create a polygon from scratch. API would be more suitable for this task.
Hey Jefri,
I was thinking of using this command:
polyCreateFacet [-constructionHistory boolean] [-hole] [-name string] [-point float float float] [-subdivision int] [-texture int]
which is essentially the "create polygon" command, problem is the command has to contain a number of vector floats for the points, so that might be more complicated than adding points in succession.
jporter313
04-15-2008, 06:56 PM
you can goal one particle object to another..., just duplicate your particle system at the frames/comformations of interest? As long as your particle system has persistant particles, you're sorted?
That's a great idea wrend, I'll try that.
adding each of the points would not be too difficult if you use a string eval method to execute the command.
string $cmd = "polyCreateFacet -n $nameHolder";
for ($particle in $particleSystem) {
float $loc[] = //query the location of the particle.
$cmd += ( " -point " + $loc[0] + " " + $loc[1] + " " + $loc[2] );
}
eval $cmd;
I still cant do any testing atm, but like I mentioned earlier I will try some things out. Did you have anything against using locators?
jporter313
04-15-2008, 07:04 PM
adding each of the points would not be too difficult if you use a string eval method to execute the command.
string $cmd = "polyCreateFacet -n $nameHolder";
for ($particle in $particleSystem) {
float $loc[] = //query the location of the particle.
$cmd += ( " -point " + $loc[0] + " " + $loc[1] + " " + $loc[2] );
}
eval $cmd;
I still cant do any testing atm, but like I mentioned earlier I will try some things out. Did you have anything against using locators?
ok, that's right, I remember the eval thing. now to find out how to query the particle positions.
ok I just did a test on querying hte particle positions, this should do the trick.
int $count = `particle -q -count particleShape1`;
for ($i = 0; $i < $count; $i++) {
float $loc[] = `particle -attribute position -id $i -q particleShape1`;
print $loc;
}
obviously you will want to change the shape from particleShape1 to whatever the name is of the particle system you created.
jporter313
04-15-2008, 07:39 PM
Here's a question, just for my own learning purposes. If I were to make this into a procedure and accept the particle object name as input, would I just have to change this:
int $count = `particle -q -count particleShape1`;
to this:
string $particleCountCommand = ( "particle -q -count " + $particleObjectName );
int $count = eval $particleCountCommand
Assuming $particleObjectName was the string input variable for the procedure.
you would not actually need to use the eval command in this instance.
it may look something like this with the procedure.
global proc myProcName (string $particleObjectName) {
int $count = `particle -q -count $particleObjectName`;
//the rest of the procedure down here.
}
mind you the method you have setup looks like it would still work. its just cleaner and easier to read if you cut out the redudnant code used by the string/eval method here. The reason being you do not need to add multiple flags to the command (as you did when querying the particles location per point)
hope that makes sense and explains it.
jporter313
04-15-2008, 08:08 PM
you would not actually need to use the eval command in this instance.
it may look something like this with the procedure.
global proc myProcName (string $particleObjectName) {
int $count = `particle -q -count $particleObjectName`;
//the rest of the procedure down here.
}
mind you the method you have setup looks like it would still work. its just cleaner and easier to read if you cut out the redudnant code used by the string/eval method here. The reason being you do not need to add multiple flags to the command (as you did when querying the particles location per point)
hope that makes sense and explains it.
Well, that's easier.
Another question, just looking at the particle position query script, it works great, but there's one thing I don't quite understand about how it works. In each loop, the script puts the position of the queried particle in a float variable, and then prints the value of that float.
What I don't understand is, the position of the particle is a vector, and therefore has 3 values, so how is Maya sticking those 3 values in a single float variable?
Thanks again for all your help so far, I'm learning a lot about Maya scripting by doing this.
ok so it seems you already know about different types of variables (string, int, float)
your variables can also be declared as Arrays. the array is like taking multiple variables and combining them into a single variable that uses indexes to seperate the data stored in the variable (like indexes for each particle in the particle system).
when you declare a variable as an array it will have brackets at the end of the variable name for instance: float $myArray[]
so in the case of the position of the particles we are returned with 3 values- x, y and z.
each of these is stored in the same variable $myArray, but with different indexes. $myArray[0] = x
$myArray[1] = y
$myArray[2] = z
Arrays are zero-based and the index will always count from 0 upwards.
That is just an intro to arrays, you can have much larger arrays that contain large amounts of data you need to organize and track.
jporter313
04-15-2008, 08:33 PM
ok, got it, so by saying "float $loc[]" rather than just "float $loc" you were instantiating a float array, rather than just a single float. That makes sense.
I do have some limited scripting knowledge, I'm mostly just trying to figure out the specifics of how to do things in MEL.
let me show you what I'm testing:
string $particleObjectName = "Sample_Input_Particle_Object"
string $cmd = ( "polyCreateFacet -n" + $particleObjectName );
int $count = `particle -q -count $particleObjectName`;
int $i;
for ( $i=0; $i < $count; $i++) {
float $loc[] = `particle -attribute position -id $i -q Sample_Input_Particle_Object`;
$cmd += ( " -point " + $loc[0] + " " + $loc[1] + " " + $loc[2] );
}
print $cmd
The problem is it keeps giving me this error:
// Error: string $cmd = ( "polyCreateFacet -n" + $particleObjectName ); //
// Error: Syntax error //
Am I concatenating that string incorrectly? If so, what's the proper command to do that?
it looks like you missed a semi-colon after the first line there, I think thats where you are getting the errors for the next line below it.
also, be sure to put a space after -n flag for the $cmd string otherwise it will end up with an error when you goto eval that string (-nSample_Input_Particle_Object instead of -n Sample_Input_Particle_Object)
jporter313
04-15-2008, 08:48 PM
it looks like you missed a semi-colon after the first line there, I think thats where you are getting the errors for the next line below it.
also, be sure to put a space after -n flag for the $cmd string otherwise it will end up with an error when you goto eval that string (-nSample_Input_Particle_Object instead of -n Sample_Input_Particle_Object)
Yep, just found the missing semicolon, man that stuff is hard to spot sometimes. I'll fix the -n problem.
jporter313
04-15-2008, 09:18 PM
Ok, I got it to work, here's the final procedure:
global proc particleTargetCreate ( string $particleObjectName, string $outputName ) {
string $cmd = "polyCreateFacet -n " + $outputName;
int $count = `particle -q -count $particleObjectName`;
int $i;
for ( $i=0; $i < $count; $i++) {
float $loc[] = `particle -attribute position -id $i -q $particleObjectName`;
$cmd += ( " -point " + $loc[0] + " " + $loc[1] + " " + $loc[2] );
};
eval $cmd;
};
Thanks again Eric. Working on anything interesting over at Midway?
I just got a new computer and they did not order my maya license so I have been out of comission for 2 days now. hence why I am browsing these forums 247 :D
jporter313
04-15-2008, 09:58 PM
I just got a new computer and they did not order my maya license so I have been out of comission for 2 days now. hence why I am browsing these forums 247 :D
Ha, good thing for me I guess. Similar thing happened to me a couple of weeks ago, luckily we found a decent workstation lying around with half of the software I use on it.
jbradley
04-16-2008, 02:32 PM
Very interesting. Let's say someone (me) wanted to take this treatment to the next level and use animation associated with the particles to drive polygon shards (per-vertex)?
1. Loop over particles and generate a polygon shard from a variable length of particles (3-10 maybe?).
2. Dynamically link particle motion to vertex positions in the polygons so that they will be affected by forces in the environment.
3. Keep looping over whenever new particles are generated to continue to 'shoot out' polygon shards from the particle data, all the while keeping track of which particles were visited already so they are not reused.
Couple of problems....
1. Would it be better to just generate the particles on the fly through scripting and set them in motion with some random velocity / accel?
2. How to keep count of the particles that were visited? Is there a reference for particles that is always increasing?
3. How do you deal with the scenario (general sense) when particles die? Hmm...
Just throwing out some ideas here.
I'm trying to get into more generative art stuff with Maya. Just hard to find resources for this type of stuff I guess.
Anyway, cool discussion.
jbradley- you could definitely get some interesting results using particles in this way-- I almost imagine stalactite growing from a cave using particles in the way you described.
as for the problems you listed below-- you would certainly need to track the data of the particles (or possibley create procedures that take action after certain events such as particle death.) in any case, because you want to continously execute commands over time you should use your scripts in expressions.
BenBarker
04-16-2008, 05:35 PM
You're starting to get into the territory of meshing an arbitrary point cloud, which can get really complex. You might want to take a different tack, and start thinking perhaps about metaballs, blobbies, or something similar.
Here's one plug-in:
http://www.highend3d.com/maya/downloads/plugins/modeling/misc/2776.html
jporter313
04-16-2008, 06:22 PM
Aren't blobby particles in Maya basically metaballs? What does that plugin do that's extra?
jporter313
04-16-2008, 09:14 PM
Well, first test just now, worked great. Created an object with 23160 points from a 23160 particle object.
jbradley
04-18-2008, 05:07 PM
You're starting to get into the territory of meshing an arbitrary point cloud, which can get really complex. You might want to take a different tack, and start thinking perhaps about metaballs, blobbies, or something similar.
Here's one plug-in:
http://www.highend3d.com/maya/downloads/plugins/modeling/misc/2776.html
I thought about that but it's not meshing an arbitrary point cloud.
It's generating polygonal facets with a defined iteration (ie, every 3,4,5..N particles). The tricky part is how to associate the particles with a mesh in a dynamic fashion - similar to softbody particles except that it's 1) rigid and 2) constantly adds new polygon facets as the particle system emits (untill the particles die, then some decision making on what to do with the orphaned poly).
CGTalk Moderation
04-18-2008, 05:07 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.