這裏有兩個python函數將數據從一個文件傳輸到另一個文件。源文件和目標文件都具有相同數量的對象,但具有不同的數據。Python中多循環邏輯和速度優化?
def getBlock(rigObj, objName):
rigObj.seek(0)
Tag = False
block = ""
for line in rigObj:
if line.find("ObjectAlias " + str(objName) + "\n") != -1:
for line in rigObj:
if line.find("BeginKeyframe") != -1:
Tag = True
elif line.lstrip().startswith("0.000 ") and line.rstrip().endswith("default"):
Tag = False
break
elif Tag:
block += line
return (block)
def buildScene(sceneObj, rigObj, objList):
sceneObj.seek(0)
rigObj.seek(0)
newscene = ""
for line in sceneObj:
newscene += line
for obj in objList:
if line.find("ObjectAlias " + obj + "\n") != -1:
Tag = True
for line in sceneObj:
if line.find("BeginKeyframe") != -1:
newscene += line
newscene += getBlock(rigObj, obj)
Tag = False
elif line.lstrip().startswith("0.000 ") and line.rstrip().endswith("default"):
newscene += line
Tag = True
break
elif Tag:
newscene += line
return (newscene)
getBlock是用於從rigobj獲取數據的子功能;
buildScene是我的主要功能,它有三個參數:
第一個參數(sceneobj)是我想要放入數據的文件;
第二個參數(rigobj)是我從中獲取數據的文件;
第三個參數(objlist)是要傳輸什麼對象數據的列表。
到目前爲止,函數,它的工作,唯一的問題是有點慢的(sceneobj < 10MB,rigobj < 2MB,objlist < 10個對象),我不知道是否有代碼是一個邏輯問題,我應該先循環sceneObj還是先循環objList?它會影響速度嗎?
UPDATE:
兩個sceneObj和rigObj有這樣類似的數據:
lines
BeginObject
lines
ObjectAlias xxx #--> object in transfer list
lines
BeginKeyframe 10 12
-9.000 4095 default #--> transfer begins
lines #--> transfer from rigObj to sceneObj and override lines in sceneObj
-8.000 63 default #--> same
lines #--> same
-7.000 63 default #--> same
lines #--> same
-1.000 63 default #--> same
lines #--> transfer ends
0.000 -1 default
lines
EndKeyframe
EndMotion
lines
EndObject
的數據要被轉移和overrided只有線bewteen BeginKeyframe和0.000 -1默認任何指定的對象(由objList)
你可以添加分析器結果和輸入數據的例子嗎? – Arnial
@Arnial更新了我的文章 – Tian
rigObj可能多次使用相同的ObjectAlias但數據不同? – Arnial