Building rFactor2 plugins with Python using PyRFactor2Plugin

Sun 03 June 2012 // posts

PyRFactor2Plugin, similar to rfactor2python for rFactor, is a plugin for rFactor2 that forwards plugin calls to Python scripts, passing the game data as well, making the task of programming rFactor2 plugins much easier, specially useful for prototyping, since Python is loaded with tons of useful modules ready to use, and their documentation is very, very good.

Features:

  • Easy to create and use Python code as a rFactor2 plugin.
  • No need to know C++, No need to compile code.
  • Python provides huge functionality with little work, has high quality documentation and there are plenty of 3rd party libraries available.
  • Multiple Python plugins can run at once.
  • Fast prototyping.

Changelog:

20120601 - download

  • First public release.
  • Some plugin methods are already supported, including UpdateScoring().

Requirements:

  • Python 2.7 32bit
  • Microsoft Visual C++ 2010 Runtime

Documentation:

  • Check the file pyrfactor2plugins_examples/skeleton.py for documentation.
  • Consult the official Internals Plugin SDK.

Installing PyRFactor2Plugin:

  1. Extract PyRFactor2Plugin zip file into rFactor2 root directory

Using Python plugins:

  1. Place Python plugins inside pyrfactor2plugins/ directory
  2. Run rFactor2 (or its dedicated server)
  3. Check pyrfactor2plugin.log for errors.

Example plugins

Log everything to file:

import sys  
import pprint

def write_to_log(what):  
    f = open('pyplugin_test.txt', 'a')  
    f.write(str(what))  
    f.close()


class RF2PyPlugin(object):  
    def __init__(self):  
        write_to_log("__init__\n")

    # game startup  
    def Startup(self, version):  
        write_to_log("Startup\n")

    # game shutdown  
    def Shutdown(self):  
        write_to_log("Shutdown\n")

    # entering realtime (where the vehicle can be driven)  
    def EnterRealtime(self):  
        write_to_log("EnterRealtime\n")

    # exiting realtime  
    def ExitRealtime(self):  
        write_to_log("ExitRealtime\n")

    # session started  
    def StartSession(self):  
        write_to_log("StartSession\n")

    # session ended  
    def EndSession(self):  
        write_to_log("EndSession\n")

    # update plugin with scoring info (approximately once per second)  
    # 'info' is a dictionary with scoring data  
    def UpdateScoring(self, info):  
        write_to_log("UpdateScoring\n")  
        sys.stdout = open('mylog.txt', 'a')  
        pprint.pprint(info)  
        sys.stdout.close()  
        sys.stdout = sys.__stdout__  

Serial plugin demo:

This demo uses pyrfactor2plugin to interface with rfactor-led-display


social