Launch animation
-
Hello
Can anyone tell me, in ruby, how to launch animation (like using sketchup standard function) to show each pages of the model ?
The idea is to make a toolbar that allow you to play, pause and stop the animation.
Thanks for your help.
-
Read the API - you need to combine various methods into a new toolset.
Note that 'scenes' are referred to as 'pages' in the API...
https://developers.google.com/sketchup/docs/ourdoc/pages
https://developers.google.com/sketchup/docs/ourdoc/page
https://developers.google.com/sketchup/docs/ourdoc/animation
https://developers.google.com/sketchup/docs/ourdoc/sketchup#send_action [page... actions]
etc etc... -
Hello TIG,
Thanks for your answer, mainly for the last link, which I didn't read yet.
But the issue I have is to keep the animation running.
With my code below, I made a class which show scenes page by page
But the animation stop when I change the page (pages.selected_page = model.pages[$xg])
Also I don't know if switch time between pages will be as per value defined in sketchup parameters...Any idea, or other way to proceed ?
class Anima
def initialize
$xg = 0
enddef nextFrame(view)
pages = Sketchup.active_model.pages
model = Sketchup.active_model
#UI.messagebox $xg
$xg+=1
if $xg > pages.count then
$xg = 0
end
pages.selected_page = model.pages[$xg]
#view=Sketchup.active_model.active_view
#view.show_framereturn true
end
def stop
UI.messagebox "fin"
end
end #class -
A few points...
Use @xg which is specific to that instance of your class... and NEVER use $xg which would be global to ALL scripts loaded...
To keep it going reset@xg=0 if @xg>pages.length-1
- so it never ends... remember that an array starts at 0 so you need the -1 when doing a comparison ..... -
This proposed feature by inteloide should be integrated by default in the next Sketchup version. Having to click three times to play the animation is no good so I'll be keeping an eye when this plugin is done
-
ok, by changing to :
@xg=0 if @xg>pages.size-1all pages are activated, but without any break between each of them. (it's like it goes directly to the last page). How to implement a break ?
Also it doesn't loop : when the last page is shown, it stops (doesn't loop). Any reason why ?Thanks again.
I will post my complete plugin when finish. Actually it's based on mover.rb plugin (to move components between pages) but I wanted to add a toolbar to have all tool within on click. -
Read the Page section of the API.
page.delay_time
gives the time to delay between pages [there is alsotransition_time
], you could try using it/them between page changes, using aUI.start_timer(...){...}
for each page ? [untested]...
If it's -1 you need to also get the model's page-options to find what the default value is...
Sketchup.active_model.options["SlideshowOptions"]["SlideTime"]
Sketchup.active_model.options["PageOptions"]["TransitionTime"]
etc -
Thanks TIG for telling me to look at Page API, because I thought it was only a collection (Pages) and not an entity.
So at the end, the solution I found is the following one :
def lire_anim (p) model = Sketchup.active_model pages = model.pages pages.selected_page = pages[p] if p<pages.size-1 then id = UI.start_timer(Sketchup.active_model.options["PageOptions"]["TransitionTime"], false) { lire_anim(p+1) } end end
and to launch : lire_anim(0)
Now, I need to find a way to stop animation while it's running...
-
You also need to understand
view.animation=
etc...
https://developers.google.com/sketchup/docs/ourdoc/view#animation=
Add this into your script's methodsanimation = Anima.new() model = Sketchup.active_model view = model.active_view anim = view.animation=animation if (anim) UI.messagebox anim else UI.messagebox "Failure" end
To cancel the current view's animation use
view.animation=nil
-
I'm close to the aim !
I have 2 issues :
1-When start the animation it goes to "stop" subrouting but it doesn't stop animation ...?!
2-When setting view.animation to nil, it' doesn't stop... ?!Again your help is welcome !
class Class_Run_Animation def initialize puts "Start of animation" show_scene(0) end def show_scene(p) puts "Go to scene #{p}" pages = Sketchup.active_model.pages pages.selected_page = pages[p] if p<pages.size-1 then id = UI.start_timer(Sketchup.active_model.options["PageOptions"]["TransitionTime"], false) { show_scene(p+1) } return true else puts "End of loop" return false end end def stop puts "Loop stopped" return false end end
-
You loop through the pages and then
return false
, this stops it, you never call stop, and don't really need to...
You should only use theinitialize()
method to set up@
variables etc, if needed...
You have dropped thedef nextFrame(view)...
Why? I think this is confusing the issue... Using it would let you create an animation class object that you could assign to the view [looping through the scenes [pages] etc] in some separate code withview.animation=Class_Run_Animation.new()
, then you can later cancel withview.animation=nil
.
The separate code used would be something like:module Inteloide def self.start_animation() Sketchup.active_model.active_view.animation=Class_Run_Animation.new() end def self.end_animation() Sketchup.active_model.active_view.animation=nil end end
Used in the console [or added to a toolbar command] as
Inteloide.start_animation
and
Inteloide.stop_animation
Your earlier code could be readily adapted to do this... -
ok, I changed to an animation class, but it still don't stop
I think the issue comes from the start_timer which start the procedure even if animation is stopped.` class Class_Run_Animation
def initialize
puts "Start of animation"
#show_scene(0)
@p=0
enddef nextFrame(view)
puts "Go to scene #{@p}"
pages = Sketchup.active_model.pages
pages.selected_page = pages[@p]
if @p<pages.size-1 then
@p+=1
id = UI.start_timer(Sketchup.active_model.options["PageOptions"]["TransitionTime"], false) { nextFrame(view) }
return true
else
puts "End of loop"
return false
end
end
endmodule Inteloide
def self.start_animation()
Sketchup.active_model.active_view.animation=Class_Run_Animation.new()
end
def self.stop_animation()
Sketchup.active_model.active_view.animation=nil
end
end` -
You've made it iterate itself!
Try something like this...def initialize() @model=Sketchup.active_model @pages = Sketchup.active_model.pages @po=@model.options["PageOptions"] end def nextFrame(view) while view @pages.to_array.each_with_index{|page, i| next unless page.name==page.label ### (SKIP) puts page.name @pages.selected_page = @pages[i] UI.start_timer(@po["TransitionTime"],false){###}### wait for a time } end#while end
or similar...
-
Well, I'm lost ;o)
what is the aim of this loop :
@pages.to_array.each_with_index{|page, i|
next unless page.name==page.label ### (SKIP)what do you put instead of "###" in the code :
UI.start_timer(@po["TransitionTime"],false){###}
(I put nextFrame(view) because if not the animation doesn't continue) -
Maybe it's because I don't understand how sketchup makes the loop for an animation, what's happen after nextframe is called and run ? I'm lost in fact.... (
-
Maybe I confused you with the addition of
next unless page.name==page.label ### (SKIP)
All this does is NOT display the page IF the name and label don't match.
A page's name and its label are the same UNLESS the page has be marked NOT to be included in the animation in the Scene-Manager dialog - it then has the name labeled inside () - so you won't show it...
The ### is a 'rem' statement - a 'comment' after the code so what follows is ignored...The next part:
puts page.name
simply displays the name of the scene it's processing in the Ruby Console
Then:
@pages.selected_page = @pages[i]
sets the selected page to be the current pages 'item' [by index 'i'] in the list of pages...
Next:
UI.start_timer(@po["TransitionTime"],false){###}### wait for a time
The timer starts a pause - the rem ### at the end is as above...
ON reflection... I appreciate that this final part is wrong.
Because the timer then runs outside of the main loop !
Perhaps using:
sleep(@po["TransitionTime"])
instead, might work instead ?? This stop processing for that long before the next page is set 'selected'. -
Hi,
I didn't saw your answer but I produced this code...
I post it for your information...but I will work on your answerSo, my code, as it is now (please don't blame me using a global variable... )
` class Class_Run_Animation
def initialize
puts "Start of animation v1"
@p=0
@id=0
enddef nextFrame(view)
if $inteloide_cont==true then
puts "Go to scene #{@p}"
pages = Sketchup.active_model.pagespages.selected_page = pages[@p] if @p<pages.size-1 then @p+=1 @id=UI.start_timer(Sketchup.active_model.options["PageOptions"]["TransitionTime"]+Sketchup.active_model.options["SlideshowOptions"]["SlideTime"], false) { nextFrame(view) } return true else puts "End of loop" return false end else puts "stop animation" @id=UI.start_timer(1, false) { nextFrame(view) } end
end
endmodule Inteloide
def self.start_animation()
$inteloide_cont=true
Sketchup.active_model.active_view.animation=Class_Run_Animation.new()
end
def self.stop_animation()
$inteloide_cont=false
Sketchup.active_model.active_view.animation=nil
end
def self.pause_animation()
$inteloide_cont = !($inteloide_cont)
puts $bg_cont
end
end`
Advertisement