ruby-processing / propane

A simpler ruby-processing for linux/Windows/MacOS?
https://ruby-processing.github.io/propane
GNU General Public License v3.0
27 stars 1 forks source link

irb? #6

Closed jtoy closed 7 years ago

jtoy commented 7 years ago

Is it possible to do image drawing with irb? I realize that you have to subclass Propane::App normally, but if we want to do more experimentation in realtime, is there a way we can draw to the screen for each line we enter in irb?

monkstone commented 7 years ago

There isn't anything like that at present I guess something along lines of https://github.com/fjenett/simplelivecoding might work or some client server type thing like https://github.com/jaymcgavren/amqp-processing might be interesting. Currently the best I have to offer is live editing in vim or emacs using pry. Create some dummy sketch eg Fred.rb

propane -c Fred 200 200

Fire up pry using

jruby -e "require 'pry'; binding.pry"

I create a shortcut by using jpry alias in my .bashrc

[1] pry(main)> load 'fred.rb'
=> true
[2] pry(main)> edit -p Fred#draw

Editor comes up allowing you to edit the draw method sketch redraws on :wq vim, with emacs I think you can use server mode so you do not need to close the editor see https://github.com/jashkenas/ruby-processing/wiki/Sketch-Watching-and-Live-Coding for links to setting up vim and emacs for live editing with pry.

monkstone commented 7 years ago

Hey @jtoy this looks interesting too http://www.praxislive.org/ because as Neil C Smith realised there are some fundamental problems with vanilla processing.

jtoy commented 7 years ago

@monkstone im revisiting this again. I dont actually need livecoding, I just want to be able to render the images from the command line . I am making lots of images inside a live process.I am using a web server that passes some parameters and then that image gets rendered back to the webserver.

jtoy commented 7 years ago

I got this working, can share if you think useful

jtoy commented 7 years ago

hmm, it actually doesn't work properly, I think it is because of propane, but it might be jruby, I cant really tell what is going on with this code.

monkstone commented 7 years ago

@jtoy I'm not sure what you are trying to do, but I am willing to help. I created this template sketch see gist https://gist.github.com/monkstone/fefb635ac0fb1a9f29f25719ed28aa27 for use with jirb. At jirb prompt you load the sketch as follows, create a new instance, then change the value of the variable back on the fly, frames get sequentially numbered and saved to data folder.

irb(main):001:0> load 'fred.rb'
=> true
irb(main):002:0> app = Fred.new
=> ##, "key"=>#, "frameRate"=>#, "mousePressed"=>#, "keyPressed"=>#}, @options={}, @render_mode=nil>
irb(main):003:0> app.back = app.color(200, 0, 0)
=> -3670016
irb(main):004:0> app.redraw
=> nil
irb(main):005:0> app.back = app.color(0, 0, 200)
=> -16777016
irb(main):006:0> app.redraw
=> nil
irb(main):007:0> 

May be create_graphics is interesting to you, if so see try this example https://gist.github.com/monkstone/40944916940eff071a2a7bb661779170 that make use of JRubyArt / propane grid function.

jtoy commented 7 years ago

@monkstone Thanks for your demo code, I will test it out. In the meantime I wanted to show you the version I came up with that is close but doesnt exactly work. Here is the code: https://gist.github.com/jtoy/94a8b95b5b65552f6bdbed98fda8b355 I have 2 issues,
1) I run the program like this: " jruby cli.rb 'rect(0,0,200,200)'"
It seems like a new thread is created because the save function doesnt actually save a frame until I completely leave the app, but I need the saved jpg so I can modify it while my program is running. I know this because the line File.exists?(path) always returns false, but after I actually fully exit the program, the file exists.
the program doesnt actually end unless I put an exit as the last line.

2) the other issue is I need to be able to hand bad code, so if I run " jruby cli.rb 'rect(0,0,200,200)badcode'" it just gets stuck here: running code: rect(0,0,200,200)badcode /tmp/cli/1497028789_47955901361083011988817431558722694170.jpg false org.jruby.exceptions.RaiseException: (SyntaxError) (eval):1: syntax error, unexpected tIDENTIFIER rect(0,0,200,200)badcode at org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1000) at cli.setup(cli.rb:11)

I cant actually catch the error. I need to have some logic in my code to know if the code I am running is bad. How can I know from my own code if the program didnt work?

Both of these errors seem to happen because it looks like I;m not fully understand how propane works and am not doing this properly, it seems propane does its work in another thread. I'll go integrate your changes now and see if that works ,thanks for the feedback!

monkstone commented 7 years ago

In propane we explicitly require a settings method, where we define size and mode of the sketch. Vanilla processing hides this from the user by doing some pre-processing, where size, smooth and hidpi get moved from setup into settings before the java code gets compiled. You can use no_loop to stop the draw loop continuously looping, and use redraw to get a sketch to refresh, but basically you should consider that you are stuck in the draw loop (so can't call back to setup, but you could use a method that can be called in setup and draw.

In my case the black, red and blue background sketches are saved without exiting the sketch. In JRubyArt and vanilla processing we support a single line of code eg background 0 as a valid sketch, but of course under the hood we create a class wrapper (using default width and height) to produce a static sketch (no draw loop), but we also support a draw loop for the likes of code golf https://monkstone.github.io/jruby_art/update/2015/10/03/golf.html.

monkstone commented 7 years ago

Regarding error in code I use atom with script plugin and got following with my modified code https://gist.github.com/monkstone/9780d9e148a459e58f5469ec6495ba2f so it is a type error, perhaps best to initialize code @@code as empty string?

/tmp/1497032475_56731685601283244702893565345489142558.jpg
false

running code: 
org.jruby.exceptions.RaiseException: (TypeError) no implicit conversion of nil into String
    at org.jruby.RubyKernel.eval(org/jruby/RubyKernel.java:1000)
    at home.tux.jtoy.setup(/home/tux/jtoy.rb:17)

It is unfortunate that with jruby error messages can be confusing.

jtoy commented 7 years ago

I am testing out your other example now, regarding your comment on the error code, I have never gotten the "implicit conversion of nil into String" issue, but I changed it to your suggestion of initializing @@code. I see that you still get the false appearing even though the save call was ran.

monkstone commented 7 years ago

For propane I had not really considered using command line parameters in the sketch, it was a feature of the original ruby-processing where hash options could be fed in. In JRubyArt I provide a post_initialize hook method to provide the user with customizable options https://github.com/ruby-processing/JRubyArt/blob/master/lib/jruby_art/app.rb, but not for propane.

jtoy commented 7 years ago

hi @monkstone , I tried incorporating your suggestions, but I still always get File.exists?(file) == false. Here is my newest version: https://gist.github.com/jtoy/f758af8e249e332150488fc46b9c0565

Im not sure what else I need to incorporate to make it work.

jtoy commented 7 years ago

I got it all working, but I think its very bad code, I have to use a while/sleep loop, is there a better way to do this without calling sleep? https://gist.github.com/jtoy/efafa693d5a88ddeab3e58bb30863906

monkstone commented 7 years ago

@jtoy progress may'be, experiment with wait length to optimize wait length? Read this http://kares.org/jruby-ji-doc/ Synchronization towards bottom of article. And modified code thus https://gist.github.com/monkstone/99d1111b4d77821406be83664cc583e9