HOM Cookbook Nodes and wiring

Overview

This example module contains objects that generate compositing networks based on methods. As you call the methods, it creates compositing (COP) nodes, sets parameters on them, and wires the nodes together.

Sample Usage

>>> importcomp# Load in $HFS/houdini/pic/default.pic and convert it to a jpeg, writing the# output to the current directory.>>> comp.readFile("default.pic").writeFile("default.jpg")# Load in a sequence of images (from $HFS/houdini/pic), brighten them,# and convert them to jpegs.>>> comp.readFile("butterfly$F.pic").bright(1.8).writeFile("butterfly$F.jpg")# Load in default.pic, brighten it, composite it over a gray background,# and write it to out.pic.>>> comp.readFile("default.pic").bright(1.2).over(... comp.constant(0.3,0.3,0.3)).writeFile("out.pic")...

Implementation

importhou"""This module lets you create and evaluate a compositing network simplyby writing an expression describing the compositing operations to perform.With some simple extensions to this example, you can create a Pythonicequivalent of Houdini's icomposite program.For example, you can write: import comp comp.readFile("default.pic").bright(1.2).over(comp.constant(0.3, 0.3, 0.3) ).writeFile("out.pic")and Houdini will build a composite network that loads the default.pic image,brightens it, composites it over a constant, and writes out the result toout.pic.Note that this module supports compositing over a sequence of images: simplyuse a time-dependent expression (like $F) in the input and output image names.If you use this module from a graphical Houdini session, you can inspectthe compositing networks it creates."""deftest():"""This function creates a simple test case that evaluates the following: comp.readFile("default.pic").bright(1.2).over( comp.constant(0.3, 0.3, 0.3)).writeFile("out.pic") """readFile("default.pic").bright(1.2).over(constant(0.3,0.3,0.3)).writeFile("out.pic")class_Image:"""This image class wraps a COP node and exposes image operations via methods that simply create COP nodes and return a new image wrapping that node. """def__init__(self,node):# The node parameter is a COP node. The user of this module will# create images with the readFile and constant methods, and construct# _Image objects directly.self.node=nodedef__createNode(self,type):# Create and return a COP node of the specified type in the current# network.returnself.node.parent().createNode(type)defbright(self,amount):"""Brighten the image, returning a new image."""n=self.__createNode("bright")n.setFirstInput(self.node)n.parm("bright").set(amount)return_Image(n)defover(self,image):"""Composite this image over the specified one, returning a new image."""n=self.__createNode("over")n.setFirstInput(self.node)n.setInput(1,image.node)return_Image(n)defwriteFile(self,file_name):"""Write this image to a file or file sequence."""n=self.__createNode("rop_comp")n.setFirstInput(self.node)n.parm("copoutput").set(file_name)self.node.parent().layoutChildren()# If we're called from a standard Python shell or hython, actually# write out the file.ifhou.applicationName()=='hbatch':n.render()def__network():# This internal function just returns the COP network. For this example,# it simply hard-codes a particular network.returnhou.node("/img/comp1")orhou.node("/img").createNode("img","comp1")def__nodeResolution(node):"""Use the hscript res() expression to return the a composite node's image resolution."""return(hou.hscriptExpression('res("%s", D_XRES)'%node.path()),hou.hscriptExpression('res("%s", D_YRES)'%node.path()))_lastResolution=NonedefreadFile(file_name):"""Return an image object corresponding to a file or file sequence."""n=__network().createNode("file")n.parm("filename1").set(file_name)# Remember the image resolution. If we later create a constant color,# we'll use this resolution.global_lastResolution_lastResolution=__nodeResolution(n)return_Image(n)defconstant(r,g,b,a=1.0):"""Return an image that's a constant color. The size of the image will be the same as the size of the last file read in."""n=__network().createNode("color")n.parmTuple("color").set((r,g,b,a))if_lastResolutionisnotNone:n.parm("overridesize").set(True)n.parmTuple("size").set(_lastResolution)return_Image(n)