Parts#

A vessel is made of parts, connected to one another in a tree. This guide shows how to walk that tree, how fuel lines fit into it, and how the staging numbers of each part relate to the staging sequence. It then shows how to reach any part module, including those of mods with no kRPC service. The tree, fuel line and staging examples use the vessel below, whose craft file you can download here.

../_images/parts.png

Figure 1 – The parts that make up the example vessel.#

../_images/parts-tree.png

Figure 2 – The tree of parts for the vessel in Figure 1. Arrows point from each parent part to its children.#

Traversing the Tree#

Parts.Root is the root of the tree, the vessel’s root part: part 1, the command pod, in the example. Part.Children gives the parts attached to a part, and is empty for a part with none. Part.Parent gives the part it is attached to, and is null for the root part.

This example walks the tree depth first, and prints each part indented by its depth:

import krpc

conn = krpc.connect()
vessel = conn.space_center.active_vessel

root = vessel.parts.root
stack = [(root, 0)]
while stack:
    part, depth = stack.pop()
    print(" " * depth + part.title)
    for child in part.children:
        stack.append((child, depth + 1))

For the example vessel, it prints:

Command Pod Mk1
 TR-18A Stack Decoupler
  FL-T400 Fuel Tank
   LV-909 Liquid Fuel Engine
    TR-18A Stack Decoupler
     FL-T800 Fuel Tank
      LV-909 Liquid Fuel Engine
      TT-70 Radial Decoupler
       FL-T400 Fuel Tank
        TT18-A Launch Stability Enhancer
        FTX-2 External Fuel Duct
        LV-909 Liquid Fuel Engine
        Aerodynamic Nose Cone
      TT-70 Radial Decoupler
       FL-T400 Fuel Tank
        TT18-A Launch Stability Enhancer
        FTX-2 External Fuel Duct
        LV-909 Liquid Fuel Engine
        Aerodynamic Nose Cone
   LT-1 Landing Struts
   LT-1 Landing Struts
 Mk16 Parachute

Attachment Modes#

A part is attached to its parent either axially, on the end of the parent to form a stack, or radially, on its side. In the example, the parachute (part 2) is attached axially to the command pod (part 1), and the landing leg (part 5) radially to the fuel tank (part 4). The root part has no parent, and counts as attached axially.

Part.AxiallyAttached and Part.RadiallyAttached give the mode. This example walks the tree as before, and prints each part’s attachment mode:

import krpc

conn = krpc.connect()
vessel = conn.space_center.active_vessel

root = vessel.parts.root
stack = [(root, 0)]
while stack:
    part, depth = stack.pop()
    if part.axially_attached:
        attach_mode = "axial"
    else:  # radially_attached
        attach_mode = "radial"
    print(" " * depth + part.title, "-", attach_mode)
    for child in part.children:
        stack.append((child, depth + 1))

For the example vessel, it prints:

Command Pod Mk1 - axial
 TR-18A Stack Decoupler - axial
  FL-T400 Fuel Tank - axial
   LV-909 Liquid Fuel Engine - axial
    TR-18A Stack Decoupler - axial
     FL-T800 Fuel Tank - axial
      LV-909 Liquid Fuel Engine - axial
      TT-70 Radial Decoupler - radial
       FL-T400 Fuel Tank - radial
        TT18-A Launch Stability Enhancer - radial
        FTX-2 External Fuel Duct - radial
        LV-909 Liquid Fuel Engine - axial
        Aerodynamic Nose Cone - axial
      TT-70 Radial Decoupler - radial
       FL-T400 Fuel Tank - radial
        TT18-A Launch Stability Enhancer - radial
        FTX-2 External Fuel Duct - radial
        LV-909 Liquid Fuel Engine - axial
        Aerodynamic Nose Cone - axial
   LT-1 Landing Struts - radial
   LT-1 Landing Struts - radial
 Mk16 Parachute - axial

Fuel Lines#

../_images/parts-fuel-lines-tree.png

Figure 3 – Part of the tree from Figure 2, with fuel line part 15.#

../_images/parts-fuel-lines.png

Figure 4 – The fuel lines of the example vessel. Fuel flows from the parts in green into the part in blue.#

A fuel line is a part in the tree, as Figure 3 shows. Its parent is the part it takes fuel from. The part it feeds is not in the tree.

In Figure 4, fuel line part 15 takes fuel from fuel tank part 11, in green, and feeds fuel tank part 9, in blue. The fuel line is a child of part 11, and nothing in the tree connects it to part 9.

Part.FuelLinesTo and Part.FuelLinesFrom give these connections. For fuel tank part 11, Part.FuelLinesTo returns a list holding fuel tank part 9. For fuel tank part 9, Part.FuelLinesFrom returns fuel tank parts 11 and 17, both in green.

Staging#

../_images/parts-staging.png

Figure 5 – The example vessel with a staging sequence.#

Each part has two stage numbers: the stage in which it is activated, from Part.Stage, and the stage in which it is decoupled, from Part.DecoupleStage. Both are -1 for a part that staging never activates or decouples.

Figure 5 shows a staging sequence for the example vessel. Figure 6 shows the stage in which each part is activated, and Figure 7 the stage in which each part is decoupled.

../_images/parts-staging-activate.png

Figure 6 – The stage in which each part is activated.#

../_images/parts-staging-decouple.png

Figure 7 – The stage in which each part is decoupled.#

For totals per stage, such as delta-v, thrust and fuel, use Vessel.Stages and Vessel.DecoupleStages, which return a Stage for each stage.

Part Modules#

A part’s behavior comes from its modules, such as ModuleEngines for an engine. kRPC wraps the common modules in classes like Engine and SolarPanel. Every module, including those of a mod, is also available directly, through Part.Modules or Parts.ModulesWithName. A module has three lists:

  • Module.FieldList: the module’s fields, such as an engine’s thrust limiter. The right-click menu shows the fields whose PartField.Visible is true.

  • Module.EventList: the module’s events, the buttons in the right-click menu. The menu shows the events whose PartEvent.Visible and PartEvent.Active are both true.

  • Module.ActionList: the module’s actions, the ones an action group can hold.

This example reads the engine fields the right-click menu shows, limits the thrust of each engine, and extends the solar panels. It covers engines with a ModuleEngines module, and skips those with ModuleEnginesFX, such as the jet engines:

# Print the fields the right-click menu shows for each engine
for module in vessel.parts.modules_with_name("ModuleEngines"):
    for field in module.field_list:
        if field.visible:
            print(module.part.title, field.gui_name, field.value)

# Limit the thrust of each engine to half
for module in vessel.parts.modules_with_name("ModuleEngines"):
    for field in module.field_list:
        if field.name == "thrustPercentage":
            field.float_value = 50

# Extend the solar panels, as their Extend button does
for module in vessel.parts.modules_with_name("ModuleDeployableSolarPanel"):
    for event in module.event_list:
        if event.name == "Extend" and event.active:
            event.trigger()

PartField.Name, PartEvent.Name and PartAction.Name are the identifiers in the module’s code, which stay the same across game versions and languages. The names the menu shows are GuiName, and they change with the game’s language.

PartField.Type gives the type of a field, and the matching property reads and sets it, such as PartField.FloatValue. PartField.Value reads any field as a string, and sets string fields only. Setting a field works as the right-click menu does, so the module reacts to the change, such as B9PartSwitch switching a part’s subtype. PartField.Reset restores the value the field had when the part was loaded.

PartEvent.Trigger clicks an event’s button, and PartAction.Activated triggers an action as its action group does. Module.Config gives the module’s configuration from the part’s configuration file, for values the module does not expose as fields.