How do I Create a Button to Show the Next Object?

A user asked

I want to create a button that shows only one object at a time, hiding all others. When I press it multiple times, it should loop through the scene objects showing me the next one.

Answer

Here is a possible solution:

SCRIPT

   macroScript showNext category: "MXS Help"
   (
       local current_object = 0 --this is local to the macroScript
       on execute do
       (
           allObjects = objects as array --collect all objects
           hide $* --hide all objects
           current_object += 1 --increment the counter
           if current_object > allObjects.count do --loop if necessary
               current_object = 1
           if allObjects.count > 0 do --if anything in the scene...
               unhide allObjects[current_object]
           -- then unhide the current object
       )
   )

And this is the same script, but changed to show objects in alphabetical order:

SCRIPT

   macroScript showNextABC category:"MXS Help"
   (
       local current_object = 0 --this is local to the macroScript
       on execute do
       (
           -- collect all names and sort them:
           allObjectNames = sort( for i in objects collect i.name )
           hide $* --hide all objects
           current_object += 1 --increment counter
           if current_object > allObjectNames.count do --loop if necessary
               current_object = 1
           if allObjectNames.count > 0 do --if anything in the scene...
               unhide getNodeByName allObjectNames[current_object]
           -- then get the object by name and unhide it
       )
   )