Python COP Filter Example Broken

   2344   3   2
User Avatar
Member
8 posts
Joined: Dec. 2017
Offline
A number of the examples for writing COP filters/generators in Python seem to be completely broken in at least the latest version of Houdini (haven't ever tried them before).

Notable example:
import numpy

def output_planes_to_cook(cop_node):
    return ("C",)

def required_input_planes(cop_node, output_plane):
    if output_plane == "C":
        return ("0", "C")
    return ()

def cook(cop_node, plane, resolution):
    input_cop = cop_node.inputs()[0]

    # Grab the pixels from the corresponding plane in the input, then build
    # a numpy array from the data.
    pixels = numpy.frombuffer(
        input_cop.allPixelsAsString(plane), dtype=numpy.float32).reshape(
        resolution[1], resolution[0], 3).copy()

    # Use numpy to scale all values by the brightness.
    pixels *= cop_node.evalParm("bright")

    # Store the contents of the numpy array back into the pixel data.
    cop_node.setPixelsOfCookingPlaneFromString(pixels.data)

This fails on the
reshape
line because the input pixel buffer is the incorrect resolution.

I've noticed the “cook” method is called twice for each node, once for the actual plane data and a second time at 1/10 resolution. This second invocation creates the error because resolution,resolution are 1/10th the size of input_cop.allPixelsAsString(plane).

The error then is returned as:
Traceback (most recent call last):
File "", line 1, in
File "", line 18, in cook
ValueError: total size of new array must be unchanged

Are there any decent examples for a default python COP filter? Do I actually have to resize the input cop pixels to match the intended plane resolution every time I run a cook?
User Avatar
Member
3 posts
Joined: Oct. 2014
Offline
Same problems here, latest H17 indie stable build.

This feature seems pretty promising! especially for Numpy image processing.

The following code doesn't work in Python COP :

pixels = numpy.frombuffer(
input_cop.allPixelsAsString("C"), dtype=numpy.float32).reshape(
input_cop.xRes(), input_cop.yRes(), 3).copy()

But seems to work in a Python SOP node (input_cop pointing to the COP node). Don't know if it can help to fix.
FX TD @ Unit Image (Paris) / 3D Scan Specialist.
User Avatar
Member
34 posts
Joined: July 2015
Offline
6 years later it's still broken
User Avatar
Member
5 posts
Joined: Aug. 2020
Offline
Reshape issue caused by wrong dtypepassed to np.frombufferfunction.

According to documentation you need to implement depthmethod to make it float32
https://www.sidefx.com/docs/houdini/hom/pythoncop.html#writing-cop-filters [www.sidefx.com]
  • Quick Links