使用Python编写MakeHuman文件:无效语法E

2024-10-04 05:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个无效的语法错误,空格在星号处突出显示

"* modifier macrodetails/Caucasian ' + str(CaucasianQuotient) + '\n\"

我把整件事看了好几遍,但还是不知道出了什么问题

ListOfBodyShape = ['Round', 'RoundBottom', 'RoundTop', 'RoundMuscular', 'MuscularBottom', 'MuscularTop', 'Muscular', 'TopBottom']
ListOfHeight = ['Medium', 'MediumShort', 'TallMedium', 'TallShort', 'Tall', 'Short']
ListOfHairStyle = ['Short', 'Long', 'CurlyShort', 'CurlyLong']
Gender = ['Male', 'Female']
ListOfAges = ['Child', 'Teen', 'Adult', 'Elder']

MuscleQuotient = 0
AfricanQuotient = 0
ProportionQuotient = 0
GenderQuotientQuotient = 0
HeightQuotient = 0
BreastSizeQuotient = 0
AgeQuotient = 0
BreastFirmnessQuotient = 0
AsianQuotient = 0
CaucasianQuotient = 0
WeightQuotient = 0

for a in range(len(ListOfBodyShape)):
    for b in range(len(ListOfHeight)):
        for c in range(len(ListOfHairStyle)):
            for d in range(len(Gender)):
                for e in range(len(ListOfAges)):
                    f = open(ListOfBodyShape[a] + ' ' + ListOfHeight[b] + ' ' + ListOfHairStyle[c] + ' ' + Gender[d] + ' ' + ListOfAges[e]  + '.mhm', 'w')

                    f.write('version v1.1.0\n\
                    tags untitled\n\
                    camera 6.0 347.5 -0.113847193662 0.694580827356 -0.399507358182 0.6375\n\
                    modifier macrodetails-universal/Muscle ' + str(MuscleQuotient) + '\n\
                    modifier macrodetails/African ' + str(AfricanQuotient) + '\n\
                    modifier macrodetails-proportions/BodyProportions '+ str(ProportionQuotient) +'\n\
                    modifier macrodetails/Gender ' str(GenderQuotient) + '\n\
                    modifier macrodetails-height/Height ' + str(HeightQuotient) + '\n\
                    modifier breast/BreastSize' + str(BreastSizeQuotient) + '\n\
                    modifier macrodetails/Age ' + str(AgeQuotient) + '\n\
                    modifier breast/BreastFirmness ' + str(BreastFirmnessQuotient) + '\n\
                    modifier macrodetails/Asian ' + str(AsianQuotient) + '\n\
                    modifier macrodetails/Caucasian ' + str(CaucasianQuotient) + '\n\
                    modifier macrodetails-universal/Weight ' + str(WeightQuotient) + '\n\
                    eyes HighPolyEyes 2c12f43b-1303-432c-b7ce-d78346baf2e6\n\
                    clothesHideFaces True\n\
                    skinMaterial skins/default.mhmat\n\
                    material HighPolyEyes 2c12f43b-1303-432c-b7ce-d78346baf2e6 eyes/materials/brown.mhmat\n\
                    subdivide False\n')

Tags: inforlenrangegendershortmodifierstr
2条回答

此行中str前面缺少“+”符号:

'modifier macrodetails/Gender ' str(GenderQuotient) + '\n\ "

最后还要使用f.close()。你知道吗

首先,您应该了解循环中不需要范围。你知道吗

for a in range(len(ListOfBodyShape)): # a range
    x = ListOfBodyShape[a]  # get value with index

for x in ListOfBodyShape:  # get value

侧注:Python命名约定不以大写字母开头变量


我建议不要试图写一大块文字。你知道吗

我不知道输出应该是什么样子,但是Python有多行字符串,所以除非需要,否则不应该自己编写\n。你知道吗

另外,\n\不仅仅是一条新线。你知道吗

所以从这个开始

f.write("""version v1.1.0
tags untitled
camera 6.0 347.5 -0.113847193662 0.694580827356 -0.399507358182 0.6375\n""")

然后我建议你使用字符串格式。你知道吗

f.write("modifier macrodetails-universal/Muscle {}\n".format(MuscleQuotient))

别忘了用f.close()

或者你可以这样做

filename = "{} {} {} {} {}.mhm".format(ListOfBodyShape[a], ListOfHeight[b], ListOfHairStyle[c], Gender[d], ListOfAges[e])
with open(filename), 'w') as f:
    f.write(...)
# f.close() automatically

相关问题 更多 >