WNP78

scale.py

May 10th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import os,sys,time
  3. class ProgressBar():
  4.     def __init__(self,title):
  5.         global progress_x
  6.         sys.stdout.write(title + ": [" + "-"*40 + "]" + chr(8)*41)
  7.         sys.stdout.flush()
  8.         progress_x = 0
  9.  
  10.     def setprog(self,x):
  11.         global progress_x
  12.         x = int(x * 40 // 100)
  13.         sys.stdout.write("#" * (x - progress_x))
  14.         sys.stdout.flush()
  15.         progress_x = x
  16.  
  17.     def end(self):
  18.         sys.stdout.write("#" * (40 - progress_x) + "]\n")
  19.         sys.stdout.flush()
  20. def subE(st):
  21.     if "E" in st:
  22.         st = st.split("E")
  23.         assert len(st) == 2
  24.         st = float(st[0])*(10**int(st[1]))
  25.     return st
  26. def gi(p):
  27.     try:return raw_input(p)
  28.     except NameError:return input(p)
  29. def tryAuto():
  30.     appd = os.environ["APPDATA"]
  31.     d = os.path.join(os.path.dirname(appd),"LocalLow","Jundroo","SimplePlanes","AircraftDesigns")
  32.     if os.path.isdir(d):
  33.         return d
  34.     else:
  35.         return 0
  36. spDir = tryAuto()
  37. if spDir == 0:spDir = gi("SP Aircraft Directory>")
  38. for ln in os.listdir(spDir):
  39.     if ln.endswith(".xml"):
  40.         pass#print(ln[:len(ln)-4:])
  41. infile = gi("Aircraft name>")+".xml"
  42. infile = os.path.join(spDir,infile)
  43. outfile = infile#os.path.join(spDir,gi("Output name>")+".xml")
  44. tree = ET.parse(infile)
  45. toScale = gi("Scale>")
  46. if toScale == "": toScale = [gi("xScale>"),gi("yScale>"),gi("zScale>")]
  47. else: toScale = [toScale,toScale,toScale]
  48. root = tree.getroot()
  49. pth = ["Assembly","Parts"]
  50. cd = root
  51. for s in pth:
  52.     for ite in cd:
  53.         if ite.tag == s:
  54.             cd = ite
  55.             break
  56. assert cd.tag == "Parts"
  57. p=1
  58. tp=len(cd)
  59. pb=ProgressBar("Sacling...")
  60. for part in cd:
  61.     pb.setprog((p/tp)*100)
  62.     eng = False
  63.     prop= False
  64.     pos = part.attrib["position"]
  65.     if "scale" in part.attrib:
  66.         scale = part.attrib["scale"]
  67.     else:
  68.         scale = "1,1,1"
  69.     if "massScale" in part.attrib:
  70.         massScale = part.attrib["massScale"]
  71.     else:
  72.         massScale = 1.0
  73.     fuel = False
  74.     for chld in part:
  75.         if chld.tag == "Engine.State":
  76.             eng = chld
  77.             if "powerMultiplier" in chld.attrib: powerMultiplier = chld.attrib["powerMultiplier"]
  78.             else: powerMultiplier = 1
  79.             if "exhaustScale" in chld.attrib: exhaustScale = chld.attrib["exhaustScale"]
  80.             else: exhaustScale = "1,1,1"
  81.         elif chld.tag == "PropEngineAdvanced.State":
  82.             prop = chld
  83.             pPower = chld.attrib["power"]
  84.         elif chld.tag == "FuelTank.State":
  85.             fuel = chld
  86.             fuelA,fuelC = fuel.attrib["fuel"],fuel.attrib["capacity"]
  87.     if eng == False:
  88.         powerMultiplier = False
  89.         exhaustScale    = False
  90.     pos = pos.split(",")
  91.     assert len(pos) == 3
  92.     scale = scale.split(",")
  93.     assert len(scale) == 3
  94.     if exhaustScale != False:
  95.         exhaustScale = exhaustScale.split(",")
  96.         assert len(exhaustScale) == 3
  97.     for i in range(0,3):
  98.         ts = float(toScale[i])
  99.         pos[i],scale[i] = subE(pos[i]), subE(scale[i])
  100.         pos[i] = float(pos[i]) * ts
  101.         scale[i] = float(scale[i]) * ts
  102.         if exhaustScale != False:exhaustScale[i] = float(exhaustScale[i]) * ts
  103.     cubeMulti = float(toScale[0]) * float(toScale[1]) * float(toScale[2])
  104.     if powerMultiplier != False:powerMultiplier = float(powerMultiplier) * cubeMulti
  105.     if prop != False: pPower = float(pPower) * cubeMulti
  106.     if fuel != False: fuelA,fuelC = float(fuelA) * cubeMulti, float(fuelC) * cubeMulti
  107.     massScale = float(massScale) * float(toScale[0]) * float(toScale[1]) * float(toScale[2])
  108.     part.set("position",  (  str(pos[0]) + "," + str(pos[1]) + "," + str(pos[2]) ) )
  109.     part.set("scale",  (  str(scale[0])  + "," +  str(scale[1])  + "," +  str(scale[2]) ) )
  110.     part.set("massScale", str(massScale))
  111.     if powerMultiplier != False:
  112.         eng.set("powerMultiplier",str(powerMultiplier))
  113.     if exhaustScale    != False:
  114.         eng.set("exhaustScale"   ,str(exhaustScale[0]) + "," + str(exhaustScale[1]) + "," + str(exhaustScale[2]))
  115.     if prop != False:
  116.         prop.set("power", str(pPower))
  117.     if fuel != False:
  118.         fuel.set("fuel",str(fuelA))
  119.         fuel.set("capacity",str(fuelC))
  120. pb.end()
  121. tree.write(outfile)
  122. gi("File Saved: "+outfile)
Advertisement
Add Comment
Please, Sign In to add comment