1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

NSImage

Posted by Tom Fuerstner on Jan 25, 2010

Hi,

I've recently developed a Jitter Connect Framework which enables me to send drawings done with Python/Pyobjc directly as native Jitter Matrix into Max/MSP Jitter.

Now, as Nodebox is really my favourite graphic toolbox I would like to send Nodebox drawing directly into Jitter instead of saving them as files. Getting Nodebox as a powerful and native Vector-Graphics-Engine inside of Max is a beautiful thing.

To accomplish this task all there is to know is how to get all the Grobs or the Canvas turned into a NSImage instead of saving them to disk. How is it possible to do this?


The exemplified code is showing how this is achieved directly with "pure" :

import objc
from AppKit import *
 
objc.loadBundle( "JitterConnector", globals(), bundle_path=objc.pathForFramework( "~/Library/Frameworks/JitterConnect.framework" ) )
print JitterConnector
 
jc = JitterConnector.alloc().init()
print jc.connectToHost_port_('127.0.0.1', 7474)
 
 
font_name = "Arial"
font_size = 76
message = "Hi Max, now you get Nodeboxified ;-)"
 
app = NSApplication.sharedApplication() 
 
newImage = NSImage.alloc().initWithContentsOfFile_('fb8.jpg')
newImage.lockFocus()
 
 
text_attributes = NSMutableDictionary.alloc().init()
font = NSFont.fontWithName_size_(font_name, font_size)
text_attributes.setObject_forKey_(font, NSFontAttributeName)
text_attributes.setObject_forKey_(NSColor.blackColor(), NSForegroundColorAttributeName)
 
 
message_string = NSString.stringWithString_(message)
size = message_string.sizeWithAttributes_(text_attributes)
point = NSMakePoint(10, 10)
message_string.drawAtPoint_withAttributes_(point, text_attributes)
 
 
newImage.unlockFocus()
tiffData = newImage.TIFFRepresentation()
bitmap = NSBitmapImageRep.alloc().initWithData_(tiffData)
    
fileType = NSPNGFileType
imageData = bitmap.representationUsingType_properties_(fileType, None)
 
 
jc.sendMatrix_(newImage)
 
jc.disconnect()


 
Posted by Tom on Jan 25, 2010

....and I should also show the interfaces for the Jitter Connect Framework:

@interface JitterConnector : NSObject {
	int sockfd;
}
 
- (void)sendMatrix:(NSImage *)image;
- (void)sendMessage:(NSString *)message;
- (int)connectToHost:(char *)host port:(int)port;
- (void)disconnect;



Posted by Tom on Jan 25, 2010

Ok, got it by reading the source code from trunk ;-)

img = canvas._nsImage



Posted by Tom on Jan 25, 2010

Finally. there's an image to watch: http://www.flickr.com/photos/39354298@N06/4304146997/