Toggling the visibility of an object in celx

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 11 months
Location: Nancy, France

Toggling the visibility of an object in celx

Post #1by Vincent » 21.08.2008, 10:57

As requested by quite a few users recently, here are two examples of scripts that toggle the visibility of an object using the keyboard.
To keep things simple, the chosen object is the Earth. It can be replaced with any other solar system body.
The first script (for Celestia 1.6) also works with DSOs.
The toggle visibility function is associated to the 'shift + T' keys. It can be replaced with any other non-virtual key.
The first script also works with a 'Ctrl + letter' combination. E.g., just use ["C-t"] for 'Ctrl + T'.
For some reason, some letters can't be used in the Windows version, though.

The first script is for use with Celestia 1.6. It uses the object:setvisible() method.

Code: Select all

-- Title: Toggle object visibility - Celestia 1.6

obj = celestia:find("Sol/Earth") -- Edit this line to define another object
obj_rad = obj:radius()

function toggleObjVisibility()
  obj:setvisible(not obj:visible())
  if obj:visible() then
      celestia:print(obj:localname().." visible")
   else
      celestia:print(obj:localname().." invisible")
   end
end

keyhandlers =
{
   T = toggleObjVisibility   -- Edit this line to define another key
}

function handlekey(k)
   handler = keyhandlers[k.char]
   if handler ~= nil then
      handler()
      return true
   else
      return false
   end
end

celestia:registereventhandler("key", handlekey)


The second script is for use with Celestia 1.5. Since the setvisible function was not implemented yet, the object:setradius() function is used to reduce the object to an invisibly small size, and then to give it back its real size.

Code: Select all

-- Title: Toggle object visibility - Celestia 1.5

obj = celestia:find("Sol/Earth") -- Edit this line to define another object
obj_rad = obj:radius()

obj_visible = true
   
function toggleObjVisibility()
   obj_visible = not(obj_visible)
   if obj_visible then
      obj:setradius(obj_rad)
      celestia:print(obj:localname().." visible")
   else
      obj:setradius(1e-5)
      celestia:print(obj:localname().." invisible")
   end
end


keyhandlers =
{
   T = toggleObjVisibility      -- Edit this line to define another key
}

function handlekey(k)
   handler = keyhandlers[k.char]
   if handler ~= nil then
      handler()
      return true
   else
      return false
   end
end

celestia:registereventhandler("key", handlekey)


Eventually, a more complex example of script that toggles several layers can be found here:
viewtopic.php?f=9&t=12702
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 10 months

Re: Toggling the visibility of an object in celx

Post #2by ElChristou » 21.08.2008, 11:09

Hey, 1000!
Tchin! :wink:
(Sorry for the off topic :oops:, but these scripts are great!)
Image

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 11 months
Location: Nancy, France

Re: Toggling the visibility of an object in celx

Post #3by Vincent » 21.08.2008, 11:56

ElChristou wrote:Hey, 1000!
Tchin! :wink:
Hey, didn't even realize... Tchin ! Image
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Re: Toggling the visibility of an object in celx

Post #4by Cham » 21.08.2008, 12:36

ElChristou wrote:these scripts are great!)

These scripts are MUCH more than great, they're fundamental ! It's allowing new kind of stuff never seen before in Celestia. As I wrote elsewhere, they are extremely important for classroom and public presentations. 8) And we can now publish a whole new class of addons. I already have TONS of ideas for new addons. We could show the evolution of the solar system, for example, using a script which turns OFF the current planets and turns ON some others with an accretion disk, etc. I'm now thinking about showing a 3D chart of the Hubble galactic classification with some fake galaxies, etc...
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 10 months

Re: Toggling the visibility of an object in celx

Post #5by ElChristou » 21.08.2008, 12:43

The problem of the script alone is that by default the addon is visible. Also, there is no future for this way to do FOR addons because after a time all keys will be used and it's will be a real mess with override of default function, without talking about addons using the same keys.
As I already said, the coming addon manager (even if you don't believe in it) will be ideal for this kind of use...
Image

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Re: Toggling the visibility of an object in celx

Post #6by Cham » 21.08.2008, 12:53

ElChristou wrote:The problem of the script alone is that by default the addon is visible. Also, there is no future for this way to do FOR addons because after a time all keys will be used and it's will be a real mess with override of default function, without talking about addons using the same keys.
As I already said, the coming addon manager (even if you don't believe in it) will be ideal for this kind of use...

The script is made to accompany the addon itself (SSC, DSC, models, etc). The addon is invisible by default (as declared in the SSC and DSC). And the script doesn't have to use any key at all (I already gave several examples elsewhere. See for example viewtopic.php?f=6&t=12894&start=15). You just put the addon's script into your scripts folder (in some "layers" sub-directory, for example), so you can togle ON/OFF the addon using your scripts menu only, without using the keyboard. There's an infinity of possibilities here.

I already have several invisible layers defined this way, without any keyboard shortcuts. I'm just using the scripts menu to toggle them ON/OFF. (I'm already using all the available keys for other layers, until Chris adds support for the alt-numbers and ctrl-alt-numbers combos)

And this has nohing to do with the "addon manager". The layers offer much more flexibility than an "addon manager", actually (with onscreen comments, actions, etc.) It's all about the power of scripts, in other words. Again, an addon manager has nothing to do with this and the layers are MUCH more powerfull because of the underlying script support.

A layer is fundamentally a script in nature (with all its commands), coupled to some "classical" addon. What is this has anything to do with an "addon manager" ?

The problem with an addon manager is its GUI limitations : it's basically a window with a scroll list, options defined with checkboxes and buttons. It's slow (time consuming, list to scroll, etc), painfull to use (buttons and checkboxes to click on), and inefficient. It is against Celestia's general philosophy (an "interface without interface"). And it will be hardly scriptable.

Any GUI based addon manager will be non-ideal for a classroom or public presentation.
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 10 months

Re: Toggling the visibility of an object in celx

Post #7by ElChristou » 21.08.2008, 13:37

Cham wrote:The script is made to accompany the addon itself (SSC, DSC, models, etc). The addon is invisible by default (as declared in the SSC and DSC). And the script doesn't have to use any key at all (I already gave several examples elsewhere. See for example viewtopic.php?f=6&t=12894&start=15). You just put the addon's script into your scripts folder (in some "layers" sub-directory, for example), so you can togle ON/OFF the addon using your scripts menu only, without using the keyboard. There's an infinity of possibilities here.

Yep, it's pretty neat; I just tested your local grid, and indeed having the script in the script menu is very nice and to my taste almost better than using shortcuts! Good point here! :D
Image

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 10 months

Re: Toggling the visibility of an object in celx

Post #8by ElChristou » 21.08.2008, 19:00

Looking again to the several last addons using layers, I think we should agree to always use the shame scheme; for example:

- always set the models visible false
- release a simple script to toggle ON/OFF via script menu with a comprehensive title
- include in the readme what do the script
- and what version of Celestia is needed

If these 4 points could be followed, I suppose it would be great for the final user...
Image

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 8 months
Location: Hamburg, Germany

Re: Toggling the visibility of an object in celx

Post #9by t00fri » 21.08.2008, 19:15

Cham wrote:
ElChristou wrote:these scripts are great!)

I already have TONS of ideas for new addons.


He, he you are "steeling" Frank's ED "show" ;-) (Never mind....)

Fridger
Image


Return to “Scripting”