Like I said, at the very least you want to throw in that filehandle.seek(0)
after you read the first line to determine if it contains a 'v'. At least
after doing that you can be sure you will be parsing the whole file.

    for char in fileHandle.readline()[0]:
        if char.startswith("v"):
            fileHandle.seek(0)
            self.processLine()

​
I will just cover my eyes and pretend I don't see the part of the code
where it does a for-loop over a single character, or the shadowing of
python builtins  ;-)


On Wed, Sep 24, 2014 at 2:50 PM, likage <[email protected]> wrote:

> I am curious, if it is possible for me to simply modify the current code I
> have, rather than dumping in a whole new bunch of codes? No offence here
> (just getting frustrated with this code of mine), just thought that if the
> initial coding is somewhat working, except that I will need to modify the
> style, then why not..
>
>
>
> On Saturday, September 20, 2014 5:04:30 AM UTC+8, Justin Israel wrote:
>>
>> Here is a really quick generic example of parsing that file format:
>> https://gist.github.com/justinfx/c49c4bd4acd872a8ad57
>>
>> I didn't use any Maya-specific concepts here because those are easily
>> filled in. The focus of this example was to highlight reading from a file
>> handle, and using seek() to make sure you reset the offset after you have
>> consume some data to validate it. I also broke out the processing of each
>> line into handlers, which makes it pretty easy to read and extend to
>> support more line types.
>>
>> Maybe this helps a bit and you could apply some of this to your
>> Maya-specific code?
>>
>> -- justin
>>
>>
>>
>> On Sat, Sep 20, 2014 at 2:38 AM, damon shelton <[email protected]>
>> wrote:
>>
>>> In your logic you should readline ().strip ()  then split the line by
>>> white space and if splits [0] is 'v' then use splits[1]  splits[2]
>>> splits[3] as X y z coordinates
>>>
>>> On a note. Obj format usually just dumps groups of types together
>>>
>>> All v together all vs together all f together mainly because most
>>> sections refer to other sections by Id
>>>  On Sep 19, 2014 12:55 AM, "Justin Israel" <[email protected]> wrote:
>>>
>>>> For now, just call self.fileHandle.seek(0) at the start of your
>>>> processLine function.
>>>>  On 19/09/2014 7:43 PM, "likage" <[email protected]> wrote:
>>>>
>>>>> hmm.. okay, bummer for me.
>>>>>
>>>>> I thought if in my processLine function, having it to read through all
>>>>> the lines in the self.fileHandle would be a different case scenario...
>>>>> Now I am totally lost...
>>>>>
>>>>>
>>>>> On Friday, September 19, 2014 3:35:43 PM UTC+8, Justin Israel wrote:
>>>>>>
>>>>>> To be honest,  that rewrite is doing a lot of strange things.
>>>>>>
>>>>>> You read the first character of the first line and loop over that
>>>>>> single character string to see if it starts with 'v'. At that point you
>>>>>> have burned the first line and won't process it. Then your processLine
>>>>>> function actually loops over the remaining lines. Is readline() doing 
>>>>>> what
>>>>>> you think it does? It doesn't always start from the beginning. Every time
>>>>>> you read, you advance the file handle. You would want to just read one
>>>>>> character, and check if it is 'v', and then use seek(0) to move the 
>>>>>> offset
>>>>>> back to the start and read all the lines.
>>>>>>
>>>>>> I can't write up an example at the moment, but if you don't figure it
>>>>>> out by the time I get to a computer, I will try and post something.
>>>>>> On 19/09/2014 4:42 PM, "likage" <[email protected]> wrote:
>>>>>>
>>>>>>> I rewrite my code again, will this be any better?
>>>>>>> By the way, I am still getting the part where my UI closes itself if
>>>>>>> it found an invalid file
>>>>>>>
>>>>>>> class ReadObjCloud():
>>>>>>>     """ read in vertex data from nuke 3d camera track """
>>>>>>>
>>>>>>>     def __init__(self, objPath):
>>>>>>>
>>>>>>>         try:
>>>>>>>             fileHandle = open(objPath,"r")
>>>>>>>             self.fileHandle = fileHandle
>>>>>>>             filePath = os.path.basename(fileHandle.name)
>>>>>>>             fileName = os.path.splitext(filePath)[0]
>>>>>>>
>>>>>>>             for char in fileHandle.readline()[0]:
>>>>>>>                 if char.startswith("v"):
>>>>>>>                     self.processLine()
>>>>>>>
>>>>>>>                     # Selects all the imported locators, grouped
>>>>>>> and transformed it
>>>>>>>                     cmds.select(camSel[0] + "_locator*")
>>>>>>>                     objGroup = cmds.group(n=("pointCloud_" +
>>>>>>> fileName))
>>>>>>>                     cmds.xform(os=True, piv=(0, 0, 0))
>>>>>>>                     cmds.scale(0.01, 0.01, 0.01)
>>>>>>>                     cmds.delete(constructionHistory=True)
>>>>>>>
>>>>>>>                     cmds.select(objGroup, camSel[0], add=True)
>>>>>>>                     cmds.group(n="cameraTrack")
>>>>>>>                     cmds.scale(10, 10, 10)
>>>>>>>
>>>>>>>                 else:
>>>>>>>                     self.noProcess()
>>>>>>>
>>>>>>>         except:
>>>>>>>             sys.stderr.write( "Failed to read file information\n")
>>>>>>>             raise
>>>>>>>
>>>>>>>     def processLine(self):
>>>>>>>
>>>>>>>         for line in self.fileHandle:
>>>>>>>             brokenString = string.split(line)
>>>>>>>
>>>>>>>             # Creation of locators and they are named with the
>>>>>>> prefix of the imported camera
>>>>>>>             cmds.spaceLocator(name = camSel[0] + "_locator",
>>>>>>> absolute=True, position=(\
>>>>>>>             float(brokenString[1])*100\
>>>>>>>             , float(brokenString[2])*100\
>>>>>>>             , float(brokenString[3])*100))
>>>>>>>             cmds.CenterPivot()
>>>>>>>             cmds.scale(0.5, 0.5, 0.5)
>>>>>>>
>>>>>>>         self.fileHandle.close()
>>>>>>>
>>>>>>>     def noProcess(self):
>>>>>>>         print "INVALID - Re-import again!!!"
>>>>>>>
>>>>>>>
>>>>>>>  --
>>>>>>> You received this message because you are subscribed to the Google
>>>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>>> send an email to [email protected].
>>>>>>> To view this discussion on the web visit
>>>>>>> https://groups.google.com/d/msgid/python_inside_maya/091119d
>>>>>>> e-fb86-451b-9162-9fe93ef592a7%40googlegroups.com
>>>>>>> <https://groups.google.com/d/msgid/python_inside_maya/091119de-fb86-451b-9162-9fe93ef592a7%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>>> msgid/python_inside_maya/64c719fb-6b35-4e36-887b-
>>>>> 190ad6c5e47b%40googlegroups.com
>>>>> <https://groups.google.com/d/msgid/python_inside_maya/64c719fb-6b35-4e36-887b-190ad6c5e47b%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>> msgid/python_inside_maya/CAPGFgA3Tf8RNYnZkcwPzthHxJ5q_
>>>> go151ET%2BQ%2Bggh7OFHXh5Zw%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3Tf8RNYnZkcwPzthHxJ5q_go151ET%2BQ%2Bggh7OFHXh5Zw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit https://groups.google.com/d/
>>> msgid/python_inside_maya/CAM9RXoLEKKZjotw%3D6XbuNdtBS57UwOLfxuh7NOopp69C
>>> MC2A0g%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/python_inside_maya/CAM9RXoLEKKZjotw%3D6XbuNdtBS57UwOLfxuh7NOopp69CMC2A0g%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/d540641c-fda2-438c-9ace-2d1c75aa540c%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/d540641c-fda2-438c-9ace-2d1c75aa540c%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2KmhbB284_ZDWAUFNcavjayiYo55tUqCEKEjhkezY7Bw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to