Page 1 of 1

Toggling the visibility of an object in celx

Posted: 21.08.2008, 10:57
by Vincent
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

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 11:09
by ElChristou
Hey, 1000!
Tchin! :wink:
(Sorry for the off topic :oops:, but these scripts are great!)

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 11:56
by Vincent
ElChristou wrote:Hey, 1000!
Tchin! :wink:
Hey, didn't even realize... Tchin ! Image

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 12:36
by Cham
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...

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 12:43
by ElChristou
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...

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 12:53
by Cham
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.

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 13:37
by ElChristou
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

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 19:00
by ElChristou
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...

Re: Toggling the visibility of an object in celx

Posted: 21.08.2008, 19:15
by t00fri
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