Python Forum
isolating part of elemnnt in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
isolating part of elemnnt in a list
#5
Thank you for the info, I will try it out very shortly but I feel an explanation of the project will help here:

I am learning Pythion because I use Blender and want to make an addon to speed up lip syncing. Here is a sample video of a lip sync test I made last week (audio recorded in my place, I play drums, I make animations): https://youtu.be/DB3MbDHIZOM

I use PapagayoMod1.3 to create the lip sync DAT files. Papagayo can only work with a couple seconds of audio at a time, so I end up with a folder of DAT files. Here is what a DAT file looks like line by line:
MohoSwitch1
1 FV
2 etc
the number is the frame #, the letters are the phoneme mouth shape.

Because I am only able to create a few seconds at a time, each subsequent DAT file would be offset in the animation timeline BUT Papagayo creates each as though it started at frame #1.

So, I need to strip out the first line of each DAT file, change the number on each line to the offset of that particular DAT file's placement on the timeline, write that to a new DAT file, then combine all those DAT files into a single DAT file for the current and capable Blender DAT importer to work. Originally, the plan was to create a new Blender addon, but I then realized the end result of my plan will create a single DAT file, so I will be able to simply use the current DAT importer addon as is. (I will make my own addon for more control at a later date)  As it currently is, I need to import a DAT file, set the parameters, add it to the timeline, rinse, repeat.  I want to have a single DAT file, as it will greatly reduce the time once I am in Blender.

The number that will be added to the frame number for the offset will be taken from the filename (ie: 0234.dat to offset the frames within the file by 234 frames)


This is the Python code I have right now:
#run in working folder

import os, csv

os.makedirs('headerRemoved', exist_ok=True)


#loop through files in directory
for datFilename in os.listdir('.'):
    if not datFilename.endswith('.dat'):
        continue  #skip non DAT files

    baseName = datFilename
    baseName = int(baseName[:-4]) #make string filename an integer


    print ('Removing header from ' + datFilename + '...')


    datRows = []    
    datFileObj = open(datFilename)
    readerObj = csv.reader(datFileObj)
    for row in readerObj:
        if readerObj.line_num == 1:
            continue  #skip first row
        datRows.append(row)
    datFileObj.close()
    

    #write out dat file
    datFileObj = open(os.path.join('headerRemoved', datFilename), 'w', newline='')
    datWriter = csv.writer(datFileObj)
    for row in datRows:
        datWriter.writerow(row)
    datFileObj.close()
And that is where my studies have got me so far!

Okay I now have it changing the first frame # of the DAT files correctly, so that is progress!
#run in working folder

import os, csv

os.makedirs('headerRemoved', exist_ok=True)


#loop through files in directory
for datFilename in os.listdir('.'):
   if not datFilename.endswith('.dat'):
       continue  #skip non DAT files

   baseName = datFilename
   baseName = int(baseName[:-4]) #make string filename an integer


   print ('Removing header from ' + datFilename + '...')


   datRows = []

   datFileObj = open(datFilename)
   readerObj = csv.reader(datFileObj)
   for row in readerObj:
       if readerObj.line_num == 1:
           continue  #skip first row
       datRows.append(row)
   a, b = datRows[0][0].split()
   datRows[0][0] = '{} {}'.format(int(a)+baseName, b)
   datFileObj.close()
   

   #write out dat file
   datFileObj = open(os.path.join('headerRemoved', datFilename), 'w', newline='')
   datWriter = csv.writer(datFileObj)
   for row in datRows:
       datWriter.writerow(row)
   datFileObj.close()
Reply


Messages In This Thread
isolating part of elemnnt in a list - by trepaning - Oct-29-2016, 04:06 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 04:31 PM
RE: isolating part of elemnnt in a list - by buran - Oct-29-2016, 04:33 PM
RE: isolating part of elemnnt in a list - by trepaning - Oct-29-2016, 07:48 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 08:49 PM
RE: isolating part of elemnnt in a list - by Yoriz - Oct-29-2016, 09:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding List Element if Second part of the List Elements are the Same quest_ 3 2,555 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Select a part of an element of a list with Index BollerwagenIng 0 1,900 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020