View Full Version : General MEL/expression Questions

03 March 2006, 03:06 PM
I am currently making a script where i have a lot of modefyed cubes (A LOT of cubes!) I want to animate some of the vertexes of that object, someting like:

object.vtx[1:10].position = time;

Would this be really slow? As i would have this script (with some if statements and some rendom values and so on aswell) on something like 200x200 cubes?

Is there a more efficent way of doing this? Maybe by creating a cluster and move the cluster(one cluster per object)?

I also have a few bonus questions if someone have some time :P

How to make this line work?
object.vtx[1:10].position = time;

How to do like:
object.PosY = (frame - where the objects start to move) eg, if the object starts to move at frame 10 posY=(frame - 10) If that makes sense

Thanks for your time

03 March 2006, 05:59 PM
You should see a great increase in speed when you combine all the cubes into one object.
I'd recommend that you then interate over all the vertices of the mesh and set their position on a more general rule that doesnt require vertex ids.

Like say for example you move every vertex that is above 1 on the y axis depending on its x and z position.
If you really do care about speed then I'd also write that as a dg node (Maya API).

03 March 2006, 06:34 PM
well, I need to have all the cubes as different objects. But I could easily add say a cluster to the vertexes , so one cluster per object.

I have no idea on how to make it a plug-in, if it is that you refer to by maya API. Still new to MEL and scripting in general

03 March 2006, 09:02 PM
object.vtx[1:10].position = time;

will not work, unless you want all the points of each cube to be in the same would need it to be time plus some default offset for each vertex on the cube. I don't know the final result you are trying to achieve but a cluster could be you want the cubes to grow uniformly? Maybe a bit more info about the final effect you are trying to achieve could help.

03 March 2006, 08:54 AM
Something like this?

file -f -new;

// Create a grid of cubes and combine them to one mesh so we can handle it better
$sizeX = 10; // The number of cubes in x
$sizeZ = 10; // The number of cubes in z

for( $x=0; $x<$sizeX; $x++ )
for( $z=0; $z<$sizeZ; $z++ ) {
move -a ($x-($sizeX/2.0)) 0 ($z-($sizeZ/2.0));
select (eval("ls -type transform")); // Select all transform nodes (here theres only the cubes)
polyUnite; // Combine them
DeleteHistory(); // Delete the history so we get rid of the empty transforms left behind
rename wurst; // Name the new mesh "wurst"

// This function is called by the expression (we create later) and moves the vertices
// based on their position (x and z) and the current frame.
global proc animateVerts( string $object )
int $numVerts[]; // Here we will later store the nuber of vertices in "wurst". Must be an array because thats what polyEvaluate returns
int $curTime = `currentTime -q`; // We get the current frame
float $newHeight = 0.0; // We'll store the new y position for the vtx here
$numVerts = `polyEvaluate -v $object`; // We query the number of vertices for "wurst"
vector $vtxPos; // Here we'll store the current position of the vertex

for( $i=0; $i<$numVerts[0]; $i++ ) // Iterate over the vertices
$vtxPos = `getAttr wurst.vrts[$i]`; // Get the position of the current vertex
if( $vtxPos.y > 0 ) // If its y position is greater than 0
$newHeight = (sin( ($vtxPos.x+$curTime)/5 ) + cos( ($vtxPos.z+$curTime)/5 ))/2+1;
// We calculate the new height for the vertex
setAttr wurst.pnts[$i].pnty $newHeight; // Then we set it
refresh -cv -f; // We force the current viewport the refresh

cycleCheck -e off; // This line removes some nasty messages :)
expression -s "animateVerts \"wurst\";" -o "" -ae 1 -uc all; // Here we create the expression

// Avoid stupid Maya bug where the mesh doesnt refresh by moving a point interactively
select wurst.vtx[1];
move -r 0 0 0;
select -cl;

// Create light to make the whole thing look nicer ;)
move 2 10 2 pointLight1;

// Switch to shaded mode and play the animation

Hope that helps. It creates an expression that evaluates every frame and moves the vertices like I said in the example in my last post.

EDIT: Code highlighting (especially for comments would really come in handy :/

03 March 2006, 09:22 AM
wow tciny, you must have been of a great help to H3ro. Keep up the good work. You rock!!!


03 March 2006, 10:30 AM
Wow, thank you alot. This looks like just the thing i wanted. I am at school now :( , so cant really try it out. But i will as soon as I get home

Thanke you again:)

Well, I am now using my laptop, on the school. Hope not the system admin see me:p

The script did not really make the effect i am looking for, but it solved some of my problems:)

Here is a link to what i am trying to replicate, I will only try to make the first part, where it makes the mountain:

I have made a script which makes the cubes(with a bevel and some other stuff on them) and i have one cluster for each object, controling the uper faces of the cube. I also have 3 locators, one for where the mountain starts, at top and end. I am not really sure on how to create this, as I want to try to make it look a bit random like it is in the link. I will drop a note if I find out something usefull.

I am not even sure if I can make it, but it will anyway be great practise.

Thanks for all again :)

