Howto:Model with parameters

From FlightGear wiki
Jump to navigation Jump to search

Using parameters in modeling makes it possible for example, to animate each single propeller of an multi-engined aircraft without the need to copy the engine model. This reduces the overall filesize and makes it easier for the developer to update the engine animations (just one file to edit).

Examples

The parameters are set in the same file that is used to position the models. In the example below we have two engines.

 <model>
  <path>Aircraft/747-400/Models/Engines/GE_CF6-80C2B1F.xml</path> 
  <overlay>
   <params>
    <n1>/engines/engine[0]/n1</n1>
   </params>
  </overlay>
 </model>
 
 <model>
  <path>Aircraft/747-400/Models/Engines/GE_CF6-80C2B1F.xml</path> 
  <overlay>
   <params>
    <n1>/engines/engine[1]/n1</n1>
   </params>
  </overlay>
 </model>

In the model's .xml file (GE_CF6-80C2B1F.xml in the example above), we place the following paramter declaration and animation:

 <params>
  <n1>/engines/engine[0]/n1</n1>
 </params>
 
 <animation>
  <type>spin</type>
  <object-name>Blades</object-name>
  <property alias="../../params/n1"/>
  <center>
   <x-m>0</x-m>
   <y-m>0</y-m>
   <z-m>0</z-m>
  </center>
  <axis>
   <x>-1.0</x>
   <y>0</y>
   <z>0</z>
  </axis>
 </animation>

This spins the "Blades" object with the speed (rpm) of the n1 properties, as defined in the parameters .xml file. For one engine this is /engines/engine[0]/n1, for the other /engines/engine[1]/n1. The parameters declaration at the top of the code is not required, but it is good for parameter-documentation and gives a default value.

Practically anything can be "aliased," not just <property> tags in animations. Some other examples:

 <animation>
  <type>select</type>
  <object-name>BladesBlur</object-name>
  <condition>
   <greater-than>
    <property alias="../../../../params/n1" />
    <value>20</value>
   </greater-than>
  </condition>
 </animation>

This selects the "BladesBlur" object if the n1 property is above 20. Notice that the number of "../"s in the alias attribute is different. This is because "../" actually navigates "up" in the XML tree, much like one would type cd .. in a command line to navigate "up" in a file directory tree. Since the aliased tag is further down in the tree, at /propertylist/animation/condition/greater-than/property, we need to add more "../"s in the beginning to get back to the root.

Other examples:

 <animation>
  <type>material</type>
  <object-name>InstrumentFrames</object-name>
  <emission>
   <red>0.5</red>
   <green>0.5</green>
   <blue>0.5</blue>
   <factor-prop alias="../../../params/light-cmd-norm"/>
  </emission>
 </animation>
 <animation>
  <type>select</type>
  <object-name>FlapGauge</object-name>
  <condition>
   <value alias="../../../params/show-flap-gauge"/>
  </condition>
 </animation>
 <animation>
  <type>pick</type>
  <object-name>AdjustFreqBigUp</object-name>
  <action>
   <button>0</button>
   <binding>
    <command>property-adjust</command>
    <property alias="../../../../params/standby-mhz"/>
    <value>1</value>
   </binding>
  </action>
 </animation>

Even Nasal scripts can be aliased!

 <animation>
  <type>pick</type>
  <object-name>BigRedButton</object-name>
  <action>
   <button>0</button>
   <binding>
    <command>nasal</command>
    <script alias="../../../../params/do-something-exciting-script"/>
   </binding>
  </action>
 </animation>