Question Simple animation on Lua (auto & managed)

This is what I was reffering to as "lua help" (and theAPI), more than a little vague sometimes, and no mention of MATPROP.LIGHT, where did you get that?
MatProp (C++ API) is described in the API_Reference. The syntax is slightly different but they work the same way. You can also search the Lua code on the Orbiter GitHub repository for 'MATPROP.' I created an issue about the lack of MATPROP documentation in the lua help there, but the information is available in the code base.
 
Thanks, found it. No criticism meant btw, your work on lua has already been a great inspiration and documentation is always difficult.
It can all look a bit like Arcane Lore to the uninitiated Padawan.🧙‍♂️
At least now I know that my syxtax is correct- for some reason it still ignores the diffuse alpha value?

I think I might have bitten off more than I can chew (at least for the moment) as regards the auto level, everything else is working ok so I'll go for a first release.
Also are there any examples of XRSound implementation with lua? or where do I find it in the DG lua code?
 
Setmaterial works, but only color, it ignores the alpha!
Use set_materialex. I seem to recall I ran into this issue as well, but forget why it was a problem. @Gondos may have more insight as he put me onto it.
I couldn't find a way to change a mesh group visibilty either?
You can set the mesh visibility with vi:set_mesh_visibility_mode(mesh, MESHVIS flags)
And this mysterious extra power value? I take it that's specular power?
I haven't sorted that out either.
 
None of the original vessels use XRSound but I added an autopilot sound as an example for the Atlantis Shuttle.
You can look here : https://github.com/orbitersim/orbit...tersdk/samples/Lua/Atlantis/Src/APMFD.lua#L24 for initialization and here : https://github.com/orbitersim/orbit...ersdk/samples/Lua/Atlantis/Src/APMFD.lua#L339 for playing a sound.
Thanks, I'll take a look, thought I might have had a case of code blindness.;)
Use set_materialex. I seem to recall I ran into this issue as well, but forget why it was a problem. @Gondos may have more insight as he put me onto it.
Sorry, I meant set_materialex too, I can maybe find a workaround, the warning lights are a little too bright in the off state momentarily.
You can set the mesh visibility with vi:set_mesh_visibility_mode(mesh, MESHVIS flags)
Can one also set msh GROUP visibility too? Couldn't quite decipher that (I think the .msh files have a flag for no render).
I've just seen your thread for GRP_EDIT, I'll see what I can find.
 
Last edited:
Thanks, I'll take a look, thought I might have had a case of code blindness.;)

Sorry, I meant set_materialex too, I can maybe find a workaround, the warning lights are a little too bright in the off state momentarily.
I am not sure what problem you are having with setting the alpha channel. I can make the R-4 disappear by setting alpha to 0:

Screenshot at 2025-03-21 15-18-45.png

Code:
color = {r=0.306406,g=0.953185,b=0.850856,a=0.000000}

I set the colors in this function:

Code:
function set_pretty.helicopter_color()

    if color ~= nil then
    
        if hdevmesh0 ~= nil then

            oapi.set_materialex(hdevmesh0, 3, MATPROP.DIFFUSE, color)
            oapi.set_materialex(hdevmesh0, 3, MATPROP.AMBIENT, color)
            oapi.set_materialex(hdevmesh0, 3, MATPROP.SPECULAR, color)

        end

        if hdevmesh1 ~= nil then

            oapi.set_materialex(hdevmesh1, 0, MATPROP.DIFFUSE, color)
            oapi.set_materialex(hdevmesh1, 0, MATPROP.AMBIENT, color)
            oapi.set_materialex(hdevmesh1, 0, MATPROP.SPECULAR, color)

        end
        
    end

end

Are you making a development mesh in clbk_visualcreated? If you are trying to manipulate the base mesh it won't work. You are manipulating the copy of the mesh that is loaded into Orbiter:

Code:
function clbk_visualcreated(vis, refcount)

    hdevmesh0 = vi:get_devmesh(vis,0)
    hdevmesh1 = vi:get_devmesh(vis,1)

    set_pretty.clear_windows() --control inner window reflections
    set_pretty.cabin_lights()
    set_pretty.cockpit_glow()
    set_pretty.helicopter_color()

end

