0

I'm trying to update some old code that worked for 2.79 but I'm trying to update to 2.8. One of the parts of the script involves writing a meta file and using some variables from the script to write it. while testing the script, it keeps giving me errors

The code:

     almeta="""fileFormatVersion: 2
guid: """+alguid+"""
timeCreated: 1505499902
licenseType: Free
TextureImporter:
 fileIDToRecycleName: {}
 serializedVersion: 4
 mipmaps:
   mipMapMode: 0
   enableMipMap: 1
   sRGBTexture: 1
   linearTexture: 0
   fadeOut: 0
   borderMipMap: 0
   mipMapsPreserveCoverage: 0
   alphaTestReferenceValue: 0.5
   mipMapFadeDistanceStart: 1
   mipMapFadeDistanceEnd: 3
 bumpmap:
   convertToNormalMap: 0
   externalNormalMap: 0
   heightScale: 0.25
   normalMapFilter: 0
 isReadable: 0
 grayScaleToAlpha: 0
 generateCubemap: 6
 cubemapConvolution: 0
 seamlessCubemap: 0
 textureFormat: 1
 maxTextureSize: """+str(height)+"""
 textureSettings:
   serializedVersion: 2
   filterMode: -1
   aniso: -1
   mipBias: -1
   wrapU: -1
   wrapV: -1
   wrapW: -1
 nPOTScale: 1
 lightmap: 0
 compressionQuality: 50
 spriteMode: 0
 spriteExtrude: 1
 spriteMeshType: 1
 alignment: 0
 spritePivot: {x: 0.5, y: 0.5}
 spriteBorder: {x: 0, y: 0, z: 0, w: 0}
 spritePixelsToUnits: 100
 alphaUsage: 1
 alphaIsTransparency: 0
 spriteTessellationDetail: -1
 textureType: 0
 textureShape: 1
 maxTextureSizeSet: 0
 compressionQualitySet: 0
 textureFormatSet: 0
 platformSettings:
 - buildTarget: DefaultTexturePlatform
   maxTextureSize: """+str(height)+"""     
   textureFormat: -1
   textureCompression: 1
   compressionQuality: 50
   crunchedCompression: 0
   allowsAlphaSplitting: 0
   overridden: 0
 spriteSheet:
   serializedVersion: 2
   sprites: []
   outline: []
   physicsShape: []
 spritePackingTag: 
 userData: 
 assetBundleName: 
 assetBundleVariant:

"""

   file=open(path+alguid+"\\asset.meta","w")
   file.write(almeta)

   file=open(path+alguid+"\\pathname","w")
   file.write("Assets/"+assetname+"/"+assetname+"_Albedo."+context.scene.my_enum2)  

and the error I'm getting is:

UnboundLocalError: local variable 'height' referenced before assignment

for the line below "- buildTarget: DefaultTexturePlatform": maxTextureSize: """+str(height)+"""

height is declared as: height=image.size[1]

What I'm getting confused is that why it is a problem when updating blender versions and it is giving an error to the second height variable instead of the first one

I'm new to python coding and any help of fixing the error is appropriated

  • 1
    Recommend using fstring formatting rather than string addition example https://blender.stackexchange.com/a/136888/15543 Basically you haven't declared height before use. Please post if and where you are declaring the variable height in script above. – batFINGER Sep 23 '20 at 07:58
  • Extending from batFINGER, please remember to escape {} if you're going to use fstring. There does contain some in your string. Using {{}} to escape the literal behavior. – HikariTW Sep 23 '20 at 08:22
  • I declared the height variable as height=image.size[1] before the code posted – user106832 Sep 23 '20 at 08:26
  • Without the whole code this is a guessing game, for instance copying - pasting - running code as is produces same error for alguid Hard to see how this has anything to do with the blender python API apart from it being run as a python script from blender. making it a candidate for closing as off-topic for being a python run-time error. Suggest making a minimum working example by editing code to declare some dummy values eg height = 33.33; alguid="Foo" etc. Often doing this pops out the solution.. – batFINGER Sep 23 '20 at 13:29

0 Answers0