代码在IF和ELSE语句中都在执行

时间:2014-09-17 08:01:49

标签: python if-statement

我遇到了一些错误,其中我的函数似乎在我创建的if和else语句中读取,而不是一次读取任何一个。

所以我要做的是,如果它读取导入文件,第一行/第一个字符,如果它不是[B] v [/ B],它会提示警告并要求用户重新 - 导入相关文件。否则,如果文件正确,它将继续并创建定位器并对其进行分组。 示例 - 正确的obj文件中的文件行应为:[I] v -0.6110637188 0.0093309134 8.1327419281 [/ I]

当我尝试另一个obj文件,其中第一行开始时为[I]#这是一个测试文件... [/ I]我被提示有大量错误,如下所示:

# 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 #

正如您在错误日志中看到的那样,它正在打印我在if和else语句中输入的语句。显然,尽管第一个字母是错误的,它只是继续迭代到下一行。此外,它正在创建定位器,它不应该。这是我的代码:

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\n")
            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))

编辑:obj文件包含一个数据列表(位置值),并通过使用它将在此之前创建定位器的值

2 个答案:

答案 0 :(得分:1)

  显然,尽管第一个字母错了,但它仍然存在   迭代到下一行..

你的代码会阻止什么?你需要添加一个检查来打破这个循环:

for line in fileHandle:
    self.processLine(line)

如果函数返回错误的值,或者除了警告之外还抛出异常。

答案 1 :(得分:0)

你需要做的事情很少

  1. 首先,您需要使用line.strip()==''找到该行是空的,因为如果它为空,则无法将其编入索引,因此您的索引超出范围错误,或者您可以使用if line.startswith('v')
  2. 如果你想搜索一个第一个字符的文件,那你为什么要阅读总行数
  3. 这里您将文件的每一行发送到processLine

    for line in fileHandle:
            self.processLine(line)
    

    self.processLine(fileHandle.readlines()[0])