PDA

View Full Version : my Tool writing sketchbook need crits!


Stellios
12-13-2010, 06:11 PM
ok so i have a zb sketchbook, a conceptart sketchbook, why not a tool writing sketchbook?

Im very new to this, and would love if i could get some real advice from the great professionals on this board. Ill be posting tools from time to time in hopes that I get better from your advice!


first up (baby steps) is a simple procedure that when an object is selected, creates, and aligns a null to the selected object, and then becomes the its parent. crits welcome :)

//store selection into a variable
global proc nullParent()
{
$selection = `ls -sl`;

//create empty group
$groupNode = `group -em`;

//parentconstrain the empty group node to the selected object and then delete it.
delete `parentConstraint $selection $groupNode`;

//parent the selection to the empty group node
parent $selection $groupNode;

}

thematt
12-13-2010, 08:33 PM
that's one seems good, although I would already add the name of the grp object in the procedure something like
string $Grpname =`rename $groupNode ($sel[0]+"_grp")`;

well you got the idea..
just a thought.

cheers

NaughtyNathan
12-13-2010, 09:28 PM
my 2 cents.. ALWAYS declare your variable types. This is MEL, not python! ;)

makes it easier to decipher the intent of the code and leads to less issues which may be hard to debug...

good idea though.. Warpcat had a similar thing going as a tiddlyWiki...

:nathaN

ewerybody
12-14-2010, 10:21 AM
here it is: mayamel.tiddlyspot.com (http://mayamel.tiddlyspot.com/). tiddly is not perfect but already a little better than this so web1.0-ish vBulletin crap here. "Purchase vBulletin, the most powerful forum software available." LOL!..But whatever.

Here we are now. Entertain us!! :D
I'm mean: @Stellios: Yea do that. I'll be pleased to read about something like that. Maybe I can give some cents as well ;]

Stellios
12-15-2010, 04:01 AM
Hey guys thanks for the advice! those tips impoved my code, just the reason i started this thread so again i thank you :bowdown:

Ok next up, this isnt really a usefull tool but im trying to learn for loops a bit. Right now im having an issue with a bit of code that delivers the error: The source attribute 'polySphere3.tz' cannot be found.

This is what im trying to do: create 10 cubes, 10 spheres, and connect their translate Z attributes. Im attepting this using the following code:

for ($i=0;$i<10;$i++)
{
string $sphere[] = `polySphere`;
string $cube[] = `polyCube`;

move ($i*2) 5 0 $sphere;
move ($i*2) 0 0 $cube;

connectAttr ($sphere + ".tz") ($cube + ".tz");
}

help! :)

NaughtyNathan
12-15-2010, 09:51 AM
$sphere and $cube are arrays [] and you cannot catenate (join) an array with a string:

{ "a","b","c" } + "text" = wtf??even if an array contains only one element it still cannot be catenated. you need to catenate individual elements:
connectAttr ($sphere[0] + ".tz") ($cube[0] + ".tz");:nathaN

ewerybody
12-15-2010, 01:11 PM
he micheal! For if you write more of that in Mel a little helper proc comes in handy:

global proc string getStringArrayItem0(string $array[])
{
return $array[0];
}

string $cube = getStringArrayItem0(`polyCube`);
print("$cube: \"" + $cube + "\"\n");
$cube: "pCube2"
so you don't need to care about those pesty arrays everytime..

And for performance and style (imho) its better to declare variables you overwrite inside a loop outside:

{
string $cube, $sphere;
int $count = 10;
for ($i=0; $i < $count; $i++)
{
$sphere = getStringArrayItem0(`polySphere`);
$cube = getStringArrayItem0(`polyCube`);

move ($i * 2) 5 0 $sphere;
move ($i * 2) 0 0 $cube;

connectAttr ($sphere + ".tz") ($cube + ".tz");
}
}

alas in py you could write stuff like that to get only the string item without a surrounding list:
import maya.cmds as mc
for i in range(10):
cube = mc.polyCube()[0]
sphere = mc.polySphere()[0]

mc.move(i * 2, 5, 0, sphere)
mc.move(i * 2, 0, 0, cube)

mc.connectAttr((sphere + '.tz'), (cube + ".tz"))

Stellios
12-15-2010, 01:15 PM
ok, thanks, i understand, so i changed it to this and got a new error:

// Error: The source attribute 'polySphere2.tz' cannot be found. //




for ($i=0;$i<10;$i++)
{
string $sphere[] = `polySphere`;
string $cube[] = `polyCube`;

move ($i*2) 5 0 $sphere;
move ($i*2) 0 0 $cube;

connectAttr ($sphere[$i] + ".tz") ($cube[$i] + ".tz");
}

why isnt the string name of the object getting stored in the the array variable?

ewerybody
12-15-2010, 01:25 PM
each loop $i you create arrays with transform and polyShapes in it. its always:
array[0]: transform
array[1]: shape

if the loop $i goes on, there is nothing at position 2 in the array. so: connectAttr ($sphere[$i] + ".tz") ($cube[$i] + ".tz"); // aka
connectAttr ($sphere[2] + ".tz") ($cube[2] + ".tz");will fail.
Its like naughty wrote:
connectAttr ($sphere[0] + ".tz") ($cube[0] + ".tz");

Stellios
12-15-2010, 01:52 PM
he micheal! For if you write more of that in Mel a little helper proc comes in handy:

global proc string getStringArrayItem0(string $array[])
{
return $array[0];
}

string $cube = getStringArrayItem0(`polyCube`);
print("$cube: \"" + $cube + "\"\n");
$cube: "pCube2"
so you don't need to care about those pesty arrays everytime..

