PDA

View Full Version : Another script question: object position


Jorge Arango
01-15-2010, 12:40 PM
Note: to execute this script you must have a polygon object selected with at least one edge selected:



CallCommand(13957); // Clear console contents---------------
println("Console cleared");//Control------------------------
println("----------");
var nul = new(NullObject);
var active = doc->GetActiveObject();
if(!object)
{
println("Select a Polygon Object and, in edge mode, select at least one edge.");
return;
}

if(! (op->GetType()==OBJECT_POLYGON))
{
println("Must be a Polygon Object");
return;
}

var tol = .1; //Tolerance
var position = 6; //Position of cut
var prcntPos = .2; //Position in ATS tag
var posNul = nul->GetPosition();//Position of null object
CallCommand(1009671); //Edge to Spline
nul->InsertUnder(active);
var nul2 = op->GetDown();//Null object
var splineRef = nul2->GetNext();//Reference spline for ATS tag
var ats = AllocTag(Taligntospline);//Assign the ats tag to variable named ats
ats#ALIGNTOSPLINETAG_LINK = splineRef;
println("Just for control:");//Control--------------------------
println(splineRef->GetName()); //Control------------------------
println("----------");//Control---------------------------------
nul2->InsertTag(ats);//Add the ats tag to currently selected object

ats#ALIGNTOSPLINETAG_POSITION=prcntPos;//Percent position in ATS tag

------------------------------------------------------------------
//Working fine to here-----------------------------------------
------------------------------------------------------------------

var posNul2 = nul2->GetPosition();//Position of null object
println("With method A, 'GetPosition()':");
println("Position in Y=", posNul2.y);
println("----------");//Control---------------------------------
active->DelBit(BIT_ACTIVE);
nul2->SetBit(BIT_ACTIVE);//To make sure that the var nul2 really corresponds to the null object

var obj = doc->FindObject("Null Object");
var matrix = obj->GetMg(); // Get the global matrix
var globalPos = matrix->GetV0(); // Get the position from the matrix
println("With method B, 'GetMg()' and 'GetV0()':");
println("Global position = ", globalPos);


return;



This script does the following:

1. Executes the edge to spline command
2. Creates a null object as first child of the polygon object
3. Applies an align to spline tag, linked to the spline created with the edge to spline command to the null object
4. Sets the position in the ATS tag to 20%
5. Prints the position of the null object in the console twice. First using the GetPosition command and then it uses the GetMg() and the GetV0() commands.
6.It also does some minor things used to monitor the script like printing some variables and selecting some objects and unselecting others. It also creates some variables that I intend to use further on.

However, the position printed in the console does not correspond to the real position of the null object.

Anybody knows what I'm doing wrong?

Scott Ayers
01-15-2010, 07:30 PM
Hi Jorge,

If you use the bottom block of your code in a separate script. It will get the Null's position coordinates properly.
So the problem appears to be that the reference link on the ATS tag is taking over the position on the Null Object. And isn't passing the information back to the script.

You've got several attributes being set before the tag is even created. Making it a little hard to troubleshoot. So I would first simplify it first, and only focus on the core problem of the tag not letting go of the Null's position.

Something like this:
myfunction(doc,obj) // A brand new custom function (forward declaration)
{
obj=doc->FindObject("Null Object");
obj->GetMg()->GetV0();
println(obj->GetMg()->GetV0());// print the Null's actual location
}

main(doc,op)
{
var active = doc->GetActiveObject();
CallCommand(1009671); //Edge to Spline
var nul = new(NullObject);
nul->InsertUnder(active);
active->DelBit(BIT_ACTIVE);
nul->SetBit(BIT_ACTIVE);

var ats = AllocTag(Taligntospline);
nul->InsertTag(ats);
var splineRef = nul->GetNext();
ats#ALIGNTOSPLINETAG_LINK = splineRef;

var a = doc->FindObject("Null Object");
myfunction(doc,a);

}

Notice that I used a forward declaration in my example. I did that for two reasons:
1- It's more organized
2- It's sometimes necessary, and the only way to make a script work. Because it allows you to execute a function outside of the running main() block of code.
I've updated the Coffee Bible with information about that.