03 March 2006, 10:54 AM
Wow, found out I have a question as well, or as allways.

If i create something like a deformer, how can i create it in a string, so that the shape node of it is selected in the string?

string $cluster[] = `newCluster " -envelope 1"`;
$cluster[0] = `rename $cluster[0] "clusterCube"`; //it renames the envelope part of the cluster

that line does not contain the shape, just the envelope :(


03 March 2006, 02:03 PM
Hi H3ro. You have to use the listRealtives Command. Something like:

string $cluster[] = `newCluster " -envelope 1"`;
$cluster[0] = `rename $cluster[0] "clusterCube"`;
string $shape = `listRelatives -shapes $cluster[0]`;
select -r $shape;

it will return the shape of the envelope, and select it...Hope it helps!!!


03 March 2006, 03:12 PM
Perfectly perfect :)

You two have saved my day twice this day:)

03 March 2006, 12:58 PM
Some more questions?

How can i make a variable witch do not change, even it it depends on a random/changing value?
animationStart = rand(0,10)
getAnimationStartFrame = quarry time at frame animationStart, and no not change as time/frames goes by

in the script tciny wrote there are a few things i do not understand.
Why the $object in the "global proc animateVerts( string $object )"?
That variable is not declared or used, so why is it there?

Why is the script not attached to any objects? What are the advantages of that?

Thanks again for you time

03 March 2006, 06:29 PM
about the $object: sorry, my bad :)
I actually wanted to substitute the "wurst" with $object to make the function more general purpose but then somehow forgot along the way ;)

I'm not sure if I understand your question tho:
If animationStart is a global variable then it wont change until you set it to something different... you mean you'd like to know what random value the variable will have at a given point in time? Because you cant tell unless you cache the results and then use that.
But if you just dont want the value of animationStart to change after you've randomized it once you could just check if the value is something other than 0 (which is will be initialized with).

btw: Thanks for the kind words lads! :)

I really liked the style of the video you posted. As I don't know any plug-in that gives you that cubed kinda look I decided to invest a little time and build one myself.
As I'm currently writing my diploma thesis my time is very limited, but here's what I came up with so far after about half an hour of work.
You just insert the node into any polygon mesh and it'll create this cubed version of it. Of course you can later bevel the mesh to make it look nicer... smooth it... whatever :)
Right now I'm changing the nearest neighbour search to a kd-tree which should greatly increase performance. And of course it'll also get a feature that not all cubes have to be in one size but there'll be some larger ones etc.

btw. the maya bevel is awfully slow on those meshes :)

03 March 2006, 02:07 PM
tciny, that looks really cool. I found out that the way i tryed to make mine did not really turn out the way i wanted. =\

Could you maybe share some ideas on how you made yours?

03 March 2006, 05:05 PM
It's a maya plug-in node.
I'm sorry I didn't have time yet to complete it and post it here, but I've been crazy busy writing my thesis.
However, when I find the time to complete it I'll post it here together with the source code.

CGTalk Moderation
03 March 2006, 05:05 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.