I have run into some errors in which my function seems to be reading in both if and else statements that I have created, instead of either one at a time.
So what I am trying to do is that, if it reads the importing file, first line/first character, if it is not v, it will prompts a warning and asked the user to re-import the relevant file. Else, if the file is correct, it will go on and creates the locators and group it.
Example - The file line in the correct obj file should be: v -0.6110637188 0.0093309134 8.1327419281
As soon as I tried another obj file in which it first line is started off as # This is a testing file… I was prompted with tons of errors as follows:
# Warning: Please import in a relevant Point Cloud Obj #
# Warning: Please import in a relevant Point Cloud Obj #
# Warning: Please import in a relevant Point Cloud Obj #
# Warning: Please import in a relevant Point Cloud Obj #
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
This part works if importing the right point cloud
# Failed to read file information
# Error: list index out of range
# Traceback (most recent call last):
# File "<maya console>", line 62, in <module>
# File "<maya console>", line 16, in __init__
# File "<maya console>", line 58, in processLine
# IndexError: list index out of range #
As you can see in the error log, it is printing the statement that I have inputted in both if and else statements. Additionally, it is creating out the locators as well which it should not be. This is my code:
import maya.cmds as cmds
import sys, os
import string
class ObjCloud():
def __init__(self):
try:
fileHandle = open("/user_data/aaa.obj","r")
filePath = os.path.basename(fileHandle.name)
fileName = os.path.splitext(filePath)[0]
for line in fileHandle:
self.processLine(line)
fileHandle.close()
except:
sys.stderr.write( "Failed to read file information
")
raise
cmds.select("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)
cmds.group(n="cameraTrack")
cmds.scale(10, 10, 10)
def processLine(self, line):
if(line[0] != "v"):
cmds.warning("Please import in a relevant Point Cloud Obj")
else:
brokenString = string.split(line)
cmds.spaceLocator(absolute=True, position=(\
float(brokenString[1])*100\
, float(brokenString[2])*100\
, float(brokenString[3])*100))
cmds.CenterPivot()