Posts Tagged ‘Maya’

Maya: Simple 3D Array

Thursday, April 26th, 2012

For those of you starting out, here’s a simple array script to create instances of a primitive. You should be able to build onto this script very easily. Play with the numbers, and see how far you can push it. Soon, I’ll modify it so it arrays your selection. I’m working on a ring mail script, and this is a fundamental part of making ring mail.


//simple 3d array

polyCube;

//How many?
int $oCount = 3;

//How far apart are they?
float $oSpacing = 1.0;

f3DArray($oCount, $oSpacing);

proc f3DArray (int $objCount, float $oSp){

int $n = 0;

//1D, X
for($i = 0; $i < $objCount; $i++){

//2D, Y
for($j = 0; $j < $objCount; $j++){

//3D, Z
for($k = 0; $k < $objCount; $k++){

instance;

vector $v = << ($oSp * $i), ($oSp * $j), ($oSp * $k) >>;

xform -ws -t ($v.x) ($v.y) ($v.z);

$n++;

}
}
}
}

Maya: Neutral Pose

Wednesday, April 25th, 2012

Use this script to create a neutral pose, sort of. For example, say you had a model robot, and you have already set the pivot point for each part. This will take the pivot, and create a group at the pivot location. The group is translation 0,0,0 rotation 0,0,0 and scale 1,1,1. Effectively, a neutral pose.


string $oSelection[] = `ls -sl`;
int $oCount = size($oSelection);

for ($i = 0; $i < $oCount; $i++){
select -r $oSelection[$i];

//get the pivot location of the selection
vector $v1 = `xform -q -t`;
vector $v2 = `xform -q -sp`;
vector $v3 = $v1 + $v2;

print ($v3);
//make a group and move it to the pivot location
group; xform -os -piv ($v3.x) ($v3.y) ($v3.z);
}

Maya: Path Constraint v0.2

Thursday, April 19th, 2012

This script is developed from my former multi-motion path script. It’s more developed, and is simpler for the user. To use it, select all the objects you’d like to path constrain, and then select the curve last. Run the tool. The order of your selection is the order along the path. Each object will be grouped, and the group will be path constrained using a standard motion path. The user’s objects can be transformed, while maintaining it’s connection to the path via the parent group. It’s simple rigging principles. :-) You can then edit the curve, and each object will maintain their percentage along the path.

Like most of my Maya tools, this tool is inspired by my usage of Softimage, and my desire to solve problems in a similar manner in both programs. For now, this tool is free to the public, so grab it while you can. When I’ve developed it far enough, I’ll sell it for a very nominal fee.

/*---------------------
Path Constraint
version 0.2
Matthew L. Stoehr
formandspace.com

Objective:
This script will attach objects to a curve.

Instructions:
Simply select the objects, then select the curve last.

Notes:
Selection order is the order of objects on the path, from 0 to 1 of the path U value.
Works best if the curve is drawn from the top view, but it's not required.

--------------------*/

string $mySelection[] = `ls -selection`;
int $size = size($mySelection);
int $objCount = $size - 1;

/*----------------- Find the curve ---------------------*/
//if the node is a curve, don't group it, and use it for the namespace
//last in selection
string $path = $mySelection[$objCount];
//make the offset controller
spaceLocator -absolute -position 0 0 0 -name ($path + "_offset_00");
//collect all offset controllers, locator transforms, and find the most recently created locator
string $myOffsets[] = `ls -tr ($path + "_offset_*")`;
int $offsetsCount = size($myOffsets);
string $recentOffset = $myOffsets[$offsetsCount-1];

/*---------------------- Make a group per object -----------------------*/
//psuedo: make a group for each selection, so it has the motion path, not the artist's objects.
for ($i = 0; $i < $objCount; $i++){
select -r ($mySelection[$i]);

//finds the pivot point of the selection, via query (-q)
vector $v1 = `xform -q -t`;
vector $v2 = `xform -q -sp`;
vector $v3 = $v1 + $v2;

group -n ($path + "_motion_grp");

//set the group's pivot to the object's pivot location
xform -os -piv ($v3.x) ($v3.y) ($v3.z);
//use the curve name for the namespace

}
//collect the new groups
string $myMotionGroups[] = `ls ($path + "_motion_grp*")`;

