Jump to content


marcob

Create Spline from CSV file

Recommended Posts

  • Topic Author
  • ace! supernice thanks!

     

    I wonder how the process of updating/reloading an existing spline could work, so far I guess it should be:

    - select spline to be changed

    - open Window/Structure manager

    - select all points, delete

    - import a new set ASCII data

    now the dream would be to have this set of operations automated....  maybe another script can do it?

    thanks again for the valuable support!

    marco

     

    Share this post


    Link to post
    Share on other sites

    There are two ways, if you don't have splines with the same names, when you load a file and the name of the file already exist in the scene, it will delete the current and replace it with the new one. The other way is to simply replace the selected spline. I will do something about it later ;)

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • great thanks, and would there be a way to set a loop action in the script, so that if you execute it once, it asks 250 times to load 250 csv files? hopefully all of them at once?

    Share this post


    Link to post
    Share on other sites

    Have fun my friend, note that, when you want to update the splines, you can't use "replace selected method" with the "import all files" checked on, it works for a single selected spline, and it doesn't check what's it's name. It simply will replace the spline you have selected with the spline that you open as a single file. I have an idea how to make it work to replace only the selected splines, based on the filenames, but for now, I think i helped you quite a lot and I have other things to do :)

    import c4d
    import os
    import glob
    from os.path import basename
    from c4d import gui,storage,documents
    
    RADIO_GROUP = 10000
    RADIO_OPTIONS = 10001
    RADIO_BY_SELECTION = 10002
    RADIO_BY_NAME = 10003
    RADIO_NEW_SPLINE = 10004
    GROUP_BUTTONS = 10005
    BTN_SELECT_FILE = 10006
    BTN_CANCEL = 10007
    CHK_ALL_FILES = 10008
    
    
    
    class Settings(gui.GeDialog):
        
        def CreateLayout(self):     
            self.SetTitle("Create spline from 'csv' file")    
            self.AddRadioGroup(RADIO_GROUP, c4d.BFH_LEFT,1,1)
            self.AddChild(RADIO_GROUP,RADIO_NEW_SPLINE,"Create new spline")     
            self.AddChild(RADIO_GROUP,RADIO_BY_SELECTION,"Replace selected")
            self.AddChild(RADIO_GROUP, RADIO_BY_NAME, "Replace by name")       
            self.SetBool(RADIO_NEW_SPLINE,True)
            self.GroupEnd()
            self.AddSeparatorH(c4d.BFH_SCALE)
            self.AddCheckbox(CHK_ALL_FILES,c4d.BFH_SCALEFIT,initw=1,inith=1,name="Import all files in the directory")
            self.SetBool(CHK_ALL_FILES,False)
            self.AddSeparatorH(c4d.BFH_SCALE)
            self.GroupBegin(GROUP_BUTTONS,c4d.BFH_CENTER,2,1)
            self.AddButton(BTN_SELECT_FILE,c4d.BFH_SCALE,name="Select file")
            self.AddButton(BTN_CANCEL,c4d.BFH_SCALE,name="Cancel")
            self.GroupEnd()
            
            self.ok = False
            
            return True
        
        def Command(self,id,msg):  
            if id==BTN_CANCEL:
                self.Close()  
            elif id==BTN_SELECT_FILE:
                self.ok = True
                self.replace_by_name = self.GetBool(RADIO_BY_NAME)
                self.replace_by_selection = self.GetBool(RADIO_BY_SELECTION)
                self.create_new_spline = self.GetBool(RADIO_NEW_SPLINE)
                self.all_files = self.GetBool(CHK_ALL_FILES)
                self.Close()
            return True
    
    def splineCreator(lines,name,replace_method):    
        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
        
        if replace_method == "new_spline":
            doc.InsertObject(Spline,checknames=True)
        elif replace_method == "by_selection":
            old_spline = doc.GetActiveObject()
            if not old_spline:
                gui.MessageDialog("No object is selected")
                return
            Spline.InsertAfter(old_spline)
            doc.AddUndo(c4d.UNDOTYPE_DELETE,old_spline)
            old_spline.Remove()
        elif replace_method == "by_name":
            old_spline = doc.SearchObject(name)
            if not old_spline:
                gui.MessageDialog("Cannot find a spline with the same name")
                return
            Spline.InsertAfter(old_spline)
            doc.AddUndo(c4d.UNDOTYPE_DELETE,old_spline)
            old_spline.Remove()
            
        
        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()
        dlg = Settings()
        dlg.Open(c4d.DLG_TYPE_MODAL,defaultw=200,defaulth=50)
        if not dlg.ok:
            return
    
        if dlg.replace_by_selection:
            replace_method = "by_selection"
        elif dlg.replace_by_name:
            replace_method = "by_name"
        elif dlg.create_new_spline:
            replace_method = "new_spline"
    
        path = storage.LoadDialog(title = "Select a csv file")
        
        if path:
            directory = os.path.dirname(path)
            name = os.path.splitext(basename(path))[0]
            files = glob.glob(directory + "\*.csv")
            if dlg.all_files:
                for csv in files:
                    fileName = os.path.splitext(basename(csv))[0]
                    csvFile = open(csv,"r",1) 
                    lines = csvReader(csvFile)
                    splineCreator(lines,fileName,replace_method)
            else:
                csvFile = open(path,"r",1) 
                lines = csvReader(csvFile)
                splineCreator(lines,name,replace_method)
            doc.EndUndo()   
        else:
            print "You must select a csv file."
    
    if __name__=='__main__':
        main()

     

    Share this post


    Link to post
    Share on other sites

    Hey Kalugin,

    I can't seem to get this script to work. 

    I can get it to work on a single spline, but I'm trying to import a batch of CSV's.

    If I select the 'Import all files in the directory' it won't let me select a folder. If I try and select just the top file (thinking it would then select every other file) it does nothing.

    Do you have any ideas what's going on?!

     

    Cheers

    P

    Share this post


    Link to post
    Share on other sites
    Guest storrence

    This script is exactly what I've been looking for. I need to create 100+ objects and didn't want to do it manually. What I'm wanting to do is create a structure like this. Is it possible to modify the script to do that? I need each object to have particular xy and h values. The rest is 0 or default. Then the triangle as a child if possible or I can just do that part manually. Thanks!

    P.X

    P.Y

    R.H

     

    c4dobjects.png

    Share this post


    Link to post
    Share on other sites
    Guest storrence

    I can just create the csv file. I would start with a star reference database. My main goal is to just create the structure but if I can do a few other things like set a few parameters of the object, such as color then that would be icing on the cake! My main difficulty is creating the script to read the csv file and create the object structure. Even if I can't create the child of each object, just having the main objects would be so much easier than manually typing in 100+ items!

    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.

    ×