Jump to content


marcob

Create Spline from CSV file

Recommended Posts

hello there this is my first post!

been working on c4d for a while now, but I'm quite new to the scripting/python world, so here is my task:

 

I need to create a spline (200 of them) based on CSV values (or any kind of text format)

I guess the solution is by using Python/Xpresso script?

and by following this: http://www.mazbox.com/node/47

I couldn't make it work... I'm not even sure where this code goes...

additionally there's another catch: it would be optimal if just by replacing the file in the finder, and reloading the c4d file the spline would update with new values

 

once the spline is set up I will attach an object to it and move it along the spline, and that's taken care of... :)

 

any kind of direction can be very helpful, thanks in advance!

thanks

Marco

 

Share this post


Link to post
Share on other sites

Hello Marco,

Please find a very simpler but manual solution to do that (no need of C++ programming ) :

  1. Go "Create" menu and select   Object -> Empty Polygon
  2. Import your CSV or TXT file in the Structure tab (File/Import ASCII Data) for the 3 first columns : (Ni,Xi,Yi) to make a "Point Cloud"
  3. Select your Polygon and create a Tracer  (Mograph menu).
  4. Select Tracing Mode : Connect Elements

 To test :

  • Create an "Align to Spline" Tag on the object to be driven.
  • Drag the Tracer  in "Spline Path" field.
  • Move the Position slider : It works !

Patrice

Share this post


Link to post
Share on other sites
  • Topic Author
  • Thanks for this Patrice,

    I have tried this solution before and yes it works, the problem is that I have 200 splines to work with, and most importantly, I have to be able to reload all path sets easily, because I'm running tests on how objects should move and look while moving...

    so imagine in one day I have to run 5-6 different path sets and it's a bit unrealistic to manually update 200 splines 5-6 times a day!

    that's why I came up thinking of python / scripting as a solution for this,

    optimally c4d should read the data from the CSV file automatically... I'm not even sure this is possible

    thanks for your help!

    m

    Share this post


    Link to post
    Share on other sites
    Guest Harrybee

    Hi,

    maybe you could ask Marek Bereza (via new comment in the link below) if he could adjust his 'Cinema4D Spline from CSV file' script (see link below) for creating of multiple splines by loading of multiple CVS files.

    http://www.mazbox.com/node/47

    Or maybe someone here in the forum with more C4D script knowledge than me can support you to adjust his script for your proposes.

     

    CU and have a great day.

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • thanks guys!

     

    Kalugin, I have attached a simple CSV file, and for this I do have several options on how to export it, so I'm flexible on this

     

    Harrybee, yeah I did get in touch with Marek, no reply so far but thanks for the idea I'll write a comment...

    the point is: I can't make that script work right now! Can't figure out where this code goes, do you guys have a clue?

    thanks!

    m

    test_4.csv

    Share this post


    Link to post
    Share on other sites
    import c4d
    from c4d import gui,storage,documents
    #Welcome to the world of Python
    
    def splineCreator(lines):
        pointCount = len(lines)
        Spline = c4d.SplineObject(pointCount,0)
        Spline[c4d.ID_BASELIST_NAME] = "csv spline"
        n = 0
        for line in lines:
            pos = c4d.Vector()
            pos.x = line[0]
            pos.y = line[1]
            pos.z = line[2]
            Spline.SetPoint(n,pos)
            n+=1
        doc.InsertObject(Spline,checknames=True)
        Spline.Message(c4d.MSG_UPDATE)
        doc.AddUndo(c4d.UNDOTYPE_NEW, Spline)
        c4d.EventAdd()
    
    def csvReader(csvFile):
        csv = csvFile.read().splitlines()
        csvFile.close() 
        lines = []
        for line in csv:
            stringLine = line.split(",")
            n = 0
            for value in stringLine:
                stringLine[n] = float(value)
                n+=1
            lines.append(stringLine)
        return lines
    
    def main():
        doc = c4d.documents.GetActiveDocument()
        c4d.EventAdd(flags = 1)
        doc.StartUndo()
        path = storage.LoadDialog(title = "Select a csv file")
        if path:
            csvFile = open(path,"r",1) 
            lines = csvReader(csvFile)
            splineCreator(lines)
            doc.EndUndo()   
        else:
            print "You must select a csv file."
    
    if __name__=='__main__':
        main()

    this works for your example, but it assumes that there is only 1 spline in the file

    Cheers :)

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • wow kalugin thanks a lot!

    It works and it's already a faster operation, maybe now we can try to improve it?

    - now it asks twice for selecting the spline, and creates also an additional empty one, maybe we can skip that?

    - and, now it doesn't allow to be executed twice but I must increment the number in line 41, maybe it's possible to be done this automatically with a variable?

    - finally, it would be amazing if the name of the spline would be exactly as the csv file... :)

    once again, thanks for your support!

    Share this post


    Link to post
    Share on other sites

    1. I don't know why is that, when I try it, it doesn't ask twice for a csv file. Also the code as it is should not ask twice :) and there is no empty spline
    2.I can run it as many times as i want, it has no limit on that.
    3.I will see if I have time to add the file name to the spline name

    Maybe your other files are different ? the sample that you attached works perfect

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • oh right now I see why, I did load your script in Python Generator panel, instead I should put it in Script Manager and it works well!

    so points 1 and 2 are solved :)

    name of the path same as filename would be very helpful!

    many thanks kalugin :)

    Share this post


    Link to post
    Share on other sites
    import c4d
    import os
    from os.path import basename
    from c4d import gui,storage,documents
    #Welcome to the world of Python
    
    def splineCreator(lines,name):
        pointCount = len(lines)
        Spline = c4d.SplineObject(pointCount,0)
        Spline[c4d.ID_BASELIST_NAME] = name
        n = 0
        for line in lines:
            pos = c4d.Vector()
            pos.x = line[0]
            pos.y = line[1]
            pos.z = line[2]
            Spline.SetPoint(n,pos)
            n+=1
        doc.InsertObject(Spline,checknames=True)
        Spline.Message(c4d.MSG_UPDATE)
        doc.AddUndo(c4d.UNDOTYPE_NEW, Spline)
        c4d.EventAdd()
    
    def csvReader(csvFile):
        
        csv = csvFile.read().splitlines()
        csvFile.close() 
        lines = []
        for line in csv:
            stringLine = line.split(",")
            n = 0
            for value in stringLine:
                stringLine[n] = float(value)
                n+=1
            lines.append(stringLine)
        return lines
    
    def main():
        doc = c4d.documents.GetActiveDocument()
        c4d.EventAdd(flags = 1)
        doc.StartUndo()
        path = storage.LoadDialog(title = "Select a csv file")
        name = os.path.splitext(basename(path))[0]
        if path:
            csvFile = open(path,"r",1) 
            lines = csvReader(csvFile)
            splineCreator(lines,name)
            doc.EndUndo()   
        else:
            print "You must select a csv file."
    
    if __name__=='__main__':
        main()

     

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    • Recently Browsing   0 members

      No registered users viewing this page.

    ×