BeamNGpy not working with exe

I’m using BeamNG.tech for collecting steering rotation data as well as the road conditions using beamNGpy for my college project. But when I’m running the code on localhost jupyter it doesn’t open the test scenario instead it opens the main menu of the beamNG.tech, how can i solve this.

For reference, I’ve attached the beamNGpy code as well:

import csv
import time
import subprocess

from beamngpy import BeamNGpy, Scenario, Vehicle
from beamngpy.sensors import Electrics, Timer

==========================================================

CONFIGURATION

==========================================================

BNG_HOME = r’E:\BeamNG.Tech’

OUTPUT_CSV = ‘legner_industrial_data.csv’

==========================================================

CLOSE OLD BEAMNG INSTANCES

==========================================================

subprocess.run(
[‘taskkill’, ‘/f’, ‘/im’, ‘BeamNG.tech.x64.exe’],
capture_output=True
)

time.sleep(3)

==========================================================

CREATE BEAMNG INSTANCE

==========================================================

bng = BeamNGpy(
host=‘localhost’,
port=64256,
home=BNG_HOME
)

try:

# ======================================================
# OPEN BEAMNG
# ======================================================

print('Opening BeamNG.tech...')

bng.open(launch=True)

print('Waiting for BeamNG initialization...')
time.sleep(10)

# ======================================================
# CREATE VEHICLE
# ======================================================

vehicle = Vehicle(
    vid='ego_vehicle',
    model='legner',
    license='DATA'
)

# ======================================================
# ATTACH SENSORS
# ======================================================

electrics = Electrics()
timer = Timer()

vehicle.sensors.attach('electrics', electrics)
vehicle.sensors.attach('timer', timer)

# ======================================================
# CREATE SCENARIO
# ======================================================

scenario = Scenario(
    level='industrial',
    name='industrial_logging'
)

# Safe spawn point
scenario.add_vehicle(
    vehicle,
    pos=(505, 178, 25),
    rot_quat=(0, 0, 0, 1)
)

# ======================================================
# BUILD SCENARIO
# ======================================================

print('Building scenario...')

scenario.make(bng)

# ======================================================
# LOAD SCENARIO
# ======================================================

print('Loading scenario...')

bng.scenario.load(scenario)

time.sleep(10)

# ======================================================
# START SCENARIO
# ======================================================

print('Starting scenario...')

bng.scenario.start()

time.sleep(3)

# ======================================================
# CONNECT VEHICLE
# ======================================================

vehicle.connect(bng)

print('Vehicle connected.')

# ======================================================
# SET DETERMINISTIC MODE
# ======================================================

bng.settings.set_deterministic(50)
bng.settings.set_steps_per_second(50)

# ======================================================
# ROAD FRICTION LOOKUP
# ======================================================

mu_lookup = {
    'asphalt': 1.0,
    'concrete': 0.95,
    'dirt': 0.60,
    'grass': 0.40,
    'mud': 0.30,
    'sand': 0.25,
    'snow': 0.20,
    'ice': 0.10
}

# ======================================================
# START LOGGING
# ======================================================

print(f'\nRecording started -> {OUTPUT_CSV}')
print('Drive vehicle manually.')
print('Press CTRL+C to stop.\n')

with open(OUTPUT_CSV, 'w', newline='') as f:

    writer = csv.writer(f)

    writer.writerow([
        'Timestamp',
        'Steering_Angle_Deg',
        'Friction_Mu',
        'Road_Condition'
    ])

    while True:

        # Advance simulation by one step
        bng.control.step(1)

        # Poll sensors
        vehicle.sensors.poll()

        elec_data = vehicle.sensors['electrics']
        timer_data = vehicle.sensors['timer']

        # Timestamp
        timestamp = timer_data['time']

        # Steering input [-1, 1]
        steering_norm = elec_data.get(
            'steering_input',
            0.0
        )

        # Convert to steering wheel angle
        steering_deg = steering_norm * 450.0

        # Ground model
        road = elec_data.get(
            'groundmodel',
            'asphalt'
        ).lower()

        # Friction coefficient
        mu = mu_lookup.get(road, 0.80)

        # Write row
        writer.writerow([
            f'{timestamp:.4f}',
            f'{steering_deg:.2f}',
            f'{mu:.2f}',
            road
        ])

except KeyboardInterrupt:

print('\nLogging stopped by user.')

except Exception as e:

print('\nERROR:')
print(e)

finally:

print('\nClosing BeamNG...')

try:
    bng.close()
except:
    pass

print('Done.')

Hi @PranavVpde,

Thanks for trying out BeamNG.tech!

I gave your script a run on my side. I don’t have the legner vehicle in my install, so I used etk800 instead. Your original spawn point the car ended up under water, so I picked a different position on industrial and from there the script ran cleanly and produced exactly the kind of CSV you are after:

Timestamp,Steering_Angle_Deg,Friction_Mu,Road_Condition
3.0955,0.00,1.00,asphalt
3.1555,0.00,1.00,asphalt
3.2155,0.00,1.00,asphalt
3.2755,0.00,1.00,asphalt

So the logging code itself is fine — the issue is just the vehicle/pose. Replace this block in your script and it should work for you too:

vehicle = Vehicle(
    vid='ego_vehicle',
    model='etk800',
    license='DATA',
)

electrics = Electrics()
timer = Timer()
vehicle.sensors.attach('electrics', electrics)
vehicle.sensors.attach('timer', timer)

scenario = Scenario(level='industrial', name='industrial_logging')
scenario.add_vehicle(
    vehicle,
    pos=(408.36, -336.56, 35.54),
    rot_quat=(0, 0, 0.99, -0.17),
)

If you want to keep legner, make sure the mod is installed in your BeamNG.tech userfolder, typically:

%USERPROFILE%\AppData\Local\BeamNG\BeamNG.tech\current\mods\<YOUR_MOD>\vehicles

Then grab a valid pos / rot_quat for industrial from the World Editor and plug those into add_vehicle(...).

Cheers

if this issue continues, please send me a copy of your console window, it should have history of logs,errors, warnings etc.