I was hoping by doing that I could solve your problem.
But the ATS tag (I think) still isn't allowing the Null's actual location to be accessed for some weird reason. Even when it's called from outside of the main(){} block.
I'm still rather new to coffee. So I'm probably missing something really simple and obvious.


-ScottA

Jorge Arango
01-16-2010, 11:49 AM
Thanks Scott,

Even though the problem is not solved, I learned a lot from your version of the script.

I'll wait to see if someone gives us a solution otherwise I'll ask somewhere else.

Jorge Arango
01-16-2010, 12:53 PM
Because it allows you to execute a function outside of the running main() block of code.


I read somewhere (maybe in your COFFEE Bible) that even though the script manager doesn't display the 'main(doc, op)' and the brackets, they are still there. That they are hidden for cosmetic reasons. So, are you sure that the forward declaration in your script is really running outside of the main function?

Scott Ayers
01-16-2010, 02:23 PM
Yeah it's working.
Otherwise it wouldn't compile and run without errors.

Even though the main block is built into the script manager and hidden. You can still override that formatting by typing out the main(){} block by hand.
If you couldn't do that, you couldn't use forward declarations. And that would be very bad. Because without them you are severely limited in what you can do. And Maxon knew that when they created the script manager.

tcastudios
01-16-2010, 02:55 PM
Hi Jorge.
I just read your description (what should happen).
See if this makes sense to you :)

(And yes, you can use functions before main(op,doc) in a script.)


if(!op || !op->IsInstanceOf(Opolygon) || !IsCommandChecked(16351))
{
println("Select Polygon Object and use Edge Tool!");
TextDialog("Select Polygon Object and use Edge Tool!", DLG_OK);
return; // No object/polyobject/edgetoolselected
}

doc->StartUndo(); // Start our undo

var n1 = AllocObject(Onull); // Create a Null
doc->AddUndo(UNDO_OBJECT_NEW,n1); // Undo inserting it
n1->InsertUnder(op); // Place it under op

var bc = new(BaseContainer); // the temp bc fpr EdgeToSpline
SendModelingCommand(MCOMMAND_EDGE_TO_SPLINE,doc,op,bc,MODIFY_ALL); //EdgeToSpline incl Undo

if(!op->GetDown()->IsInstanceOf(Ospline)) // Check if spline was created
{
println("No edge is selected!");
TextDialog("No Edge is selected!", DLG_OK);
n1->Remove();
return;
}

var spline = op->GetDown(); // Locate our new spline
n1 = spline->GetNext(); // locate our Null

var ats = AllocTag(Taligntospline);// Create AlignToSplineTag
n1->InsertTag(ats); // Add it to the Null

ats#ALIGNTOSPLINETAG_LINK = spline; // Place our spline in ATS
ats#ALIGNTOSPLINETAG_POSITION = 0.2;// Set position to 20%

DrawViews(DA_STATICBREAK); // Refresh our scene

var localpos = n1->GetPosition(); // Get the local position of Null
var globalpos = n1->GetMg()->GetV0();// Get Global position of Null

println("LocPos = ",localpos);
println("GlobPos = ",globalpos);

doc->EndUndo(); // End our undo


Cheers
Lennart

Jorge Arango
01-16-2010, 03:36 PM
I see that you use a different approach and that's good because these snippets are my main source of learning after reading all the documentation that I've found. Also your code is very neat and above all, it works.

I also see that the key is in the "DrawViews(DA_STATICBREAK)" command. If I remove it from your code it does't show the real position of the null. And if I insert it in mine, it also works. Weird, since it's supposed to affect editor view, but that's a very useful lesson!


Thank you Scott and Lennart.

tcastudios
01-16-2010, 03:53 PM
That's true, I spent the first six months or so asking questions
here gathering "snippets". Since I'm not a pro programmer it wasn't
until then I could read and understand most stuff in the COFFEE SDK 9.5..

Cheers
Lennart

Scott Ayers
01-16-2010, 04:16 PM
Thanks for the information about DrawViews Lennart.
I'll add it to my list of things to add to the coffee bible.

I only wish I could sit down with someone who knows how to use all of the obscure SDK functions for one day and write them all down. So I could document them in plain english. Rather than accidentally stumbling upon them one by one in the forums.

-ScottA

CGTalk Moderation
01-16-2010, 04:16 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.