Example 1: searching for nodes using a custom filter

The section Finding nodes included an example of searching for a node in a stream using the type name of the node as the search criterion. In some situations, a more generic search is required and this can be implemented using the NodeFilter class and the stream findAll() method. This kind of search involves the following two steps:

  1. Creating a new class that extends NodeFilter and that implements a custom version of the accept() method.
  2. Calling the stream findAll() method with an instance of this new class. This returns all nodes that meet the criteria defined in the accept() method.

The following example shows how to search for nodes in a stream that have the node cache enabled. The returned list of nodes could be used to either flush or disable the caches of these nodes.

import modeler.api

class CacheFilter(modeler.api.NodeFilter):
	"""A node filter for nodes with caching enabled"""
	def accept(this, node):
		return node.isCacheEnabled()

cachingnodes = modeler.script.stream().findAll(CacheFilter(), False)