Python API 2.0 Reference
python/api1/py1NameFilter.py
1 # ===========================================================================
2 # Copyright 2018 Autodesk, Inc. All rights reserved.
3 #
4 # Use of this software is subject to the terms of the Autodesk license
5 # agreement provided at the time of installation or download, or which
6 # otherwise accompanies this software in either electronic or hard copy form.
7 # ===========================================================================
8 import maya.OpenMaya as om
9 import maya.OpenMayaMPx as omMPx
10 
11 # The pyNameFilter class is a cache configuration filter that looks at the
12 # dependency graph (DG) nodes relating to caching in order to match their
13 # name with a pattern.
14 #
15 # This filter requires a param string
16 # (newFilterParam from the cacheEvaluator command) formatted like so:
17 # "pattern,option"
18 # where
19 # pattern is the substring to search for in all the dependency nodes
20 # relating to the cache configuration
21 # option is optional. Can be one of the following string values:
22 # reverse: reverse logic match behavior: the filter will match on
23 # node names NOT containing the pattern
24 #
25 # It can be used to include or exclude nodes from caching modes based on
26 # their names when used with the cacheEvaluator command.
27 #
28 # Example:
29 # Use default evaluation cache configuration mode but exclude cones from
30 # the cache (Python):
31 #
32 # import maya.cmds as cmds
33 # cmds.loadPlugin( 'py1NameFilter.py' )
34 #
35 # # Reset Rules
36 # cmds.cacheEvaluator(resetRules = True);
37 #
38 # # Add Eval Cache action/filter
39 # cmds.cacheEvaluator(newFilter = 'evaluationCacheNodes', newAction = 'enableEvaluationCache' )
40 #
41 # # Add Custom Filter
42 # cmds.cacheEvaluator(newFilter = 'pyNameFilter', newFilterParam = 'Cone', newAction = 'disableAllCache' )
43 #
44 # See also the OpenMaya API documentation for the MPxCacheConfigRuleFilter
45 # proxy class
46 
47 def pyNameFilterCreator( param ):
48  # Parse the creation parameters here
49  if 0 == len( param ):
50  om.MGlobal.displayError( 'Cannot construct NameFilter object: empty parameter string' )
51  return None
52 
53  tokens = param.split( ',' )
54  if len( tokens ) > 2:
55  om.MGlobal.displayError( 'Cannot construct NameFilter object: the parameter string has too many tokens' )
56  return None
57 
58  # Get the pattern to match against
59  pattern = tokens[0]
60 
61  # Get the option
62  reverseLogic = False
63  if len(tokens) > 1:
64  if tokens[1] == "reverse":
65  reverseLogic = True
66  else:
67  displayString = 'Cannot construct NameFilter object: expected "reverse" for the second token in the parameter string, got "' + tokens[1] + '"'
68  om.MGlobal.displayError( displayString )
69  return None
70 
71  return omMPx.asMPxPtr( pyNameFilter(pattern = pattern, reverseLogic = reverseLogic) )
72 
73 
74 def initializePlugin( mObject ):
75  om.MCacheConfigRuleRegistry.registerFilter( "pyNameFilter", pyNameFilterCreator );
76 
77 def uninitializePlugin( mObject ):
78  om.MCacheConfigRuleRegistry.unregisterFilter( "pyNameFilter" );
79 
80 class pyNameFilter( omMPx.MPxCacheConfigRuleFilter ):
81  def __init__( self, pattern = None, reverseLogic = False ):
82  super(pyNameFilter, self).__init__()
83  self._pattern = pattern
84  self._reverseLogic = reverseLogic
85 
86  def isMatch( self, evalNode ):
87  fnNode = om.MFnDependencyNode( evalNode.dependencyNode() )
88  nodeAbsName = fnNode.absoluteName( );
89 
90  # Simple name-matching, using the reverse logic if specified
91  nodeNameMatchesPattern = True if self._pattern in nodeAbsName else False
92  return nodeNameMatchesPattern if False == self._reverseLogic else not nodeNameMatchesPattern
93