Cinema4D Spline from CSV file script
Recovered from the old blog (28/03/2013).
The following code is a script for Cinema4D that allows you to load a CSV file of points into a spline object so you can do stuff with it like using it as a camera rail or sweeping a profile over it. To choose the CSV file, a file dialog will pop up. It needs to be in the format
x0, y0, z0
x1, y1, z1
x2, y2, x2
...
i.e. with no comma at the end of the line.
var d;
splitIntoVector(s) {
var v = vector(0,0,0);
var p = strchr(s, ',', 0);
if(p!=-1) {
v.x = evaluate(strmid(s, 0, p));
s = strmid(s, p+1, 50);
p = strchr(s, ',', 0);
if(p!=-1) {
v.y = evaluate(strmid(s, 0, p));
s = strmid(s, p+1, 50);
v.z = evaluate(s);
}
}
println(tostring(v));
return v;
}
countLines(txt) {
var done = false;
var start = 0;
var lineCount = 0;
while(!done) {
var retIndex = strchr(txt, '\n', start);
if(retIndex==-1) {
done = true;
} else {
lineCount++;
}
start = retIndex+1;
}
return lineCount;
}
readFile() {
var name = GeGetStartupPath();
var res = name->FileSelect("Please select a file.", FALSE);
if(!res) {
return null;
}
var filename = name;
var file = new (BaseFile);
file->Open(filename, GE_READ);
var str = file->ReadString(file->GetLength());
return str;
}
loadData() {
var str = readFile();
var lineCount = countLines(str);
println(tostring(lineCount) + " points");
var points = new(array, lineCount);
var done = false;
var start = 0;
var i =0;
while(!done) {
var retIndex = strchr(str, '\n', start);
// println(retIndex);
if(retIndex==-1) {
done = true;
} else {
var s = strmid(str, start, retIndex - start);
var v = splitIntoVector(s);
points[i] = v;
//println(s);
}
start = retIndex+1;
i++;
}
return points;
}
main(doc, op) {
d = doc;
var myspline =AllocObject(Ospline);
var pointarray = loadData();
var pointcount = sizeof(pointarray);
myspline->SetPoints(pointarray);
var vc = new(VariableChanged);
vc->Init(0,pointcount);
myspline->Message(MSG_POINTS_CHANGED,vc);
var i = 0;
for(i = 0; i SetPoint(i,pointarray[i]);
}
// Place the second point here
myspline->SetName("MySpline");// Change it's name
doc->InsertObject(myspline,NULL,NULL);//Add it to the scene
}