Hi, this is my first post here. And I am new to python.

I have script which supposed to read in a series of files in the format "myfile..obj" where "" is a sequential no. (001,002...) but when I run the script, it seems that it have difficulty recognizing the "*" thing, it run without problem when a specified the file name like "myfile.0001.obj". I would be appreciated if can give me some pointer on what's going wrong. I feel lost because while there is so many information about python in the net, it is so difficult to find solution for specific question like that.

Any idea will be much appreciated.

Thanks

# #!/usr/bin/env python
#
# """Convert a series of obj files to a single .pc2 file
#
# Command line options:
#    h - show help on usage
#    o - provide output .pc2 filename, default is constructed from first obj file
#    s - provide start frame for .pc2 header, default is 1
#    f - provide sample rate for .pc2 header, default is 1
#    r - do not rotate, (x,y,z) does not become (x,-z,y)
# Examples:
#     $ obj_2_pc2.py myobjs.*.obj
#     $ obj_2_pc2.py -r myobjs.*.obj
#     $ obj_2_pc2.py -s 101 -f 2 -o sample_rate_of_2.pc2  myobjs.*.obj
#
# """
#
# import getopt
# import sys
# import struct
#
# class HelpException(Exception):
#    pass
#
# class NonDigitStartFrameException(Exception):
#    pass
#
# class NonDigitSampleRateException(Exception):
#    pass
#
# def main(argv):
#
#
#    # Set defaults
#    pcFileName=""
#    startFrame=1.0
#    doRotate=1
#    sampleRate=1
#
#    optlist, args = getopt.getopt(argv, "ho:s:rf:")
#
#    # Any files given?
#    if len(args) < 1:
#       raise HelpException()
#
#    # Parse command line arguments
#    for o, a in optlist:
#       if o == "-h":
#          raise HelpException()
#       if o == "-o":
#          pcFileName = a
#       if o == "-s":
#          if a.isdigit():
#             startFrame = float(a)
#          else:
#             raise NonDigitStartFrameException()
#
#       if o == "-r":
#          doRotate=0
#
#       if o == "-f":
#          if a.isdigit():
#             sampleRate = float(a)
#          else:
#             raise NonDigitSampleRateExecption()
#
#
#
#    # Create a default output filename
#    # Assuming some sort of foo.bar.0001.obj or foo.0001.obj format
#    # That will generate foo.bar.pc2
#    if pcFileName == "":
#       splitFirstFile = args[0].split('.')[:-2]   
#       for partName in splitFirstFile:
#          pcFileName = pcFileName + partName + "."
#       pcFileName = pcFileName + "pc2"
#
#
#    # Read the first file to get number of samples..
#    # Where a sample is a vertex, represented as a line beginning with "v"
#    objFile = open(args[0], 'r')
#    vertCount = 0
#    for objLine in objFile:
#       splitObjLine = objLine.split(' ')
#       if splitObjLine[0] == "v":
#          vertCount = vertCount + 1
#    objFile.close()
#
#    numPoints = vertCount
#    numSamples = len(args)
#
#    print "Creating:    " + pcFileName
#    print "Examined:    " + args[0]
#    print 'Vertices:    %(n)d' % {'n': vertCount}
#    print 'Samples:     %(n)d' % {'n': numSamples}
#    print 'Start frame: %(n)f' % {'n': startFrame}
#    print 'Sample Rate: %(n)f' % {'n': sampleRate}
#
#
#    # .pc2 files have a header defined as such:
#
#    # char    cacheSignature[12];   // Will be 'POINTCACHE2' followed by a trailing null character.
#    # int     fileVersion;          // Currently 1
#    # int     numPoints;            // Number of points per sample
#    # float   startFrame;           // Corresponds to the UI value of the same name.
#    # float   sampleRate;           // Corresponds to the UI value of the same name.
#    # int     numSamples;           // Defines how many samples are stored in the fi
#
#    # Create the header
#    headerFormat='12ciiffi'
#    headerStr = struct.pack(headerFormat, 'P','O','I','N','T','C','A','C','H','E','2','\0', 1, numPoints, startFrame, sampleRate, numSamples)
#
#    # Begin writing the pc2 file
#    pcFile = open(pcFileName, 'w+')
#    pcFile.write(headerStr)
#
#    # For each obj file grab the lines beginning with v and pack them into the pc2 file
#    for currObj in args:
#       objFile = open(currObj, 'r')
#       objLine = objFile.readline().split(' ')
#
#       if objLine[0] == "g":
#          vertCount = 0
#          for objLine in objFile:
#             splitObjLine = objLine.split(' ')
#             if splitObjLine[0] == "v":
#                if doRotate == 1:
#                   thisVertex = struct.pack('fff', float(splitObjLine[1]), -float(splitObjLine[3]),  float(splitObjLine[2]))
#                else:
#                   thisVertex = struct.pack('fff', float(splitObjLine[1]), float(splitObjLine[2]),  float(splitObjLine[3]))
#                pcFile.write(thisVertex)
#                vertCount = vertCount + 1
#
#       # Check if vertex count matches that of the first obj file
#       # If this doesn't match then things will go badly wrong.. probably should raise exception
#       if (vertCount != numPoints):
#          print 'Warning: %(obj)s has %(has)d vertices, should have %(should)d.' %\
#             {'obj': currObj, 'has': vertCount, 'should': numPoints}
#
#
#    # Wrap things up
#    pcFile.flush()
#    pcFile.close()
#    print "Done."
#
# # Get the ball rolling, call the main function:
# if __name__ == "__main__":
#    try:
#       main(sys.argv[1:])
#    except getopt.GetoptError, e:
#       print >> sys.stderr, e
#       print >> sys.stderr, "Try '%s -h' for help." % sys.argv[0]
#    except HelpException, e:
#       print "Usage: %s  [-h] [-s startframe] [-o outfile.pc2] [-r] [-f samplerate] objfile .. objfile_n " % sys.argv[0]
#       print __doc__
#    except NonDigitStartFrameException, e:
#       print "Error: %s Start frame not a digit" % sys.argv[0]
#    except NonDigitSampleRateException, e:
#       print "Error: %s Sample rate is not a digit" % sys.argv[0]

Strange looking Python code, everything is in comments?

It looks like the part of the code that parses the object file name has no provision for name globbing (that is, interpreting "*" as "all files"). I don't think it's supposed to work that way.

It may be that when you run it under a *NIX environment (pun intended), the shell expands the *.obj into all file names for you, but under Windows it doesn't.

But the program itself is definitely not written so as to handle *.obj.

Yeah, in fact, here's where the code reads each file:

#    # For each obj file grab the lines beginning with v and pack them into the pc2 file
for currObj in args:
   objFile = open(currObj, 'r')
   objLine = objFile.readline().split(' ')
...

And all it does is run through each supplied file name. So the code itself appears to be relying on the shell to unglob.

:(

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.