View Full Version : create curve from given coordinates

02 February 2011, 11:11 PM

In my scene i have 244 meshes randomly placed along the grid. I was looking for a script that could take the "centre" from each one of the meshes and then create a curve that would link them. After some search i found the script that can calculate the "centre" of the meshes but i couldnt apply it to all of them at once. I had to choose one of them at a time and the apply the script. Because of that i began to write a new script that would work automatically for my scene. So far so good. Then i tried to change the "centre" point to a curve point so as to create a curve. But when i run the script every "centre" becomes a one-point curve which i can not manipulate. I would appreciate it very much if someone could help me. Below is the script i used:

for($j=1; $j<245; $j++){
$blob = "test6_blogram_" + $j;
select -r $blob;
string $poly[] = `ls -sl`;

if($poly[0] != "")


string $verts[] = `polyInfo -fv $poly[0]`;
string $vert[] = stringToStringArray($verts[0],
"FACE :");
string $obj[] = `ls -sl -objectsOnly`;

int $numVerts = (`size ($vert)`-2);

float $px=0;
float $py=0;
float $pz=0;

for ($i=1; $i< ($numVerts+1); $i++)
string $vertID = ($obj[0]+".vtx["+$vert[$i]+"]");
float $pos[] = `xform -q -ws -t $vertID`;
$px += $pos[0];
$py += $pos[1];
$pz += $pos[2];

vector $center = <<($px/$numVerts), ($py/$numVerts),

print ("center point of "+$poly[0]+ " is: "+ $center);

xform -os -t ($center.x) ($center.y) ($center.z)
string $initialCurve = (`curve -p ($px/$numVerts) ($py/$numVerts) ($pz/$numVerts)`) ;
rename $initialCurve ("curve_pt_" + $j);

thanks in advance:)

02 February 2011, 04:50 PM
I admit I'm a little confused by your code, so assuming that you just want a curve with a CV in each object's center (skip the rest is I'm wrong) here is what I would do:

1 Collect all objects' centers and store them in vector array:
// Init the array:
vector $cvPos[] = {};
int $i = 0;
for ($obj in `ls -sl`) {
float $objC[] = `getAttr ($obj + ".center")`;
$cvPos[$i] = <<$objC[0], $objC[1], $objC[2]>>;

Then create a string with the command for creating the curve and a desired degree. This one is linear:

string $curveStr = "curve -degree 1";
for ($v in $cvPos) $curveStr += (" -p " + $v.x + " " + $v.y + " " + $v.z);
eval $curveStr;

Hope that helps :)

02 February 2011, 04:58 PM
just tried it and it works like a beauty :applause:
thank you very much azrail :cool:

CGTalk Moderation
02 February 2011, 04:58 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.