Attach Spline Points to Clones using Xpresso

Spline Points to Clones on Type

If you are looking to unleash more power in your Cinema 4D Mograph rigs, this tutorial from Tim Clapham demonstrates a technique which will open up a world of possibilities for your day to day, animation and rigging tasks. Tim takes you step by step through the process of building a setup which allows you to attach Spline Points to Clones using Xpresso, whilst still keeping the setup procedural. By using Xpresso (and one line of Python), you can access the points, edges or polygons of your individual cloned objects.


If you prefer to jump straight to the video click here

Link spline point to object

The tutorial is split into four logical sections. In part one, we start with a very basic Xpresso setup which uses the Point node and links a single vertex of a spline to the position of an object, (in this case an Audio Jack Plug). This section is to help you familiarise yourself with Xpresso if you are new to using the Cinema 4D Nodes, plus it gives you a gentle introduction to the basic foundation of this powerful technique.

Hierarchy of splines

In part two, Tim introduces the MoGraph Data node, along with the concept of iterating through a hierarchy of objects to keep your expressions efficient and flexible. The Data node allows you to access information such as the position, rotation and colour of your Mograph objects,(as well as lots of other useful data), and pass this to other elements within your scene. By using the Hierarchy node, you can iterate through a hierarchy of objects in your scene. In this example we link the vertex of each spline to the position of each Audio Jack, but this method can be expanded to work with many other types of objects and techniques.

Iteration time

The obvious limitation with this rig so far is that all of the splines are actual objects in our scene. This means we need to add or remove splines if we change the count on our Cloner. It would be much more flexible if the splines were also generated by a Cloner Object so we could keep the number generated in sync. If we create our splines with a Cloner, then this will result in a virtual hierarchy of objects and we could use the Data node to iterate through this. Unfortunately the Data node will not allow you to access the individual vertices of the generated splines, so we need an alternative solution for this.

This is where Python comes to the rescue, and trust me you don’t need to understand Python to use this method. It is a simple one liner that solves our problem and opens up a whole new world of fun for your rig.

import c4d
#Welcome to the world of Python

def main():
global Output1
Output1 = Input1.GetCache()

The only part we are really interested in is this line, the rest is already there when you add a Python node.

Output1 = Input1.GetCache()

NOTE: When you add the Python Node, it is important to delete the existing inputs and add a new input using the data type Link, if you do not then you will not be able to connect the object to it.

What this does is set the output port of our Python node to be the cache from our input port. This part of the code — Get.Cache() — will return the object from the previously built cache, it can also return a list of objects and if we plug a Cloner into the input, then that is exactly what will happen. We then have a virtual hierarchy of objects to iterate through. To put this into context a little, the cache of the object is something that normally happens under the hood. For example if we create an array object and then add a cube as a child, internally the cache that is generated will be a hierarchy of cubes, one for each generated, then each cube will have it’s own cache where it is a polygonal object. Different objects produce different caches, an Extrude object could return, the extruded geometry, the fillet and the caps, depending on the settings you have used. You can make a Generator object editable by pressing ‘C’, to see it’s cache, and by using this simple piece of code, you can access this cache whilst keeping your object procedural.

Iterate through the virtual hierarchy

Now we’ve got that part done, the rest is simple. Wire up the Python output into the hierarchy node and everything worked as it did in part two above. The big difference is that now you’re generating the splines with a Cloner and the Python script is extracting a virtual hierarchy for us to iterate through. Now you can easily create hundreds or thousands of objects and have them all connected with splines, and if you link the Count parameter for both Cloner in your Xpresso then you can drive it from one Cloner to easily keep the count in sync.

To link the other end of the splines to another Cloner is a fairly trivial task now. Simply duplicate the Object, Data & Point nodes, then set the point index to the last point on your spline (in this case index 3), link your second Cloner into the Object node and wire the index into the Data node. (Watch the video, it’s much easier to watch Tim do it than to explain it in writing.)

Connecting both ends

Now you’ve built this Xpresso, you can rework the setup to help with a variety of tasks where you need to access the components of generated objects, it doesn’t have to be a Mograph setup, just adapt the expression based on your requirements. In this particular usage we could take things a step further as there is a rather obvious problem with this setup.

The issue is that although we have linked the spline points to our Mograph objects, if we move or rotate the Cloner around, the spline does not smoothly exit the back of the Audio Jack. What we need to do to fix this is constrain the second vertex of the spline to a position which is inline with our objects position, but offset along the Z axis. This will ensure we get a nice smooth curve on our spline and should stop any problems with it intersecting the object too.

Adding to the position vector

We already have the position of our Cloned Audio Jacks, and as position is a vector, I originally thought the simplest solution would be to split that vector into it’s X,Y,Z components, add a value to the Z parameter, then convert back to a vector and hey presto, we have the answer. Now this does work, but the problem here is that when we add a value to the Z position value, it uses global space. This is fine if you are never going to rotate your clones, but probably you will want to. This part had me stumped for a while as my first attempt at solving this almost worked but as it turns out, I was on the wrong track.

After sifting through a lot of google searches I finally found someone who verbally explained how to solve this in code. It turns out the solution is to actually use the Matrix of the object and then multiply it with the local vector. In Xpresso we can access the Global Matrix of our Clones using the Data node, just as we access the position, rotation, etc. The Global Matrix is a collection of vectors from which we can obtain information on the objects position, orientation, plus other values such as the length of the direction vector. Luckily for us, there is a node specifically made for multiplying Matrices and Vectors, it has the handy name MatrixMulVector and all we have to do is input our Global Matrix, multiply it by our Vector with this node, the result is a Global Vector which we then input into our Point Position.

Using MatrixMulVector

Now we can adjust the value / axis of this vector to control the offset of our spline points, thus solving the problem. We now have our splines smoothly exiting our Audio Jacks. Of course this is a fairly specific solution to a problem, but along the way, we have explored many solutions which can be used in so many other ways. I guess the rest is up to you and your creativity. We would love to see what you get up to with this technique, so please post it to twitter / instagram / facebook etc and tag helloluxx and we will share your creations with our followers.

If you enjoyed this tutorial and would like to learn more about rigging, then take a look at learn Cinema 4D Rigging in One Day. Professional premium training which covers many rigging techniques, working with traditional rigging tools such as deformers, building your own custom rigs, working with xpresso and many more useful techniques that will vastly improve your productivity, allowing more time to be creative.

For more of our free training, please visit the tutorials section of the site here.

Here’s the video tutorial

Author: Tim

Leave a Reply