Work with Data in Cinema 4D

Different ways to import and work with CSV files

Example for the visualization of data in Cinema 4D.

The possibilities shown here are:

· Convert values to points
· Create values as keyframes
· Importing values via the XPresso Python Node
· Add values via a script
· Add values via the Python tag

The CSV File

Entity,Code,Year,Annual CO₂ emissions (tonnes )
Afghanistan,AFG,1949,14656
Afghanistan,AFG,1950,84272
Afghanistan,AFG,1951,91600
Afghanistan,AFG,1952,91600
Afghanistan,AFG,1953,106256
Afghanistan,AFG,1954,106256
Afghanistan,AFG,1955,153888
Afghanistan,AFG,1956,183200
Afghanistan,AFG,1957,293120

Convert values to points

Create an object first, meaningless wich one. This serves as our “source”.
After you have converted it, you can see all points in the Structure window. Delete all!
Under File click Import ASCII DATA, and select your CSV file. Now your object has the number of points your file has in columns, and they also have the position of the file’s values. Our “source” is ready.
To make the points “visible”, create a cloner, set the mode to Object and drag your “Source” into Object. Set the distribution to Vertex. Now each data point of the source has a cloned object in 3D space.
Since the values in the CSV file often do not have the dimension to see them well in the viewport, scale your “source” until you see the cloned objects in the viewport.
This is roughly the result.
The “source” in the Voronoi Fracture Object.

Create values as keyframes

Create an object, preferably a Null object.
Drag and Drop the object into the timeline.
In the Functions menu the CSV file can be added with the ASCII Animation Import.
For a CSV file, Comma must be set as the separator. If you want one keyframe per value, set the Time to Frame.
Hurray!
Now create a new track for the property you want to animate. For example the X position.
Now copy the data you want to animate with into the track.
For this example I combined the technique above and this technique. The animation was modified using the XPresso tag and the animated object was dragged into the MoGraph Tracer.

Importing values via the XPresso Python Node

With just a little bit of Python code, which even a layman can understand and use with copy paste, completely new possibilities open up. Any kind of object can be created, any parameter can be set and any function can be animated, all based on the data read in. Cinema 4D offers a number of ways to implement Python code. To animate and modify existing objects, the Python Node in XPresso is suitable.

Here you can see the Python node and the included code, which outputs different values from our CSV file.
This is what the Python node looks like when you create it. You can already see how it works. The global variable Output1 prints the sum of Input1 and Input2. This output can be used in XPresso.
In order to use the CSV file, we have to read it into the code. The code shown above is a good start.
Note that the Python internal CSV library has been imported. This is needed to process CSV files.
First enter the path to your CSV file.
Next, the file is read in code. We open the path with open() and create a variable (readed) that contains the CSV file.
Next, we create a “loop” that reads each cell in our CSV file (readed). In each cell the code tries to define the variables entity, year and emission with the respective value from the column. If it can’t (because the cell is empty, for example), it tells you the column in the console and moves on to the next one. This loop is executed once per frame, which in this case means that entity, year and emission always return the last value of the table.

As a reminder, this is what the CSV file looks like:

Entity,Code,Year,Annual CO₂ emissions (tonnes )
Afghanistan,AFG,1949,14656
Afghanistan,AFG,1950,84272
Afghanistan,AFG,1951,91600

Now we define the three outputs as global variables and tell them to output entity, year and emission.
The three outputs must be added to the node itself. Click on the red field for this. Take care to define the output correctly. The most important ones are Float for numbers, and String for text.
And already our Python Node gives us the last three results.
An example of an application. The Python node writes the values of the CSV file to a list which is then read out based on time and then dragged as text into MoGraph Text.

Adding values via a script

In the Cinema 4D Script Manager, Python scripts can be edited and executed. Unlike the XPresso Python Node, they are only executed once, but you can add objects and set keyframes, for example.

To edit scripts in Cinema 4D, it is worth creating your own layout.
The script in this example is suitable for drawing a spline based on values. This can then be visualized in a lathe object.
The associated code is not dissimilar to the example from the Python node. But there is also the function add_spline(), which draws the spline. In this example the spline for the year 2017 is drawn.
Here is the result from the example shown above.

Add values via the Python tag

The Python tag can run with the same code as in the Script Manager. However, the Python tag is executed on every “action” of Cinema 4D. That means as soon as the hierarchy of objects changes, a parameter moves or the next frame is calculated, Cinema 4D executes the script. This can be an advantage if you want to change the parameters of the objects in relation to each other. However, if you want to create objects, they are created anew each time. A workaround is to create the object only if it does not already exist. In addition you have to insert it in the same position.

This is how the add_spline() function from the example Adding values with a script in the Python tag would look.

Conclusion

The possibilities offered by Cinema 4D for importing data are limited as long as you want to work without code. With code, however, there are actually no more limits. But if this is the best way to visualize data depends on the code. If you are confident in using Cinema 4D, you will quickly achieve success. If you start from the very beginning, you should rather work with SideFX Houdini, Three.js etc. The code for the examples I have put here under GitHub. Also the code and data for the cover picture, which was created with the above mentioned techniques.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store