Can one also set msh GROUP visibility too? Couldn't quite decipher that (I think the .msh files have a flag for no render).
I've just seen your thread for GRP_EDIT, I'll see what I can find.
I don't believe there is a way to alter the visibility of a particular mesh group aside from setting the alpha channel to 0. Alternatively, you might want to separate that meshgroup into a separate mesh file and load it and set its visibility separately.
 
I am not sure what problem you are having with setting the alpha channel. I can make the R-4 disappear by setting alpha to 0:

Screenshot at 2025-03-21 15-18-45.png


color = {r=0.306406,g=0.953185,b=0.850856,a=0.000000}
I set the colors in this function:

function set_pretty.helicopter_color() if color ~= nil then if hdevmesh0 ~= nil then oapi.set_materialex(hdevmesh0, 3, MATPROP.DIFFUSE, color) oapi.set_materialex(hdevmesh0, 3, MATPROP.AMBIENT, color) oapi.set_materialex(hdevmesh0, 3, MATPROP.SPECULAR, color) end if hdevmesh1 ~= nil then oapi.set_materialex(hdevmesh1, 0, MATPROP.DIFFUSE, color) oapi.set_materialex(hdevmesh1, 0, MATPROP.AMBIENT, color) oapi.set_materialex(hdevmesh1, 0, MATPROP.SPECULAR, color) end end end
Are you making a development mesh in clbk_visualcreated? If you are trying to manipulate the base mesh it won't work. You are manipulating the copy of the mesh that is loaded into Orbiter:

function clbk_visualcreated(vis, refcount) hdevmesh0 = vi:get_devmesh(vis,0) hdevmesh1 = vi:get_devmesh(vis,1) set_pretty.clear_windows() --control inner window reflections set_pretty.cabin_lights() set_pretty.cockpit_glow() set_pretty.helicopter_color() end
I don't believe there is a way to alter the visibility of a particular mesh group aside from setting the alpha channel to 0. Alternatively, you might want to separate that meshgroup into a separate mesh file and load it and set its visibility separately.
Thanks, so Diffuse alpha would be the way to do it.
I'll try again:)
 
Note that I am setting diffuse, ambient, and specular colors to the same rgba color with a = 0.
I did notice that in the Orbiter API only Diffuse has an alpha value, maybe different in lua? I could change the color just not the alpha, will experiment further and post back.
 
I would like to try making simple gear animations on Lua, namely assign a key to revert rotation direction between "gear up" and "gear down". I already know how to create a cyclic infinitive animation (rotation of solar panels of a satellite), and how to assign a key to enable/disable a spotlight on an astrounaut helmet.

Maybe anyone has a simple template. Lua code for DG looks too difficult for me.
 
I need some help with gear animation. In a mesh file the gears are up and gear doors are closed. I have working Lua script for one of my previous add-on where the gears are down and gear doors are open. What the minimal and simple changes can I do in order after launch a scenario to get "gears down" and "gear doors open"?
C++:
FrontDoorRear =
{
  type = 'rotation',
  mesh = 0,
  grp = {14,15},
  ref = {x=0.0003,y=-0.7170,z=3.35},
  axis = {x=1,y=0,z=0},
  angle = 110*RAD
}

-- Gear animation parameters

GearAnimationSpeed = 0.3

GearOpen=0
GearClosed=1
GearOpening=2
GearClosing=3

GearStatus = GearOpen
GearState = 0

-- CLBK SETCLASSCAPS FUNCTION

function clbk_setclasscaps(cfg)
  
 -- Gears animation

  FrontDoorRearComponent = oapi.create_animationcomponent(FrontDoorRear)
  GearAnimation = vi:create_animation(0)
  vi:add_animationcomponent(GearAnimation, 0, 1, FrontDoorRearComponent)

end

-- Reverse function for gear animation

local function revert_status(status)
  if status == GearOpen or status == GearOpening then
    return GearClosing
  else
    return GearOpening
  end
end

-- Function for gear animations

