Sketch Watching and Live Coding

Martin Prout edited this page Mar 21, 2017 · 41 revisions

Automatically Reload your Sketch with rp5 watch

rp5 watch will keep an eye on the source file of your sketch. Whenever you save a change to it, it'll reload your running code, so you can try out your ideas quickly. rp5 watch is the best way to quickly prototype a sketch. To guard against you running watch in a top level folder we limit the number of files to watch to 20, you can set a different limit in ~/.rp5rc MAX_WATCH: 100 (pure yaml) or "MAX_WATCH": 100 (json format).

TIP

If you declare constants in your sketch you may wish to define the thusly CONSTANT ||= 'some value' to avoid getting warning: already initialized constant when the sketch re-loads.

Live Coding

Ruby-Processing is a playground for live coding. Sketches can be loaded into an interactive code session (pry) using rp5 live. Once your sketch is running, the full powers of Ruby metaprogramming are there for you to use. Methods and classes can be redefined on the fly, arguments passed, values changed and all that.

>> rp5 live Example*/samples/contributed/jwishy.rb
[1] pry(#<Sketch>)> def bluish
[1] pry(#<Sketch>)*   sin(y_wiggle)
[1] pry(#<Sketch>)* end  
=> :bluish
[2] pry(#<Sketch>)> @back_color = 0.5, 0.5, 0.5
=> [0.5, 0.5, 0.5, 0.5]

This opens up the Wishy Worm example in a window, and redefines the bluish method to return a function of the y_wiggle. The blue begins to pulse. We then set background color by changing the `back_color variable, you also need to move alpha slider to 0.03 or so to get the following look to your sketch:-

jwishy pic

To view a bunch of methods available ls in the pry console, pry aficionados will know more.

NB: you should be aware that bare 'sketches' cannot be listed in pry console with $, instead the super class gets listed (but to be honest you are not going to miss this function). However if you really want the sketch to be listed in the usual way with $ just wrap the sketch as a class eg:-

class JWishy < Processing::App
#... bare sketch code
end

Now what if you have a complex method eg draw and you don't want to type it all out again the magic of pry edit to comes the rescue:-

[3] pry(#<Sketch>)> edit -p Sketch#draw

This opens up your favourite editor for you to edit the method, and on exit you return to the running sketch.

TIP: To make sure you use the editor you want eg vim add this to your .pryrc. Pry.config.editor = "vim", for JEdit or Emacs use "jedit" or "emacsclient" emacs users will know more, to return to the running sketch with jEdit save and close sketch for Emacs Ctrl x # and save no need to close editor for vim write and save :wc...

Also remember you can easily start the live mode from vim :!rp5 live %, further you will find your edits get saved for you to return to...

Note that changes made to jwishy.rb are only made on the in-memory version of it. But if that's what you want, use ruby-processing watch mode instead of live.

There are a couple of videos to get you inspired pry video by Josh Creek and live video by Martin Prout