Mosplines - Iterate Width

  • Hi Andreas,

    really appreciate the help. I understand the formula, but Do I need to write one by one each segment?

    I am not sure about how to concatenate the formula string via Xpresso.

    I am trying to share my c4d scene but it seem that uploads are disable.


  • Hi Javier,

    sorry, it took so long, busy times here.

    I can't provide you with a final polished solution, that's out of our scope.
    But the following may get you at least started.

    In general with Xpresso you can simply add (Math node in mode "Add" and data type "String") string in order to concatenate them.

    With that said, I will ignore the Xpresso solution and instead provide a rough sketch with Python node.

    Here's the Xpresso setup:

    And the code of the Python node:

    import c4d
    # width per spline segment (actually multiplied with MoSpline's Width parameter)
    g_arrWidth = [1.0, 2.0, 3.0, 4.0]
    def BuildFormula(idx, numSegments, defaultWidth):
        width = defaultWidth
        if idx < len(g_arrWidth):
            width = g_arrWidth[idx]
        if numSegments <= 0:
            return str(width)
        if idx == numSegments - 1:
            return str(width)
        formula = "if (segment == " + str(idx) + "; " + str(width) + "; "
        formula += BuildFormula(idx+1, numSegments, defaultWidth)
        formula += ")"
        return formula
    def main():
        global MoSplineFomula
        if not Spline.CheckType(c4d.Ospline):
            MoSplineFomula = str(DefaultWidth)
        numSegments = Spline.GetSegmentCount()
        if len(g_arrWidth) != numSegments:
            print "Length of radius array does not match segment count"
        MoSplineFomula = BuildFormula(0, numSegments, DefaultWidth)
        # print MoSplineFomula

    Port types of Python node:
    Spline - Link
    DefaultWidth - Float
    MoSplineFormula - String

    Regarding scene upload, currently we support only image uploads. But we'll take this as an idea and will see if we can provide an upload option for scenes in future as well.


  • Riccardo just enabled upload of .c4d files. You now have a new "Upload File" button in the editor.

    Here's the scene containing above setup: MoSpline_width_per_segment.c4d

  • Hi Andreas,

    many thanks for the file, really appreciate the help, learnt a lot.

    I have been trying to input values from Xpresso into the Python node, in order to manipulate the "g_arrWidth" value.

    My idea is to send a iteration of numbers, for example if I have 25 splines.

    In the exmample, I tried to just create a iteration from 1 to 4 (1 2 3 4), adding a math node in String mode, so I get in the console (1,2,3,4,), however python is giving me an error and telling me that Input1 is not defined.

    Is possible to change send input from Xpresso and change the g_Arrwidth value with a iteration of numbers?

    You can see in this file what I tried to do.


  • Hi,

    there are a few issues or things to think about.

    First of all, you can not pass the "iteration" as string to the Python node in order to initialize the array. You'd end up with an array of size one with just the entire string being the single entry.

    Then you can use the input ports only inside of the main function. So the global initialization of the array fails.

    Lastly you need to take into account, how Xpresso works. In the end it tries to find the final result by going from right to left. So a final node requests values for its inputs from the previous one. Now with iterators there's another level of complexity, without going into too much detail, think of the iterator making a following node request another result from the previous node. Hm... maybe not the best sentence I have ever written.
    Please see the scene I attached. I have attached a Python node, which simply prints the value of the input port to the console, to an iterator. Watch the console, you will see the Python node printing the iterated values one by line. The Python node gets called once per iteration.
    So now, you have the issue, there's no memory involved. Trying to build this memory for example into the Python node leads to all sorts of issues. For example, you could make the Python node add the input value to a global list (see the commented lines in the example Python).
    Problem: When do you reset the content of the list?
    One could try to compare current to last frame, assuming whenever the frame number changes, one would need to start from scratch. But this would only hold, if the Xpresso is really only executed once per frame. And there are more issues with this approach... not sure, you could solve this reliably for all situations.

    Instead I would recommend to build the array (actually a list) of width values inside the Python node, depending on values fed by input ports. I have changed the example accordingly.



  • Hey Andreas!

    sorry to don't reply sooner but I have been busy with another project. Really thanks for this explanation and the file, I need more time to look into the code and understand it, I will take this scene and put with my system and let you know if it works.

    I am not sure If I understand the inconvenience of using this method, but I guess I will understand better as soon as I play with the file.

    I let you know if it works! :)


  • Hi again Andreas,

    I have been looking the file and I see what you said about the problem of the iteration, is adding one list of numbers every frame. The other python node is very interesting, I see that is multiplying the idx number * 2, and that's how the width distribution is created. The problem is that my particles are not scaled in order by index... I think I gave you a bad example with the last c4d I uploaded..

    I have uploaded the 3D scene, is done with Xparticles, I don't know if you have xparticles, there is a demo, but in case you can't download it I could send you a video demonstration.

    With Xpartcles I created the system explained in first posts, also xparticles has some really cool nodes in Xpresso, with the XpIterator I get the index, with XpGetParticleData I get the radius of each particle (23)

    I think the solution of compare current to last frame would be nice here, because the iteration number is not changing in the whole scene.(Particles are emitted the first frame, they don't die, no value change) Also I only need to activate it one frame, I could uncheck the xpresso enable after first frame if that would fit.
    Sorry, I am trying to look at documentation and/or tutorials as well, but it's very hard to find easy examples to follow. But I have to say I am learning a lot with this examples you provide me.

    Also, I see that is not recognising the XpTrail as a Spline (when I add the XpTrail to the Spline input) I think because this line "if not Spline.CheckType(c4d.Ospline)" and XpTrails has another nomenclature right?

    I work a lot with trails and splines, getting this work would be really helpful.

    (Can't upload the file, the size is bigger than maximum and it's not allowed zip files. If you don't mind, here is a link for download it


  • Hi,

    I think, the basic build blocks are laid out. And if you need to rely on an Xparticles iterator, then you will probably need to stick to the notes in my last post.

    Regarding the check of the input object type, my example code was indeed a bit simple. Knowing nothing about Xparticles trails object, I can't say for sure, but maybe this check works better in this case:

        if not Spline.GetRealSpline().IsInstanceOf(c4d.Ospline):

    I'm not sure I can be of further help here. Sorry!

    Neither do I have Xparticles, nor are we supposed to develop final solutions. Maybe someone in our community who has Xparticles would be able to help further? Or the Xparticles support is able to help out?


  • Hi Andreas,

    thanks for the reply. I will ask to the xparticles community and let's see if there is any solution.

    Regarding to the Mospline formula, Is there way the width for each segment randomly with for example values from 1 to 5?


  • Hi,

    I'm sorry for getting back so late.
    My examples should be easily adapted to random values. Our Python SDK does not directly provide means for random numbers, but Python's random module should do the trick just fine.


Log in to reply