function clbk_poststep(simt, simdt, mjd)

    if GearStatus >= GearOpening then 
    local da = simdt * GearAnimationSpeed
        if GearStatus == GearOpening then
        if GearState > 0.0 then
        GearState = math.max(0.0, GearState - da)
            else
        GearStatus = GearOpen
      end
        else
            if GearState < 1.0 then
        GearState = math.min(1.0, GearState + da)
            else
        GearStatus = GearClosed
      end 
    end
    vi:set_animation(GearAnimation, GearState)
  end
end

-- Key for gear animation

function clbk_consumebufferedkey(key, down, kstate)
  if oapi.keydown(kstate, OAPI_KEY.G) then
    GearStatus = revert_status(GearStatus)   
  end
end
I see a solution: release gears in game and save a scenario, but it requires to save the gear status in the scenario file, but I don't know how to implement it yet, although it would be better.
 
Oh, I'm sorry, I forgot again there's the beautiful HST.lua example with needed for me feature. It certainly should help me.
 
I would like to implement simple infinite, but managed animation for a propeller. I have the code for infinite propeller rotation:
C++:
  -- Propeller animation
 
  T = 0.33333

  propeller =
  {
    type = 'rotation',
    mesh = 0,
    grp = {19,20,21},
    ref = {x=0, y=-0.010465, z=3.8782},
    axis = {x=0, y=0, z=1},
    angle = 2*PI
  }

  create_anim_propeller = oapi.create_animationcomponent(propeller)
  anim_propeller = vi:create_animation(0)
  vi:add_animationcomponent(anim_propeller, 0, 1, create_anim_propeller)
And I have a code for light switching:
C++:
-- Key for propeller switching

PropellerSwitch = true

function clbk_consumebufferedkey(key, down, kstate)
  if oapi.keydown(kstate, OAPI_KEY.E) then

    if PropellerSwitch == false then
    PropellerSwitch = true
    else PropellerSwitch = false
    end

    PropellerSwitch.active = LightSwitch
  end   
end
I understand the code for light switching is not compatible with animation, so I need a simple example how to implement enabling/disabling (by pressing a key) infinite propeller rotation.
 
I would like to implement simple infinite, but managed animation for a propeller. I have the code for infinite propeller rotation:
C++:
  -- Propeller animation
 
  T = 0.33333

  propeller =
  {
    type = 'rotation',
    mesh = 0,
    grp = {19,20,21},
    ref = {x=0, y=-0.010465, z=3.8782},
    axis = {x=0, y=0, z=1},
    angle = 2*PI
  }

  create_anim_propeller = oapi.create_animationcomponent(propeller)
  anim_propeller = vi:create_animation(0)
  vi:add_animationcomponent(anim_propeller, 0, 1, create_anim_propeller)
And I have a code for light switching:
C++:
-- Key for propeller switching

PropellerSwitch = true

function clbk_consumebufferedkey(key, down, kstate)
  if oapi.keydown(kstate, OAPI_KEY.E) then

    if PropellerSwitch == false then
    PropellerSwitch = true
    else PropellerSwitch = false
    end

    PropellerSwitch.active = LightSwitch
  end  
end
I understand the code for light switching is not compatible with animation, so I need a simple example how to implement enabling/disabling (by pressing a key) infinite propeller rotation.
Why not condition using vi:set_animation on your toggle switch ?
 
Thanks. I just ask ChatGPT about it, and honestly I didn't expect, but it gave me a working code:
C++:
-- Propeller animation
 
  T = 0.33333

  propeller =
  {
    type = 'rotation',
    mesh = 0,
    grp = {19,20,21},
    ref = {x=0, y=-0.010465, z=3.8782},
    axis = {x=0, y=0, z=1},
    angle = 2*PI
  }

  create_anim_propeller = oapi.create_animationcomponent(propeller)
  anim_propeller = vi:create_animation(0)
  vi:add_animationcomponent(anim_propeller, 0, 1, create_anim_propeller)

  PropellerOn = false
      
function clbk_consumebufferedkey(key, down, kstate)   
  if oapi.keydown(kstate, OAPI_KEY.E) then
    PropellerOn = not PropellerOn
  end     
end

function clbk_poststep(simt)
  if PropellerOn then
    anim_propeller_level = (simt/T) % 1
    vi:set_animation(anim_propeller, anim_propeller_level)
  else
    vi:set_animation(anim_propeller, 0)
  end
end
I just made several small changes. It really works.
 
Back
Top