My first post here so I really hope you tolerate my mistakes if I make any.
I'm working on a path generation tool (for Windows and macOS) that creates paths with x and y coordinates of each points. I can easily covert them to SVG (using 'polyline' as it's more convenient than 'path' for my case). As far as I know, C4D doesn't support direct SVG import; it either needs to be *.dwg or *.dxf. Instead of expecting users to import the generated file into their scenes, I'd like to write a script that would do that with one click. Here are my questions:
How can I get those values from a *.txt file to create a spline in C4D? I can change the format of the values if needed. I just need to know the workaround (or a piece of script) to do that. Any help would be much appreciated.
thank you for reaching out us. Also a big thank you @Cairyn and @x_nerve for the provided community support. I'll answer in bullet points:
I hope this helps,
Your test data as a txt file to load in with the script below: points.txt
""" Basic Python file IO to create a Cinema 4D linear spline from a CSV
style file as discussed in:
Run this file in Cinema 4D's script manager (Shift+F11). It will open a file
dialog, where you should locate the provided example file 'points.txt',
which ten will be loaded into the document as a linear spline.
!THIS IS AN EXAMPLE AND NOT PRODCUTION READY CODE!
"""Reads one of your CSV style data structures into a single spline.
# The points of the spline we are going to build.
points = 
# Open a file object in read only text mode and read it line by line.
with open(file_path, "rt") as file:
for line in file.read().splitlines():
# Parse the line in a comma separated value style.
components = line.split(",")
if len(components) != 2:
raise RuntimeError("Parsing Error")
# Cast the components to floats. We need these gymnastics, because
# Python's string type cannot implicitly be cast into a float and
# Cinema does expect that type when instantiating a vector.
components = [float(decimal.Decimal(c)) for c in components]
# Create the vector and add it to our points list.
p = c4d.Vector(*components, 0)
# Create a spline object with our points.
spline = c4d.SplineObject(pcnt=len(points), type=c4d.SPLINETYPE_LINEAR)
# Insert it into the active document.
# Tell Cinema to update.
# Open a file dialog.
file_path = c4d.storage.LoadDialog(type=c4d.FILESELECTTYPE_ANYTHING,
title="Select a file.",
# Read in the data as a single spline and insert into the scene.
if __name__ == "__main__":
Python can easily read lines from a file, split the line into partial strings, and convert the strings into float values.
But for creating a spline from that, you will need to have a look at the class SplineObject and its parent PointObject:
C++ would require the huge project overhead so I can't really recommend this for such a simple script.
I think I'm lost. I feel stuck. Is there way to get this done by paying somebody? I'm sorry, I know that's not the right place to list a job but python coders for C4D is extremely rare.
I think it might be faster to try to import the point location directly as an external library (.py). At the same time, C4D supports importing Illustrator files, and SVG can be processed in Illustrator before importing C4D. Here is a simple SplineObject code.
#The location of the point.
Points = [c4d.Vector(0, 0, 0),c4d.Vector(0, 0, 2),c4d.Vector(0, 4, 4)]
#Initializes the spline object.
Spline = c4d.SplineObject(len(Points), c4d.SPLINETYPE_LINEAR
#Change the number of spline object segments, the number of points.
#Set the locations of all the points of the spline object.
#Set properties of the segment.
Spline.SetSegment(0, len(Points), True)
#Close the spline object.
Spline[c4d.SPLINEOBJECT_CLOSED] = 1
#Inserts the spline object into the current document.
#Update current document, routine code.
without further feedback, we will consider this thread as solved by Wednesday and flag it accordingly.