And for performance and style (imho) its better to declare variables you overwrite inside a loop outside:

{
string $cube, $sphere;
int $count = 10;
for ($i=0; $i < $count; $i++)
{
$sphere = getStringArrayItem0(`polySphere`);
$cube = getStringArrayItem0(`polyCube`);

move ($i * 2) 5 0 $sphere;
move ($i * 2) 0 0 $cube;

connectAttr ($sphere + ".tz") ($cube + ".tz");
}
}

alas in py you could write stuff like that to get only the string item without a surrounding list:
import maya.cmds as mc
for i in range(10):
cube = mc.polyCube()[0]
sphere = mc.polySphere()[0]

mc.move(i * 2, 5, 0, sphere)
mc.move(i * 2, 0, 0, cube)

mc.connectAttr((sphere + '.tz'), (cube + ".tz"))

so just to understand, each time the loop runs, the variable isnt appended, its overwritten? in otherwords when the code is finished only 2 of the objects will be stored in a variable?

more questions to understand: when I put an object into an array, it throws the transform, and the shape node into separate indices? ok im a little confused... :S

also why encapsulate in a procedure? why are the arrays pesty? Sorry im so noob :(

ewerybody
12-15-2010, 02:11 PM
@variables in loops:// this creates an array inside the loop each time it runs:
for ($i=0;$i<10;$i++)
string $array[];

// this refills an outside created array each time the loop runs:
string $array[];
for ($i=0;$i<10;$i++)
$array = `ls -sl`;

// this refills an ouside created variable with the first item of an array:
string $myString;
for ($i=0;$i<10;$i++)
$myString = getStringArrayItem0(`ls -sl`);

@primitive creating commands:
yep. Thats what it does. Easy to reproduce:polyCube;
// Result: pCube42 polyCube42 // first transform, second shape. You don't care about the shape most of the time though... (I think I never did when doing so...) thats why these arrays are pesty you most of the time only need the first [0] element. You'd never go to [1] or another indey in these cases. Its just inconvenient to always have arrays in these cases.

pixelranger
12-18-2010, 10:43 PM
I've got a post about 'for loops' on my blog (http://andrehitsoy.tumblr.com/post/1464596489/01-meet-the-for-loop-my-best-friend) (as well as a lot of other, more advances examples). but yeah, the polySphere command will always return 2 strings and [0] is always the transform and [1] is always the shape.

if you for some reason would like to store each cube in an array and apply the connection AFTER the loop, declare the array beforehand:

// declare a couple of empty string arrays:
string $cubes[] = {};
string $spheres[] = {};

//create cubes and spheres
for ($i=0; $i<10; $i++){
string $sphere[] = `polySphere`;
string $cube[] = `polyCube`;

// strictly speaking, we only want to move the transform node...
move ($i*2) 5 0 $sphere[0];
move ($i*2) 0 0 $cube[0];

// add the newly created cube (transform node) to the $cubes array.
// if size($cubes) is 7, the last cube is $cube[6] and we'll add the
// new cube as $cube[7] (changing the size to 8)
$cubes[size($cubes)] = $cube[0];

// same with the spheres
$spheres[size($spheres)] = $sphere[0];
}

// and NOW we can go and iterate through the list of cubes
// (holding the names of 10 transform nodes)
for ($i=0; $i<size($cubes); $i++){
connectAttr ($spheres[$i] + ".tz") ($cubes[$i] + ".tz");
}

Stellios
12-23-2010, 04:45 PM
Great guys, this is awesome, im learning quite a bit.

I am having weird issues with a peice of code that is returning something i find to be unexpected.

whats the problem?

{
int $nums[] = { 4, 4, 4, 4, 6, 4 } ;
int $elem;


for( $elem in $nums )
{
print $nums[$elem];
}

}




this returns

666606



whaaat?

thematt
12-23-2010, 06:32 PM
just print $elem;

cheers

Stellios
12-29-2010, 05:28 PM
ok next up for your approval is a bit of code that makes an ik Spline stretchy. Right now it has some limitations: there is no error catching and the joints x translate has to be running down the bone lengths. Also nothing is getting renamed. but here she is! what do you think?

//Variable Catching: Curve, Curve info Node, Joint List,
string $crv[] = `ls -sl`;
string $crvInfoNode = `arclen -ch 1`;
float $crvLength = `getAttr ($crvInfoNode + ".arcLength")`;
string $ikHandle[] = `listConnections -type ikHandle ($crv[0] + ".worldSpace[0]")`;
string $joints[] = `ikHandle -q -jl $ikHandle`;
string $scaleFactor = `createNode multiplyDivide`;
//Getting the scale factor:Using a MultiplyDivideNode, divide the curves current length by its new length
connectAttr ($crvInfoNode + ".arcLength") ($scaleFactor + ".input1X");
setAttr ($scaleFactor + ".input2X") $crvLength;
setAttr ($scaleFactor +".operation") 2;
//Query the bone lengths, and then using a MultiplyDivideNode for each joint, multiply the scale factor by the bone lengths for each bone in the chain
for ($n in $joints)
{
float $jointLength = `getAttr ($n + ".tx")`;
string $div = `createNode multiplyDivide`;
connectAttr ($scaleFactor + ".outputX") ($div + ".input1X");
setAttr ($div + ".input2X") $jointLength;
connectAttr ($div + ".outputX")($n + ".tx");
}


I eventually want to set this up to a GUI. Have an option to scale the bones instead of translate. Have an option to keep the initial length of the chain, and as well, anything else you might think of! let me know :D

CGTalk Moderation
12-29-2010, 05:28 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.