thePyFactor 3.0
Return to list

The following code snippet will read the current Blender scene and extract any selected Blender Light that has a label starting with 'Nightlight'. In the current form, you will need to copy the output from the Console (see Using Blender's Console in above list) and paste it into a external SCN file. Use a Blender Point Light to represent rFactor's Omni light. The Point Light controls provide the appropriate values for rFactor's Omni light. The following has been tested in 2.8+.


# Output must look like this::::
#   Light=Nightlight00
#   {
#     Type=Omni Pos=(-384.0, -20.0, -143.0) Range=(0.000000, 76.000000) Active=True Intensity=(1.000000) Color=(255, 255, 255)
#   }

# Type=Omni   :: rFactor's 'Omni' is Blender's 'Point', must make the change before writing out the file
# Pos=()      :: rFactor's 'Pos' is Blender's 'location'
# Range=()    :: rFactor's 'Range' param 2 is Blender's 'distance' - not sure what param 1 is
# Active=     :: no Blender equivilent --- but True is assumed
# Intensity=():: rFactor's 'Intensity' is Blender's 'energy'
# Color=()    :: rFactor's 'Color' is Blender's 'color', BUT Blender's values are normalized 0.0 to 1.0;; rFactor's is 0 to 255, MUST CONVERT

import bpy

for selectedLamp in bpy.data.lights:
    rf_type = 'Not Found'
    lampName = selectedLamp.name
    
    if lampName.startswith('Nightlight'):
        rf_type = 'Omni'
    else:
        continue

    rf_red = int(bpy.data.lights[lampName].color.r * 255)
    rf_green = int(bpy.data.lights[lampName].color.g * 255)
    rf_blue = int(bpy.data.lights[lampName].color.b * 255)
    rf_Pos = bpy.data.objects[lampName].location
    rf_Range = "{:.6f}".format(bpy.data.lights[lampName].distance)       # use::   "{:.6f}".format(some_floating_point_number) to pad ending zeros to 6 decimal places
    rf_Intensity = "{:.6f}".format(bpy.data.lights[lampName].energy)

# -------- instead of printing to the console, this should be appended to a "temp_nightlight_scn.txt" file
# ------------ FOR NOW:::   copy and paste from the console....
    print('Light=',lampName, sep='')
    print('{')
    print('   Type=',rf_type,' Pos=(',rf_Pos.x,', ',rf_Pos.y,', ',rf_Pos.z,') Range=(0.000000, ',rf_Range,') Active=True Intensity=(',rf_Intensity,') Color=(',rf_red,',',rf_green,',',rf_blue,')', sep='')
    print('}')