Export contents of a UI object


    Nov 05 2014 | 7:49 pm
    Dear Devs,
    I'm wondering if you had an elegant workaround for this problem: assuming that you have an UI object with a rather complex paint method, is there a simple way to export the rendered graphics to a file?
    The ideal case would be if there was a way to create a jpatcherview object manually and then invoke the paint method on it, but I think that's not possible. On the other hand, I'd like to avoid refactoring my paint method into pieces of code which don't rely on an actual patcherview (as my paint method is using many box layers, this would be rather inefficient anyway).
    Any ideas?
    Thanks, Ádám
    P.S. BTW, I guess the best I could achieve is a PNG export at the end of the day, but if you knew of any trick to export the graphics into an SVG file, don't keep it for yourself. ;-)

    • Nov 05 2014 | 8:25 pm
      Hi Adam,
      yes, the best I can do is to export your UI graphics as a PNG file. I wouldn't really know how to perform an SVG export but if anybody knows, please share...
      The patcherview object has an A_CANT method called "getimage" with the following prototype:
      t_jsurface *patcherview_getimage(t_object *patcherview, t_rect *rect);
      
      A few lines of code are better than a thousand words:
      patcher = jbox_get_patcher((t_object *)x);
      patcherview = jpatcher_get_firstview(patcher);
      jbox_get_rect_for_view((t_object *)x, patcherview, &rect);
      surface = object_method(patcherview, gensym("getimage"), &rect);
      
      now that you have the graphics as a surface, you could usejgraphics_image_surface_writepng() to write the surface to a file.
      Cheers
      - Luigi
    • Nov 05 2014 | 10:32 pm
      Hi,
      thanks for the fast reaction.
      Indeed, jpatcher_get_firstview is the best way to go if you trust the user that the object is in a patcher which is not zoomed etc when the export command is to be executed. However, I'm wondering if there was a way to create a "hidden" jpatcherview instance where I could have full control of the patcherview without needing to deal with the user's current settings (ideally, I'd even want to control the actual dimensions of the view, but that's perhaps too much to ask for ;-) ).
      Cheers, Ádám
      P.S. I really didn't know about the patcherview_getimage method. If nothing else works, I'll go that way. Thanks.