Work with Data in Cinema 4D

Example for the visualization of data in Cinema 4D.

If you want to visualize data in 3D space, Maxon’s Cinema 4D is more of the immaculate 3D tool. SideFX Houdini was developed for procedural visualization, and is accordingly powerful when dealing with large data sets. And if you want to experience the content interactively in your browser, you can use the JavaScript library Three.js.
Nevertheless, the tool Cinema 4D, which is focused on design and motion graphics, offers some means to import and process data. Sometimes even without any programming knowledge at all. The result is a whole range of visualization possibilities, from classical infographics to experimental. In the following I will show the different techniques for importing and processing CSV files.

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

Above you can see the first ten lines of a CSV (Comma-separated values) file (this was also used for this article). A CSV file usually has a header, the first line of the file. It is the column caption, and each row that follows lists the values. These are, as the name says, separated by commas. To process data in Cinema 4D as well as in any other file, a data set in this or a similar form is necessary.

Convert values to points

The easiest way to insert a CSV file. To do this, you load your data as dots in Cinema 4D. The weak point is here: That there are dots in 3D space, there are only three numbers per line. Text is not accepted.

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.

This result is static and relatively boring so far. But now the MoGraph tools of Cinema 4D can be used to further modify or animate the object. One possibility is to drag the created source into the Point Source of the MoGraph object Voronoi Fracture and “break it open” with the Push Apart Effector:

The “source” in the Voronoi Fracture Object.

Create values as keyframes

It is also possible to load the values of the CSV file as keyframes in Cinema 4D. This allows objects to be animated based on data. This is actually for importing MoCap data, but you can import any other data.

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.

Your null object now moves according to the Year column in the CSV file. To further develop this, you can use the XPresso Tag or the MoGraph Tracer:

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.

How to add objects with Python is well explained here. For the parameters you can now add the values from the CSV file.

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.

The commands “InsertUnder” (insert object under a specific other) or “InstertAfter” (insert object after a specific other) are listed here.

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.

If you liked this article you can follow me on instagram at: https://www.instagram.com/la.benzzz/

--

--

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
Gorm Labenz

Gorm Labenz

Designing designs that design designs