Can I use Ruby to call the timeline from the SketchUp shadow setting function?

Can I use Ruby to call the timeline from the SketchUp shadow setting function?If so,how?The goal is to control the rotation of the plane with the drag of the timeline slider
,88bd63b5b09f20ca382ed77a8c43b09

You access these setting via the API’s Sketchup::ShadowInfo class.
This class’ instances as Hash-like objects.

model = Sketchup.active_model
shadowinfo = model.shadow_info

t = shadowinfo["ShadowTime"]
#=> 2021-11-08 08:30:00 -0500
t.class
#=> Time

So, the object returned is a core Ruby Time object.

You can use the Time class’ + and - methods to add or subtract time.
(These methods take numeric seconds as their argument.) …

secs_per_hour = 3600
# Subtract 4 hours from t:
t = t - (secs_per_hour * 4)
# Reassign it to the model's shadow time:
shadowinfo["ShadowTime"]= t

OR, you can construct a new time object and assign that to the model.

shadowinfo["ShadowTime"]= Time.new(2021,1,22, 9,24,0, "+08:00")

To be more generic, use the model’s current year, month, day and gmt offset …

# The ( hour, min, sec ) arguments are in GMT time, not local time.
def set_model_time( hour, min, sec )
  model = Sketchup.active_model
  shadowinfo = model.shadow_info
  t = shadowinfo["ShadowTime"]
  shadowinfo["ShadowTime"]= Time.new(
    t.year, t.month, t.day, hour, min, sec, t.gmt_offset
  )
end
3 Likes

If you want a method that takes the model’s local time:

# The ( hour, min, sec ) arguments are in local time.
# The hours are per a 24 hour clock.
def set_model_time( hour, min, sec )
  model = Sketchup.active_model
  shadowinfo = model.shadow_info
  t = shadowinfo["ShadowTime"]
  t2 = Time.new(
    t.year, t.month, t.day, hour, min, sec, t.gmt_offset
  )
  shadowinfo["ShadowTime"]= t2 + t.gmt_offset
end
1 Like

I have a problem. What I want to achieve is that the fetching time in the program is constantly changing with the slider. Currently, I can only fetch the fixed position of the slider, and it cannot change with the slider

I do not know if this is possible.

If it is, then you will need to define a custom ShadowInfoObserver to react to the changes that the user makes as they move the slider.

But, … the change may not fire until the user stops dragging the slider. You’ll need to test.

Indeed, as you said, the program changes only start when the user stops dragging the slider. Is there any other way to improve it? Do you have any new ideas about using the time axis to control the dynamic rotation of the plane?

Zuoyou, this seems to work on my computer

module SomeModuleName
  class MyShadowInfoObserver < Sketchup::ShadowInfoObserver
    def onShadowInfoChanged(shadow_info, type)
      # puts "onShadowInfoChanged: #{type}"
      Sketchup.active_model.active_view.invalidate
    end
  end

  class ShadowTool
    def initialize()
      Sketchup.active_model.select_tool(self)
    end
     
    def activate
      # puts 'activate'
      # Attach the observer.
      @observer = MyShadowInfoObserver.new
      Sketchup.active_model.shadow_info.add_observer(@observer)
      Sketchup.active_model.active_view.invalidate
    end

    def deactivate(view)
      # puts 'deactivate'
      Sketchup.active_model.shadow_info.remove_observer(@observer)
      view.invalidate
    end
    
    def draw(view)
      model = Sketchup.active_model
      shadowinfo = model.shadow_info 
      @text = shadowinfo["ShadowTime"].to_s
      view.draw_text([30, 30], @text, {size: 18, color: [80, 80, 80]})
    end
    
  end # tool
end

SomeModuleName::ShadowTool.new
nil
1 Like

Yes, it works on my machine as well. Nicely done !

1 Like

I want to use the variable of year, month, day, hour, minute, second and so on in module SW respectively for operation. I have tried for many times, but there are always mistakes. Could you please help me write an instance?

shadowinfo["ShadowTime"] returns a standard Ruby Time class object.
It has instance methods to get all that you ask for.

The module name was just a temporary namespace for the example. You would name it something else, or more likely add the example methods to your own extension module.

Through your module, we can get the time that changes with the time axis. For example, I want to use the hours in the changing time to calculate, for example, h=h-8. The hours in this calculation still change with the time axis. Thank you for your patient answer

NOTE: This topic continues in another thread:

1 Like