Python scripts

Python is a scripting language used for many purposes. Python can be used in combination with X-Plane to create and run fast scripts (mainly because they are compiled before running). Python is not as easy to learn as FlyWithLua.

To install Python 2.7.9 follow these instructions from the FSE website: Python Install


FSE aliases

Although you should always match your plane in X-Plane as close as possible (weight, payload, fuel usage) with the plane you are flying in FSE, it is not always possible to use the exact same plane. With the alias option you can set the plane in X-Plane to the type you use in FSE.

In FSE I fly a lot for OAAE (see website), where we have a lot of similar but different types of planes. I have made a Python script to make it easier to quickly switch the alias without typing the whole name in the input box (inspired by an idea of Teddii).

The script creates a simple menu item from which you can choose the alias: see screenshot (NB: does not match the example).

Python

The aliases are loaded from a file “FSE_aliases.txt” which you should place in the X-Plane Aircraft folder. #CAT is used to make a menu item followed with the name after the comma. The aliases of the planes are followed by the number of pax after the comma (just for easy reference).
Show FSE_aliases.txt

#CAT, GA 1
Beechcraft 17, 4
Beechcraft Baron 58 - tip tanks (Dreamfleet), 5
Beechcraft Baron 58, 5
Beechcraft Bonanza F33, 5
Beech Debonair, 4
Beechcraft Royal Turbine Duke B60, 5
Cessna 195, 4
Cessna 206 Stationair, 5
Cessna 210 Centurion, 5
Cessna 340A, 5
#CAT, GA 2
BAe Jetstream 32, 19
Beechcraft 1900C, 19
Beechcraft 1900D, 19
Embraer 110, 18
#CAT, Heli's
Bell 407, 6
Eurocopter AS-350 Ecureuil, 5

Copy the following code to any notepad application and save it with a .py extension (instead of .txt). Then place this Python script in the PythonScripts folder (in the Plugin folder).
Show Python script

#########################################################################################################################
# 	Creates a menu from which you can select the alias to use for your plane with FSE
# 	The file with the aliases can be found in the aircraft folder: FSE_aliases.txt
#########################################################################################################################
 
from XPLMDataAccess import *
from XPLMMenus import *
from XPLMPlanes import *
import time
import os
 
##########################################################################################################################
## the main plugin interface class
class PythonInterface:
	def XPluginStart(self):
		self.Name = "X-Economy aliases"
		self.Sig =  "MH.Python.FSE"
		self.Desc = "FSE alias changer"
		self.VERSION="Beta-1"
 
		self.readMaxConfigLines=100
 
		self.aliasFile = os.path.join('Aircraft', 'FSE_aliases.txt')
 
		self.aliasData=[] #list of planes
 
		self.readConfig(0);
 
		return self.Name, self.Sig, self.Desc
 
	def XPluginStop(self):
		pass
 
	def XPluginEnable(self):
		return 1
 
	def XPluginDisable(self):
		pass
 
	def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
		pass
 
	#############################################################
	## Menu Handler
	def XFSEMenuHandler(self, inMenuRef, inItemRef):
		print "[MH|dbg] XFSEMenuHandler: MenuRef="+str(inMenuRef)+" ItemRef="+str(inItemRef)
 
		if inItemRef==100: #reload config
			self.readConfig(1)
			return 1
		for entry in self.aliasData:
			if entry[0]==inItemRef:
				self.changeAlias(entry[1])
				return 1
		return 0
 
	#############################################################
	## read config file
	def readConfig(self, initial):
 
		# XPLMFindPluginsMenu (menubar XP)
		#	- ParentItem (= FSE aliases)
		#		- ParentId (= menu of plugin)
		#			- MenuItem (= options + aliases)
 
		if initial==0:
			self.ParentItem = XPLMAppendMenuItem(XPLMFindPluginsMenu(), self.Name, 0, 1)
			self.XFSEMenuHandlerCB = self.XFSEMenuHandler
			self.ParentId = XPLMCreateMenu(self, self.Name , XPLMFindPluginsMenu(), self.ParentItem, self.XFSEMenuHandlerCB, 0)
		XPLMClearAllMenuItems(self.ParentId)
		self.aliasData=[]
		btnId=110
		XPLMAppendMenuItem(self.ParentId, "Reload aircraft aliases", 100, 1)
		XPLMAppendMenuItem(self.ParentId, "-", 101, 1)
		lineCount=0
 
		if (os.path.exists(self.aliasFile) and os.path.isfile(self.aliasFile)):
			with open(self.aliasFile, "r") as lines:
				print "[MH|dbg] Reading config "+self.aliasFile
				for line in lines:
					# split lines ...
					elements=line.split(",")
					# trim all elements
					elements = [w.strip() for w in elements]
					lineCount=lineCount+1
					if lineCount>self.readMaxConfigLines:
						MenuItem=XPLMAppendMenuItem(self.ParentId, "stopped parsing here ... there are more aircrafts defined in the config!", btnId, 1)
						XPLMEnableMenuItem(self.ParentId, MenuItem, 0)
						break
					else:	
						if elements[0]=="#CAT":
							self.MenuItem = XPLMAppendMenuItem(self.ParentId, elements[1], 0, 1)
							self.MenuId = XPLMCreateMenu(self, elements[1] , self.ParentId, self.MenuItem, self.XFSEMenuHandlerCB, 0)
						else:
							if len(elements)>1:
								pax = elements[1]
							else:
								pax = "?"
							XPLMAppendMenuItem(self.MenuId, elements[0]+" | "+pax+" pax", btnId, 1)
							self.aliasData.append( (btnId, elements[0]) )
							btnId=btnId+1
				return 0
		print "[MH|dbg] Reading config failed"
		return 0
 
	#############################################################
	def changeAlias(self, alias):
		raw_PlanePath = XPLMGetNthAircraftModel(0)
		planePath = os.path.dirname(raw_PlanePath[1])
		print "[MH|dbg] Change alias ("+alias+", "+planePath+")"
		aliasFile = os.path.join(planePath, 'xfse_alias.txt')
		if (os.path.exists(aliasFile) and os.path.isfile(aliasFile)):
			#patch the alias file
			fd = open(aliasFile, 'wb')
			alias = fd.write(alias)
			fd.close()
		else:
			print "[MH|ERR] ... aliasFile '"+aliasFile+"' not found!"
		return 0
 
	#The End