<s> """<STR_LIT>""" <EOL> import numpy <EOL> from collada import source <EOL> from collada import triangleset <EOL> from collada import lineset <EOL> from collada import polylist <EOL> from collada import polygons <EOL> from collada import primitive <EOL> from collada . common import DaeObject , E , tag <EOL> from collada . common import DaeIncompleteError , DaeBrokenRefError , DaeMalformedError , DaeUnsupportedError <EOL> from collada . xmlutil import etree as ElementTree <EOL> class Geometry ( DaeObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , collada , id , name , sourcebyid , primitives = None , <EOL> xmlnode = None , double_sided = False ) : <EOL> """<STR_LIT>""" <EOL> self . collada = collada <EOL> """<STR_LIT>""" <EOL> self . id = id <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> """<STR_LIT>""" <EOL> self . double_sided = double_sided <EOL> """<STR_LIT>""" <EOL> self . sourceById = sourcebyid <EOL> """<STR_LIT>""" <EOL> if isinstance ( sourcebyid , list ) : <EOL> self . sourceById = { } <EOL> for src in sourcebyid : <EOL> self . sourceById [ src . id ] = src <EOL> self . primitives = [ ] <EOL> """<STR_LIT>""" <EOL> if primitives is not None : <EOL> self . primitives = primitives <EOL> if xmlnode != None : <EOL> self . xmlnode = xmlnode <EOL> """<STR_LIT>""" <EOL> else : <EOL> sourcenodes = [ ] <EOL> verticesnode = None <EOL> for srcid , src in self . sourceById . items ( ) : <EOL> sourcenodes . append ( src . xmlnode ) <EOL> if verticesnode is None : <EOL> verticesnode = E . vertices ( E . input ( semantic = '<STR_LIT>' , source = "<STR_LIT>" % srcid ) , <EOL> id = srcid + '<STR_LIT>' ) <EOL> meshnode = E . mesh ( * sourcenodes ) <EOL> meshnode . append ( verticesnode ) <EOL> self . xmlnode = E . geometry ( meshnode ) <EOL> if len ( self . id ) > <NUM_LIT:0> : self . xmlnode . set ( "<STR_LIT:id>" , self . id ) <EOL> if len ( self . name ) > <NUM_LIT:0> : self . xmlnode . set ( "<STR_LIT:name>" , self . name ) <EOL> def createLineSet ( self , indices , inputlist , materialid ) : <EOL> """<STR_LIT>""" <EOL> inputdict = primitive . Primitive . _getInputsFromList ( self . collada , self . sourceById , inputlist . getList ( ) ) <EOL> return lineset . LineSet ( inputdict , materialid , indices ) <EOL> def createTriangleSet ( self , indices , inputlist , materialid ) : <EOL> """<STR_LIT>""" <EOL> inputdict = primitive . Primitive . _getInputsFromList ( self . collada , self . sourceById , inputlist . getList ( ) ) <EOL> return triangleset . TriangleSet ( inputdict , materialid , indices ) <EOL> def createPolylist ( self , indices , vcounts , inputlist , materialid ) : <EOL> """<STR_LIT>""" <EOL> inputdict = primitive . Primitive . _getInputsFromList ( self . collada , self . sourceById , inputlist . getList ( ) ) <EOL> return polylist . Polylist ( inputdict , materialid , indices , vcounts ) <EOL> def createPolygons ( self , indices , inputlist , materialid ) : <EOL> """<STR_LIT>""" <EOL> inputdict = primitive . Primitive . _getInputsFromList ( self . collada , self . sourceById , inputlist . getList ( ) ) <EOL> return polygons . Polygons ( inputdict , materialid , indices ) <EOL> @ staticmethod <EOL> def load ( collada , localscope , node ) : <EOL> id = node . get ( "<STR_LIT:id>" ) or "<STR_LIT>" <EOL> name = node . get ( "<STR_LIT:name>" ) or "<STR_LIT>" <EOL> meshnode = node . find ( tag ( '<STR_LIT>' ) ) <EOL> if meshnode is None : raise DaeUnsupportedError ( '<STR_LIT>' ) <EOL> sourcebyid = { } <EOL> sources = [ ] <EOL> sourcenodes = node . findall ( '<STR_LIT>' % ( tag ( '<STR_LIT>' ) , tag ( '<STR_LIT:source>' ) ) ) <EOL> for sourcenode in sourcenodes : <EOL> ch = source . Source . load ( collada , { } , sourcenode ) <EOL> sources . append ( ch ) <EOL> sourcebyid [ ch . id ] = ch <EOL> verticesnode = meshnode . find ( tag ( '<STR_LIT>' ) ) <EOL> if verticesnode is None : <EOL> vertexsource = None <EOL> else : <EOL> inputnodes = { } <EOL> for inputnode in verticesnode . findall ( tag ( '<STR_LIT:input>' ) ) : <EOL> semantic = inputnode . get ( '<STR_LIT>' ) <EOL> inputsource = inputnode . get ( '<STR_LIT:source>' ) <EOL> if not semantic or not inputsource or not inputsource . startswith ( '<STR_LIT:#>' ) : <EOL> raise DaeIncompleteError ( '<STR_LIT>' ) <EOL> inputnodes [ semantic ] = sourcebyid . get ( inputsource [ <NUM_LIT:1> : ] ) <EOL> if ( not verticesnode . get ( '<STR_LIT:id>' ) or len ( inputnodes ) == <NUM_LIT:0> or <EOL> not '<STR_LIT>' in inputnodes ) : <EOL> raise DaeIncompleteError ( '<STR_LIT>' ) <EOL> sourcebyid [ verticesnode . get ( '<STR_LIT:id>' ) ] = inputnodes <EOL> vertexsource = verticesnode . get ( '<STR_LIT:id>' ) <EOL> double_sided_node = node . find ( '<STR_LIT>' % ( tag ( '<STR_LIT>' ) , tag ( '<STR_LIT>' ) ) ) <EOL> double_sided = False <EOL> if double_sided_node is not None and double_sided_node . text is not None : <EOL> try : <EOL> val = int ( double_sided_node . text ) <EOL> if val == <NUM_LIT:1> : <EOL> double_sided = True <EOL> except ValueError : pass <EOL> _primitives = [ ] <EOL> for subnode in meshnode : <EOL> if subnode . tag == tag ( '<STR_LIT>' ) : <EOL> _primitives . append ( polylist . Polylist . load ( collada , sourcebyid , subnode ) ) <EOL> elif subnode . tag == tag ( '<STR_LIT>' ) : <EOL> _primitives . append ( triangleset . TriangleSet . load ( collada , sourcebyid , subnode ) ) <EOL> elif subnode . tag == tag ( '<STR_LIT>' ) : <EOL> _primitives . append ( lineset . LineSet . load ( collada , sourcebyid , subnode ) ) <EOL> elif subnode . tag == tag ( '<STR_LIT>' ) : <EOL> _primitives . append ( polygons . Polygons . load ( collada , sourcebyid , subnode ) ) <EOL> elif subnode . tag != tag ( '<STR_LIT:source>' ) and subnode . tag != tag ( '<STR_LIT>' ) and subnode . tag != tag ( '<STR_LIT>' ) : <EOL> raise DaeUnsupportedError ( '<STR_LIT>' % subnode . tag ) <EOL> geom = Geometry ( collada , id , name , sourcebyid , _primitives , xmlnode = node , double_sided = double_sided ) <EOL> return geom <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> meshnode = self . xmlnode . find ( tag ( '<STR_LIT>' ) ) <EOL> for src in self . sourceById . values ( ) : <EOL> if isinstance ( src , source . Source ) : <EOL> src . save ( ) <EOL> if src . xmlnode not in meshnode . getchildren ( ) : <EOL> meshnode . insert ( <NUM_LIT:0> , src . xmlnode ) <EOL> deletenodes = [ ] <EOL> for oldsrcnode in meshnode . findall ( tag ( '<STR_LIT:source>' ) ) : <EOL> if oldsrcnode not in [ src . xmlnode <EOL> for src in self . sourceById . values ( ) <EOL> if isinstance ( src , source . Source ) ] : <EOL> deletenodes . append ( oldsrcnode ) <EOL> for d in deletenodes : <EOL> meshnode . remove ( d ) <EOL> vnode = self . xmlnode . find ( tag ( '<STR_LIT>' ) ) . find ( tag ( '<STR_LIT>' ) ) <EOL> delete_inputs = [ ] <EOL> for input_node in vnode . findall ( tag ( '<STR_LIT:input>' ) ) : <EOL> if input_node . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> input_vnode = input_node <EOL> else : <EOL> srcid = input_node . get ( '<STR_LIT:source>' ) [ <NUM_LIT:1> : ] <EOL> if srcid not in self . sourceById : <EOL> delete_inputs . append ( input_node ) <EOL> for node in delete_inputs : <EOL> vnode . remove ( node ) <EOL> vert_sources = [ ] <EOL> for prim in self . primitives : <EOL> for src in prim . sources [ '<STR_LIT>' ] : <EOL> vert_sources . append ( src [ <NUM_LIT:2> ] [ <NUM_LIT:1> : ] ) <EOL> vert_src = vnode . get ( '<STR_LIT:id>' ) <EOL> vert_ref = input_vnode . get ( '<STR_LIT:source>' ) [ <NUM_LIT:1> : ] <EOL> if not ( vert_src in vert_sources or vert_ref in vert_sources ) and len ( vert_sources ) > <NUM_LIT:0> : <EOL> if vert_ref in self . sourceById and vert_ref in vert_sources : <EOL> new_source = vert_ref <EOL> else : <EOL> new_source = vert_sources [ <NUM_LIT:0> ] <EOL> self . sourceById [ new_source + '<STR_LIT>' ] = self . sourceById [ new_source ] <EOL> input_vnode . set ( '<STR_LIT:source>' , '<STR_LIT:#>' + new_source ) <EOL> vnode . set ( '<STR_LIT:id>' , new_source + '<STR_LIT>' ) <EOL> vert_src = vnode . get ( '<STR_LIT:id>' ) <EOL> vert_ref = input_vnode . get ( '<STR_LIT:source>' ) [ <NUM_LIT:1> : ] <EOL> for prim in self . primitives : <EOL> for node in prim . xmlnode . findall ( tag ( '<STR_LIT:input>' ) ) : <EOL> src = node . get ( '<STR_LIT:source>' ) [ <NUM_LIT:1> : ] <EOL> if src == vert_ref : <EOL> node . set ( '<STR_LIT:source>' , '<STR_LIT>' % vert_src ) <EOL> self . xmlnode . set ( '<STR_LIT:id>' , self . id ) <EOL> self . xmlnode . set ( '<STR_LIT:name>' , self . name ) <EOL> for prim in self . primitives : <EOL> if type ( prim ) is triangleset . TriangleSet and prim . xmlnode . tag != tag ( '<STR_LIT>' ) : <EOL> prim . _recreateXmlNode ( ) <EOL> if prim . xmlnode not in meshnode . getchildren ( ) : <EOL> meshnode . append ( prim . xmlnode ) <EOL> deletenodes = [ ] <EOL> primnodes = [ prim . xmlnode for prim in self . primitives ] <EOL> for child in meshnode . getchildren ( ) : <EOL> if child . tag != tag ( '<STR_LIT>' ) and child . tag != tag ( '<STR_LIT:source>' ) and child not in primnodes : <EOL> deletenodes . append ( child ) <EOL> for d in deletenodes : <EOL> meshnode . remove ( d ) <EOL> def bind ( self , matrix , materialnodebysymbol ) : <EOL> """<STR_LIT>""" <EOL> return BoundGeometry ( self , matrix , materialnodebysymbol ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . id , len ( self . primitives ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) <EOL> class BoundGeometry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , geom , matrix , materialnodebysymbol ) : <EOL> self . matrix = matrix <EOL> """<STR_LIT>""" <EOL> self . materialnodebysymbol = materialnodebysymbol <EOL> """<STR_LIT>""" <EOL> self . _primitives = geom . primitives <EOL> self . original = geom <EOL> """<STR_LIT>""" <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _primitives ) <EOL> def primitives ( self ) : <EOL> """<STR_LIT>""" <EOL> for p in self . _primitives : <EOL> boundp = p . bind ( self . matrix , self . materialnodebysymbol ) <EOL> yield boundp <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . original . id , len ( self ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( self ) </s>
