'''MaskDown: Acorn filter to apply the next lower layer as a clipping mask on the current layer, replacing the current layer with the result. Honors the place where your layer and mask become one. Namaste. ''' __author__ = 'Daniel Sandler ' __date__ = '1-OCT-2007' __version__ = '0.1' __license__ = 'BSD' # see __full_license__ at end of file __URL__ = 'http://toastycode.com/acorn' import objc from Foundation import * from AppKit import * ACScriptSuperMenuTitle = 'Masking' ACScriptMenuTitle = "Apply Layer Below as Mask" ACShortcutKey = 'm' ACShortcutMask = "command control" CIColor = objc.lookUpClass('CIColor') CIFilter = objc.lookUpClass('CIFilter') def main(image): #image is a CIImage doc = NSDocumentController.sharedDocumentController().currentDocument() layers = doc.layers() curLayer = doc.currentLayer() curLayerIdx = layers.index(curLayer) maskLayerIdx = curLayerIdx + 1 if not 0 <= maskLayerIdx < len(layers): NSRunCriticalAlertPanel("No available mask layer", "Please create a layer below this one to use as a mask.", "OK", None, None) return None maskLayer = layers[maskLayerIdx] if not hasattr(maskLayer, 'CIImage'): NSRunCriticalAlertPanel("Mask layer not rasterized", "The mask layer is a Text or Shape layer; please convert it to pixels to use it as a mask.", "OK", None, None) return None mask = maskLayer.CIImage() compose = CIFilter.filterWithName_('CISourceInCompositing') compose.setDefaults() compose.setValue_forKey_(mask, 'inputBackgroundImage') compose.setValue_forKey_(image, 'inputImage') # nothing happens if we return the image here, but if we set the image to # the layer explicitly, things work # see: http://flyingmeat.com/board/viewtopic.php?t=1241 curLayer.setCIImage_usingUndo_(compose.valueForKey_('outputImage'), True) return None __full_license__ = '''\ Copyright (c) 2007, Daniel Sandler. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. '''