<s> from meshtool . filters . base_filters import OptimizationFilter <EOL> import collada <EOL> def combineMaterials ( mesh ) : <EOL> material_sets = [ ] <EOL> for m in mesh . materials : <EOL> matched = False <EOL> for s in material_sets : <EOL> if s [ <NUM_LIT:0> ] . effect == m . effect : <EOL> s . append ( m ) <EOL> matched = True <EOL> break <EOL> if not matched : <EOL> material_sets . append ( [ m ] ) <EOL> for s in material_sets : <EOL> if len ( s ) <= <NUM_LIT:1> : <EOL> continue <EOL> to_keep = s . pop ( <NUM_LIT:0> ) <EOL> for scene in mesh . scenes : <EOL> nodes_to_check = [ ] <EOL> nodes_to_check . extend ( scene . nodes ) <EOL> while len ( nodes_to_check ) > <NUM_LIT:0> : <EOL> curnode = nodes_to_check . pop ( ) <EOL> for node in curnode . children : <EOL> if isinstance ( node , collada . scene . Node ) : <EOL> nodes_to_check . append ( node ) <EOL> elif isinstance ( node , collada . scene . GeometryNode ) : <EOL> for matnode in node . materials : <EOL> if matnode . target in s : <EOL> matnode . target = to_keep <EOL> for material in s : <EOL> del mesh . materials [ material . id ] <EOL> def FilterGenerator ( ) : <EOL> class CombineMaterialsFilter ( OptimizationFilter ) : <EOL> def __init__ ( self ) : <EOL> super ( CombineMaterialsFilter , self ) . __init__ ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def apply ( self , mesh ) : <EOL> combineMaterials ( mesh ) <EOL> return mesh <EOL> return CombineMaterialsFilter ( ) <EOL> from meshtool . filters import factory <EOL> factory . register ( FilterGenerator ( ) . name , FilterGenerator ) </s>
