View Full Version : Creating objects
nottoshabi 11-19-2009, 09:22 AM I'm trying to create a tool that would create an object with the name of a selected object, then point and orient constraint the new object to the selected object. Also the new object needs to be grouped to its self twice before being constrained to the selected obj. It also needs to handle multiple selections. I have gotten this far but I'm having some troubles with the loop. This is what I have so far.
//Select obj to constraint to
string $objSel[] = `ls-sl`;
//Create null and constraint them to the selections
for ($i=0; $i<size($objSel); $i++)
{
//Empty selection
select -cl;
//Create null
string $nullCreate[] = `CreateEmptyGroup`;
print $nullCreate;
//Group the null to its self twice and name it as the selection in $objSel
group -n ("$objSel[$i]" + "01" + "_Grp") $nullCreate;
//Here I get an error saying that the name does not match.
//What name is it talking about?
group -n ("$objSel[$i]" + "02" + "_Grp") $nullCreate;
string $grpSel[] = `ls-sl`;
//Constraint new Grp to the existing
string $poConst[] =
`pointConstraint
-weight 1
$objSel $grpSel`;
string $orConst[] =
`orientConstraint
-weight 1
$objSel $grpSel`;
//Delete the constraints
delete $orConst $poConst;
}
|
|
sciLoop
11-19-2009, 12:39 PM
Don`t know what you really want to do, but this here eliminates the error:
//Select obj to constraint to
string $objSel[] = `ls-sl`;
//Create null and constraint them to the selections
for ($i=0; $i<size($objSel); $i++)
{
//Empty selection
select -cl;
//Create null
string $nullCreate = `group -empty`;
print $nullCreate;
//Group the null to its self twice and name it as the selection in $objSel
string $group1 = `group -n ( $objSel[$i] + "_01" + "_Grp" ) $nullCreate`;
string $group2 = `group -n ( $objSel[$i] + "_02" + "_Grp" ) $nullCreate`;
string $grpSel[] = `ls-sl`;
//Constraint new Grp to the existing
string $poConst[] =
`pointConstraint
-weight 1
$objSel[0] $grpSel[0]`;
string $orConst[] =
`orientConstraint
-weight 1
$objSel[0] $grpSel[0]`;
//Delete the constraints
delete $orConst $poConst;
}
nottoshabi
11-20-2009, 01:07 AM
I'm trying to create a transform that I can place any were I want. For example I need to have something I can connect a remote control to a characters hand. I make a null, group it twice and point and orient constraint to the hand joint. Grp1 takes the constraints, Grp2 is for avoiding gimbal and the null will rotate the actual control. And all things have to named correctly. The null will the name of the object I'm constraining it to. Then the groups fallow. I don't want to be renaming things all the time so the renaming has to be procedural.
First I was selecting the obects or objects to constraint the new grouped nulls. $objSel[] Then creating the null renaming it and point and orient constraint to the object or objects in the $objSel[] array. I hope that makes sence.
isoparmB
11-20-2009, 04:40 AM
{
//Select obj to constraint to
string $objSel[] = `ls-sl`;
string $item;
//Create null and constraint them to the selections
for ($item in $objSel) {
//Empty selection
select -cl;
//Create null
string $nullCreate = `group -empty`;
print $nullCreate;
//Group the null to its self twice and name it as the selection in $objSel
string $group1 = `group -n ( $item + "_01" + "_Grp" ) $nullCreate`;
string $group2 = `group -n ( $item + "_02" + "_Grp" ) $nullCreate`;
//Constraint new Grp to the existing
string $poConst[] = `pointConstraint -weight 1$item $group2`;
string $orConst[] = `orientConstraint -weight 1 $item $group2`;
//Delete the constraints
delete $orConst $poConst;
}
}
I would try naming the first group though.
nottoshabi
11-20-2009, 09:54 PM
Thanks IsoparmB. Why did you use, for each obj in selection? Instead of the one I had? I was using the counter to help with the naming. Like what I have here. This works perfectlly.
//Select obj to constraint to
string $objSel[] = `ls-sl`;
//Create null and constraint them to the selections
for ($i=0; $i<size($objSel); $i++)
{
//Empty selection
select -cl;
//Create null
string $nullCrt = `group -empty -n ($objSel[$i] + "_Grp")`;
//Group the null to its self twice and name it as the selection in $objSel
group -n ($objSel[$i] + "01" + "_Grp") $nullCrt;
group -n ($objSel[$i] + "02" + "_Grp") $nullCrt;
Here is the problem in order to constraint the 01_Grp to the selection in the array I have to put this group -n ($objSel[$i] + "01" + "_Grp") $nullCrt;
in an array so I can call it back. And everytime I do that I get an error, can not convert type string to string. You did it in your loop, how is it in mine I get an error?
norbertnacu
11-20-2009, 11:00 PM
Hello There,
Here's my basic rule for the for loop statement:
1. If you just wanna loop through each item in your array, then use the "for in loop" statement. It's easy, clean, and simple. (Keep It Simple)
// Syntax
// for( string in string array )
// {
// }
// Iterate through each node
string $selNodes[] = `ls -sl -typ "transform"`;
for( $selNode in $selNodes )
{
setAttr( $selNode + ".v" ) false;
}
2. If you want more control in your loop (manipulate variables, counters, increment, decrement, etc..), then use the "for loop" statement.
// Syntax
// for (init; condition; increment)
string $selNodes[] = `ls -sl -typ "transform"`;
for( $index = 0; $index < size( $selNodes ); $index++ )
{
if( $index >= 0 && $index <= 4 )
{
// Your Code Here
}
else
{
// Your Code Here
}
}
// ie..
string $selNodes[] = `ls -sl -typ "transform"`;
for( $index = 0; $index < size( $selNodes ); $index++ )
{
if( $index > 0 )
{
parent $selNodes[$index - 1] $selNodes[$index];
}
}
// ie..
// Multiple init vars
// condition
// Multiple counters
for( $x, $y, $z; $x < 10 ; $x++, $y += 1, $z += 2 )
{
// Do something here
}
If you're gonna do something with your counter variables ($index), then use the "for loop". If you just wanna quickly go through each item in your array, then use the simplified for loop version "for in loop".
Thank You,
Norbert Nacu
isoparmB
11-21-2009, 05:52 AM
string $group2 = `group -n ( $objSel[$i] + "_02" + "_Grp" ) $nullCreate`;
string $grpSel[] = `ls-sl`;
$group2 makes $grpSel redundant, as they are both referring to the same thing, so I used $group2 instead and ditched the $grpSel variable (plus $grpSel is an array, to refer to your group using that variable you have to use $grpSel[0], not a problem really but you already had $group2).
You can use either a for loop or a for in loop, but to use a for loop:
for($i = 0; $i <= (size($objSel) - 1); $i++) {
}
You have to subtract one from the size command to find the end statement for a for loop, as the size command returns a 1 based index result, and an array uses 0 based indexes.
CGTalk Moderation
11-21-2009, 05:52 AM
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-2013, Jelsoft Enterprises Ltd.