PDA

View Full Version : How hard this is in Coffe? (if posible)


indeep
04-01-2010, 08:49 AM
I "just" want to create a spline of 2 points. Bezier spline... something similar to this:
http://img704.imageshack.us/img704/4140/bezier.jpg

Scott Ayers
04-01-2010, 08:05 PM
I think there is a way to create and edit spline parameters directly with coffee. But I can't seem to figure it out based on the rather cryptic SDK.

Here is a rather unconventional way to do it though: // This script creates a two point spline object

CallCommand(5182); //Create an Arc primitive
object()#SPLINEOBJECT_INTERPOLATION=0;//Make it a straight line later--It will be curved if this is skipped
CallCommand(12236); // Make Editable
var s = doc->FindObject("Spline");
var gp = s->GetPoints();//Collect all the points
var PointsCollected;//Used as an argument later in the script
var bs = new(BaseSelect);//Used later to hold the points collected

for (PointsCollected=0; PointsCollected<sizeof(gp); PointsCollected++)
{
gp[PointsCollected].x = 0;
if(gp[PointsCollected].y > 0 && gp[PointsCollected].y < 200) bs->Select(PointsCollected);
}
s->SetPoints(gp);// Moves the collected points to zero
s->SetPointSelection(bs);// Makes the collected points selected
SendModelingCommand(MCOMMAND_DELETE,doc,s,new(BaseContainer), MODIFY_POINTSELECTION);// Deletes the selected points


Hopefully this will be good enough to get you want you wanted.
I'm hoping someone who knows how to create splines directly will chime in here. Because I'm pretty sure there's a much more elegant way to do this.

-ScottA

indeep
04-01-2010, 11:38 PM
Thanks ScottA!

tcastudios
04-02-2010, 01:28 AM
I'll chime in if you stop dissing the COFFEE SDK, ok ? :)
All things in the world can always be "better" but the Coffee SDK 9.5
is very complete and covers, I'd say, 98 percent of most questions
put here on the forum (About scripting).
Programming can never be paint by numbers,
it's a pain striking trip of research and testing snippets of code ideas.
Thanks to the introduction of the Script Manager testing snippets is
very convenient. You can also quickly save your blocks of successful
tests over time with it.
That said, I also hit the wall all the time, not being a pro educated coder.
So searching and testing is the only way to go, short of hiring someone
and that in the long run and by my experience as always a good idea.
Not only to get things done but also to learn faster.

And -that- said, I've never done a bezier spline (setting tangents) so here was
my go at it and what I did.
First was to check what the tangents in Cinema are.
The SDK (Under "Splines") let me know they are set by an array. How should this array look then?.
Made a Bezier spline with two points and looked in the StructureManager,
moved around the "handles" and observed the StructureManager.
Ok, how "big" should this array be?
Selected the bezier spline object and, again from the SDK ,
found "GetTangents()" and under "Array" found "sizeof(arr)".

Test in ScriptManager:

var tarr = op->GetTangents():
var tsize = sizeof(tarr);


The answer was 4. Ok, that must be two values per point, one for each tangent handle?
Locations in space are expressed as vectors (x,y,z) so it looked as if each slot in the array
should hold a vector value.
From the test looking in the Structure Manager, the tangent handles were set locally relative
to their points.

Test that in ScriptManager with simple vector values easy to read:

var tarr = new(array,4);
tarr[0] = vector(100,0,0);
tarr[1] = vector(-100,0,0);
tarr[2] = vector(100,0,0);
tarr[3] = vector(-100,0,0);
op->SetTangents(tarr);


Success! The tangents are pointing in the X and -X direction.

Ok. I guess I need the pair of tangents to be "equal opposite".

Test in ScriptManager:

var tarr = new(array,4);
tarr[0] = vector(100,0,0);
tarr[1] = -tarr[0]; // <- negate the previous one for the same (first) point
tarr[2] = vector(100,0,0);
tarr[3] = -tarr[2]; // <- negate the previous one for the same (second) point
op->SetTangents(tarr);


Yup, fine. I got the tangents under control.

All dressed up and nowhere to go! Have tangents, need a spline to use them on.
(While I have a function since before to make a spline here's a routine anyway).

Ok, what is a spline?
It's a point object. You first need to create the spline object then fill it with the amount
of points you want and where you want them. The points are an array in the spline object.
You need to check out the "VariableChanged" object to make it work.
(Additionally check the "BackupTags" for editing splines, changing point amounts)

Resulting Test that actually make a spline (bezier with two points).

var myspline = AllocObject(Ospline); // the empty object
var pointcount = 2;
var pointarray = new(array,pointcount);
myspline->SetPoints(pointarray);
var vc = new(VariableChanged); // Important
vc->Init(0,pointcount); // Important
myspline->Message(MSG_POINTS_CHANGED,vc); // Important

myspline->SetPoint(0,vector(0,0,-100));
myspline->SetPoint(1,vector(0,0,100));
myspline->Message(MSG_UPDATE); // Important
myspline#SPLINEOBJECT_TYPE = 4; // Bezier type
myspline->SetName("MySpline");
doc->InsertObject(myspline,NULL,NULL);


There are the snippets to make a 2 point bezier spline with set tangents :)
I hope it can nudge someone in the right direction.
Additional info, to set a points or tangents position from an object, check
"GetMulP()" to get the global object position into the local position
of the spline.

Cheers
Lennart

Scott Ayers
04-02-2010, 02:52 AM
Thanks Lennart.

Coffee is a lot easier to learn when it's written that way.
I've been hoping that you will someday create an entry like this for the BaseTime() function on your wiki.

-ScottA

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