/*---------------------- Path Constraint -----------------------*/
float $offset = 0.0;
float $spacing = 1.0;
//objCount - 1 so the objects distribute along the entire curve.
float $divisions = $spacing/($objCount-1);

for($i=0; $i < $objCount; $i++){

float $uLinearDistribution = $divisions * ($i+$offset);
string $motion = `pathAnimation -f 1 -fm 1 -etu 0 -c $path $myMotionGroups[$i]`;

string $source = $motion + "_uValue.output";
string $destination = $motion + ".uValue";
disconnectAttr $source $destination;

setAttr ($motion + ".uValue") $uLinearDistribution;

expression -s ($motion + ".uValue = "+ $uLinearDistribution +" + "+ $recentOffset +".translateX * 0.1") -o $motion;
//expression -s ($motion + ".uValue = "+ $spacing +"/(" + $objCount + "*("+ ($i+1) +" + "+ $path + ".offset));") -o $motion;

}

/*----------------------- Group All to Organize the Outliner----------------*/
for ($i = 0; $i < $objCount; $i++){
select -add ($myMotionGroups[$i]);
//rename the motion groups to allow more objects to attach to the same path
rename $myMotionGroups[$i] "_motion_grp_";
}
group -n ($path + "_grp_00");

select -r $path;

Inherited Transformation

Thursday, November 17th, 2011

Here’s a simple script for a long chain of parented objects.
Create a bunch of cubes in one place, select then all in the viewport, and run this script. Marquee select them all again, and transform the objects to see the effect. This takes advantage of inherited transformation.


string $null[] = `ls -tr -sl`;

int $n = size($null);

for($i = 0; $i < $n; $i++){
if($i != $n-1){
parent $null[$i] $null[$i+1];
}
}

Scripting

Tuesday, November 15th, 2011

Yesterday I was asked by one of my students, Andrew, where I learned scripting and what sources I’d recommend for learning scripting. To the first question, I learned on my own, with no formal training. I bought books, I read websites on the topic, and I read the SDKs of the software, over and over again. I gradually became more familiar with the syntax, the logic, and my own patience. In 2002, I started with Actionscript, AS, for Flash, which is based on a type of J-script, and from there I graduated to Softimage XSI. XSI had large libriaries of examples in the SDK, software developers kit, written in J-script, and I was able to read and write from those examples based on my experience with Actionscript 1.0. In 2008, I was back into Actionscript for a short while, on the social networking website prototype, at that point it was now AS 3.0. A fully object oriented programming language, or OOP, and I took to that right away. It took me several years of work in XSI and AS to understand OOP. I’ve also attempted to learn Python, though I haven’t committed the time and resources to use it well enough.

It takes me a long time to write code of any kind, and I require 50-60% guidance, the rest I dream up on my own. Whenever the software couldn’t solve a problem, or when I became very curious on how something works in a software, I’d take the time to write it myself. Sometimes, I’ll automate a task, just to see it in code, and from there it encourages me to take on something a little bigger. Changing software, like the switch from Lightwave to Softimage XSI back in 2005, motivates me to make tools to ease my transition from one software to software. For example, I’m quite happy with my abiility to model with curves in Maya, and I’d like that ability in Softimage (curve modeling being one of SI’s weaknesses).

As for the second question, here is a list of books in my library that have helped me over the years. I recommend all of these authors.

Any book on the subject of J-script or programming syntax and logic. Each language has it’s own particulars, but all languages share similar rules. Learning one language makes learning a second far easier.

Python
John Zelle (Personlly, if I were to start all over again, I’d start here. Not because it’s pythin, but because this guy is a great teacher).

Actionscript
Keith Peters
Joshua Davis (I’m not sure if he has published anything recently).

MEL (Maya Embedded Language)
David Gould

There are many other good references, but these authors stand out in my mind. You can find more information about these authors and other great people by sifting through my links to the right.

I have to mention that programming has led me to love math. More specifically, programming has allowed me to see math in action, and I understand it better and apply it better as a result of learning programming.