Using an observer to delete a screen note
-
Hello -
I'm trying to write a plugin that will always display the current shadow time as a screen note.
I have the [TBD] Time plugin from Smustard, and it doesn't quite do what I need. (Because [TBD] Time updates at its smallest interval of 1 second, frames that are exported for use in an animation do not display the correct time).
I'm also aware of the reported bug of the incorrect shadow time being returned from the console. (Assume that doesn't matter here.)
So, here's my stab at the script that will do what I want. (This is really bits of code from other SU users that I've cobbled together here)
What this script currently does, is it places a screen note with the current shadow time any time any shadow attribute is changed. After a few changes, the notes become unreadable because each note is placed on top of the one placed before. What I would like to add, is that whenever the shadow attribute is changed, the previous note is deleted, and the current shadow time is placed as a new screen note.
Any suggestions?
class SO < Sketchup;;ShadowInfoObserver def onShadowInfoChanged(info, type) model = Sketchup.active_model stime = model.shadow_info["ShadowTime"].gmtime model.add_note stime.to_s, 0.8, 0.9 end end ShadowInfoWatcher = SO.new Sketchup.active_model.shadow_info.add_observer(ShadowInfoWatcher)
-
The trick is to assign the note to a variable when you make it. Then you can delete it the next time the method is run. So all I've done is made it delete the note if it exists, then when the new note is made, I have made a variable to point to the note object so it can be deleted on the next run.
class SO < Sketchup;;ShadowInfoObserver def onShadowInfoChanged(info, type) model = Sketchup.active_model model.entities.erase_entities(@mod_note) if @mod_note stime = model.shadow_info["ShadowTime"].gmtime @mod_note = model.add_note stime.to_s, 0.8, 0.9 end end ShadowInfoWatcher = SO.new Sketchup.active_model.shadow_info.add_observer(ShadowInfoWatcher)
-
Consider using Text.set_text also.
-
Thanks, Jim and Chris.
Would using Text.set_text be more efficient for this use.
I may have to rethink the structure of this plugin anyway, as it doesn't do what I was hoping.
The function I describe above works great: when run, this plugin creates a dynamic screen note that always displays the current shadow date and time.
What I do next, is create a large number of scenes with a different shadow position. I then export these scenes to an animation. Whether I export to jpg or an avi, the screen note for each scene is the same note that is displayed when I execute the animation export. The screen note remains unchanged for the duration of the animation.
Chris, you mentioned in another thread (http://forums.sketchucation.com/viewtopic.php?f=323&t=28307) that setting an animation up like this would require the creation of a screen note for each scene, which is only visible for that scene. Is this the only way to accomplish what I'm trying to do?
-
Yes, I think so. When SU renders it animations (either a video format like .avi or as jpg stills), it does not run your observer like I think you were hoping it would do. So in my mind there are a few ways to tackle this, but all essentially do the same thing. Add a note for each scene.
If I were to write it, I think I would try to set it up so that the user only had to set up their desired scenes. Then I would write a custom animation jpg exporter. It would work so that it reads how many seconds per scene the user has set up. Then it would ask how many frames per second the user wants. It would then setup a camera for the first frame, and it would have to change the shadow info from frame to frame also, and then it would add the note. Then once it has the view set, the shadows set and the note added, it would move on to the next frame. And start over setting all that info again.
Setting the camera for each frame is actually easy. The API lets you get that info already. You already know how to add and remove and update the screen text. so that would be easy enough. the hardest part would be figuring out the shadow info from frame to frame. That would require getting the starting scene's shadow info and the next scene's shadow info, then divide the time transition up over how many frames there between the scenes, then change the model shadow info to amtch what you have calculated. Its not impossible at all, but it would make my head hurt I'm sure.
Then its just a matter of exporting the images, which is fairly straightfoward.
That is how (I think) I would tackle it if I really wanted to do a thorough job.
Chris
-
Hmm. As always, thanks for the detailed thoughts.
I think I'll attack this from the angle of putting a screen note on each scene. I think this will be the cleanest approach for my application, as I have not been using transition times or scene delays. This will, I think, eliminate the issue of needing to retrieve the next scenes shadow info during the rendering process.
I do like the little snippet we've developed here. And, if I may say so, I think it's a cleaner approach than the [TBD] Time plugin on smustard (no offense to the author; I've appreciated and studied the plugin). The reason I like this approach more is that is updates instantaneously instead of at one second intervals. It also doesn't update until there is actually a change in the shadow info. If I add a on/off switch, and a few options to choose the format of the note, it will be a fully functioning utility.
I am running into a problem with this script, though. When I start SketchUp from scratch, in the first model I work on, the script works great. However, if I open an existing model, or create a new model while SketchUp is running, the script does not work in either case. Is there any obvious reason why this would be the case?
-
The ShadowInfo is part of the model, so when you open an existing model or start a new model, you need to attach your observer to the new ShadowInfo.
You can automate this by using an AppObserver to notify the script when a model is opened or created, and attach the ShadowInfoObserver at that time.
Note on Macs, you can have more than a single Model open. Not so on Windows where there is always only a single Model available.
-
Wouldn't this be a perfect job for the FrameChangeObserver ??
-
I don't think the FrameChangeObserver works either when exporting an animation.
-
That's interesting. I've never noticed that observer (I tend to stay away form them after hearing all of Thom's problems with them). That seems like it would be a great one to use here if only it worked during animation export.
Chris
-
Hello,
it's a pity that I read this post just now, when I finished my little plugin using the shadowinfo_observer to achieve exactly what danbig described. Display the Shadowtime for different shading situations.
My Plugin works perfectly. I was proud of it until I tried an animation export
Is this problem solved by now?
It seems not very sophisticated to me, to write notes on every page which are hidden when another page is displayed...
is there no other way ?Matthias
-
@chris fullmer said:
That's interesting. I've never noticed that [FrameChangeObserver observer] ...
Yes that is because is one of the API missing class boo-boos. It IS mentioned within the API, in passing.
Pages.add_frame_change_observer
Pages.remove_frame_change_observerIt was featured (documented) in the Sketchup API BlogSpot:
Dynamic Components that react to scene (aka page) changes('scenes.rb') Code properly formatted (by Todd Burch) at:
http://www.smustard.com/forum/viewtopic.php?f=9&t=25It basically only has one event callback:
frameChange( fromPage, toPage, percent_done ),
the intialize method, plus any custom method(s) you need inside to keep your code readable and organized.@chris fullmer said:
That seems like it would be a great one to use here if only it worked during animation export.
I guess the animation export is a manual 'built-in' C++ side plugin? And as such all Ruby processing stops? (..similar to what happens when the App Modal 'Preferences' dialog opens.)
.. or is it a special way of running an Animation object? -
I, for one, have decided that I have a knack for attempting the most complicated tasks as my "test project" in a new area.
Matthias: I'm sorry that you got so far before realizing the issue with the shadowinfo_observer. This is a lesson that I've learned too many times: test code at the smallest possible snippets, and verify it's functionality individually before putting it all together into a larger code block.
-
@danbig said:
I, for one, have decided that I have a knack for attempting the most complicated tasks as my "test project" in a new area.
danbig,
It's dawning on me that this might be true for me tooI try the approach with one note per page. Hopefully this will be more successful
Matthias
-
It really is not all that difficult to make your script export a series of jpgs that can then turned into a movie (its more dificult for the user though ). But if you feel that is an option, you could try writing your own export method and then you can update the screen text for each frame easily that way.
Chris
-
@chris fullmer said:
It really is not all that difficult to make your script export a series of jpgs
ChrisChris,
can I also export directly into an avi file somehow ?
To make it a bit easier for the user ...Matthias
-
@chris fullmer said:
It really is not all that difficult to make your script export a series of jpgs that can then turned into a movie (its more dificult for the user though ). But if you feel that is an option, you could try writing your own export method and then you can update the screen text for each frame easily that way.
Chris
"Difficult" being a relative term here.
-
Oh I agree. But really exporting an image is not that hard. For me, the hardest part BY FAR was getting it recognize the location I was trying to save it. I really struggle with ruby and files/paths. Its is remarkably painful. But you might be able to use the save dialog box and let the user supply the path easier than hard coding like I think I did.
Chris
-
@pvbuero said:
@chris fullmer said:
It really is not all that difficult to make your script export a series of jpgs
can I also export directly into an avi file somehow ?
To make it a bit easier for the userA thot...
.. I wonder if MS MediaPlayer or MovieMaker or PhotoStory could be run in batch mode, to stitch a collection of image files (in a folder,) into a AVI, by calling the exe with command line parameters.
Many of the old SlideShow executables would do similar for a slideshow. (Which is now built into XP.)
If not, perhaps out there somewhere, is a command line "AVI maker" executable that would do the job.
-
Call me unambitious, but I'm back to the strategy of placing the screen note on each scene, and hiding it on all the others. It would work for my workflow, and it seems digestible for me.
I could then use the standard "export to animation" function to either jpg or avi, and (I think) it would come out as expected.
Advertisement