Interacting with Parts#

The following examples use the parts of a vessel to achieve various tasks. The parts guide explains the tree of parts, attachment modes, fuel lines, staging and part modules.

Deploying all Parachutes#

Sometimes things go horribly wrong. The following script does its best to save your Kerbals by deploying all the parachutes:

import krpc

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

for parachute in vessel.parts.parachutes:
    parachute.deploy()

The script deploys the parachutes one call at a time, and the game can advance between the calls. A server side function deploys them all in the same physics tick, with a single call. Python and JavaScript compile it from a function. A C# lambda holds no loop, so C# and the other clients build the loop with the expression API:

def deploy_parachutes():
    for parachute in vessel.parts.parachutes:
        parachute.deploy()


conn.run(deploy_parachutes)

‘Control From Here’ for Docking Ports#

The following example will find a standard sized Clamp-O-Tron docking port, and control the vessel from it:

import krpc

conn = krpc.connect()
vessel = conn.space_center.active_vessel
part = vessel.parts.with_title("Clamp-O-Tron Docking Port")[0]
vessel.parts.controlling = part

Combined Specific Impulse#

The following script calculates the combined specific impulse of all currently active and fueled engines on a rocket. See here for a description of the maths: https://wiki.kerbalspaceprogram.com/wiki/Specific_impulse#Multiple_engines

import krpc

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

active_engines = [
    e for e in vessel.parts.engines if e.active and e.has_fuel
]

print("Active engines:")
for engine in active_engines:
    print("   %s in stage %d" % (engine.part.title, engine.part.stage))

thrust = sum(engine.available_thrust for engine in active_engines)
fuel_consumption = sum(
    engine.available_thrust / engine.specific_impulse
    for engine in active_engines
)
isp = thrust / fuel_consumption

print("Combined Isp = %.0f seconds" % isp)

The script reads the thrust and specific impulse of each engine with separate calls, which can land in different physics ticks. A server side function reads them all in the same tick, with a single call. Python, C# and JavaScript compile it from a function, and the other clients build it with the expression API:

def combined_isp():
    engines = [e for e in vessel.parts.engines if e.active and e.has_fuel]
    thrust = sum(engine.available_thrust for engine in engines)
    fuel_consumption = sum(
        engine.available_thrust / engine.specific_impulse
        for engine in engines
    )
    return thrust / fuel_consumption


isp = conn.run(combined_isp)

See also

The combined specific impulse of each stage is also available directly from the staging API, as Stage.specific_impulse on the object returned by Vessel.stage_at().