/usr/share/pyshared/collada/source.py is in python-collada 0.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | ####################################################################
# #
# THIS FILE IS PART OF THE pycollada LIBRARY SOURCE CODE. #
# USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS #
# GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE #
# IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. #
# #
# THE pycollada SOURCE CODE IS (C) COPYRIGHT 2011 #
# by Jeff Terrace and contributors #
# #
####################################################################
"""Module for managing data sources defined in geometry tags."""
import numpy
from collada.common import DaeObject, E, tag
from collada.common import DaeIncompleteError, DaeBrokenRefError, DaeMalformedError
from collada.xmlutil import etree as ElementTree
class InputList(object):
"""Used for defining input sources to a geometry."""
class Input:
def __init__(self, offset, semantic, src, set=None):
self.offset = offset
self.semantic = semantic
self.source = src
self.set = set
semantics = ["VERTEX", "NORMAL", "TEXCOORD", "TEXBINORMAL", "TEXTANGENT", "COLOR", "TANGENT", "BINORMAL"]
def __init__(self):
"""Create an input list"""
self.inputs = {}
for s in self.semantics:
self.inputs[s] = []
def addInput(self, offset, semantic, src, set=None):
"""Add an input source to this input list.
:param int offset:
Offset for this source within the geometry's indices
:param str semantic:
The semantic for the input source. Currently supported options are:
* VERTEX
* NORMAL
* TEXCOORD
* TEXBINORMAL
* TEXTANGENT
* COLOR
* TANGENT
* BINORMAL
:param str src:
A string identifier of the form `#srcid` where `srcid` is a source
within the geometry's :attr:`~collada.geometry.Geometry.sourceById` array.
:param str set:
Indicates a set number for the source. This is used, for example,
when there are multiple texture coordinate sets.
"""
if semantic not in self.semantics:
raise DaeUnsupportedError("Unsupported semantic %s" % semantic)
self.inputs[semantic].append(self.Input(offset, semantic, src, set))
def getList(self):
"""Returns a list of tuples of the source in the form (offset, semantic, source, set)"""
retlist = []
for inplist in self.inputs.values():
for inp in inplist:
retlist.append((inp.offset, inp.semantic, inp.source, inp.set))
return retlist
def __str__(self): return '<InputList>'
def __repr__(self): return str(self)
class Source(DaeObject):
"""Abstract class for loading source arrays"""
@staticmethod
def load(collada, localscope, node):
sourceid = node.get('id')
arraynode = node.find(tag('float_array'))
if not arraynode is None:
return FloatSource.load(collada, localscope, node)
arraynode = node.find(tag('IDREF_array'))
if not arraynode is None:
return IDRefSource.load(collada, localscope, node)
arraynode = node.find(tag('Name_array'))
if not arraynode is None:
return NameSource.load(collada, localscope, node)
if arraynode is None: raise DaeIncompleteError('No array found in source %s' % sourceid)
class FloatSource(Source):
"""Contains a source array of floats, as defined in the collada
<float_array> inside a <source>.
If ``f`` is an instance of :class:`collada.source.FloatSource`, then
``len(f)`` is the length of the shaped source. ``len(f)*len(f.components)``
would give you the number of values in the source. ``f[i]`` is the i\ :sup:`th`
item in the source array.
"""
def __init__(self, id, data, components, xmlnode=None):
"""Create a float source instance.
:param str id:
A unique string identifier for the source
:param numpy.array data:
Numpy array (unshaped) with the source values
:param tuple components:
Tuple of strings describing the semantic of the data,
e.g. ``('X','Y','Z')`` would cause :attr:`data` to be
reshaped as ``(-1, 3)``
:param xmlnode:
When loaded, the xmlnode it comes from.
"""
self.id = id
"""The unique string identifier for the source"""
self.data = data
"""Numpy array with the source values. This will be shaped as ``(-1,N)`` where ``N = len(self.components)``"""
self.data.shape = (-1, len(components) )
self.components = components
"""Tuple of strings describing the semantic of the data, e.g. ``('X','Y','Z')``"""
if xmlnode != None:
self.xmlnode = xmlnode
"""ElementTree representation of the source."""
else:
self.data.shape = (-1,)
txtdata = ' '.join(map(str, self.data.tolist() ))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
stridelen = len(self.components)
sourcename = "%s-array"%self.id
self.xmlnode = E.source(
E.float_array(txtdata, count=str(rawlen), id=sourcename),
E.technique_common(
E.accessor(
*[E.param(type='float', name=c) for c in self.components]
, **{'count':str(acclen), 'stride':str(stridelen), 'source':"#%s"%sourcename} )
)
, id=self.id )
def __len__(self): return len(self.data)
def __getitem__(self, i): return self.data[i]
def save(self):
"""Saves the source back to :attr:`xmlnode`"""
self.data.shape = (-1,)
txtdata = ' '.join(map(lambda x: '%.7g'%x , self.data.tolist()))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
node = self.xmlnode.find(tag('float_array'))
node.text = txtdata
node.set('count', str(rawlen))
node.set('id', self.id+'-array' )
node = self.xmlnode.find('%s/%s'%(tag('technique_common'), tag('accessor')))
node.clear()
node.set('count', str(acclen))
node.set('source', '#'+self.id+'-array')
node.set('stride', str(len(self.components)))
for c in self.components:
node.append(E.param(type='float', name=c))
self.xmlnode.set('id', self.id )
@staticmethod
def load( collada, localscope, node ):
sourceid = node.get('id')
arraynode = node.find(tag('float_array'))
if arraynode is None: raise DaeIncompleteError('No float_array in source node')
if arraynode.text is None:
data = numpy.array([], dtype=numpy.float32)
else:
try: data = numpy.fromstring(arraynode.text, dtype=numpy.float32, sep=' ')
except ValueError: raise DaeMalformedError('Corrupted float array')
data[numpy.isnan(data)] = 0
paramnodes = node.findall('%s/%s/%s'%(tag('technique_common'), tag('accessor'), tag('param')))
if not paramnodes: raise DaeIncompleteError('No accessor info in source node')
components = [ param.get('name') for param in paramnodes ]
if len(components) == 2 and components[0] == 'U' and components[1] == 'V':
#U,V is used for "generic" arguments - convert to S,T
components = ['S', 'T']
if len(components) == 3 and components[0] == 'S' and components[1] == 'T' and components[2] == 'P':
components = ['S', 'T']
data.shape = (-1, 3)
#remove 3d texcoord dimension because we don't support it
#TODO
data = numpy.array(zip(data[:,0], data[:,1]))
data.shape = (-1)
return FloatSource( sourceid, data, tuple(components), xmlnode=node )
def __str__(self): return '<FloatSource size=%d>' % (len(self),)
def __repr__(self): return str(self)
class IDRefSource(Source):
"""Contains a source array of ID references, as defined in the collada
<IDREF_array> inside a <source>.
If ``r`` is an instance of :class:`collada.source.IDRefSource`, then
``len(r)`` is the length of the shaped source. ``len(r)*len(r.components)``
would give you the number of values in the source. ``r[i]`` is the i\ :sup:`th`
item in the source array.
"""
def __init__(self, id, data, components, xmlnode=None):
"""Create an id ref source instance.
:param str id:
A unique string identifier for the source
:param numpy.array data:
Numpy array (unshaped) with the source values
:param tuple components:
Tuple of strings describing the semantic of the data,
e.g. ``('MORPH_TARGET')`` would cause :attr:`data` to be
reshaped as ``(-1, 1)``
:param xmlnode:
When loaded, the xmlnode it comes from.
"""
self.id = id
"""The unique string identifier for the source"""
self.data = data
"""Numpy array with the source values. This will be shaped as ``(-1,N)`` where ``N = len(self.components)``"""
self.data.shape = (-1, len(components) )
self.components = components
"""Tuple of strings describing the semantic of the data, e.g. ``('MORPH_TARGET')``"""
if xmlnode != None:
self.xmlnode = xmlnode
"""ElementTree representation of the source."""
else:
self.data.shape = (-1,)
txtdata = ' '.join(map(str, self.data.tolist() ))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
stridelen = len(self.components)
sourcename = "%s-array"%self.id
self.xmlnode = E.source(
E.IDREF_array(txtdata, count=str(rawlen), id=sourcename),
E.technique_common(
E.accessor(
*[E.param(type='IDREF', name=c) for c in self.components]
, **{'count':str(acclen), 'stride':str(stridelen), 'source':sourcename})
)
, id=self.id )
def __len__(self): return len(self.data)
def __getitem__(self, i): return self.data[i][0] if len(self.data[i])==1 else self.data[i]
def save(self):
"""Saves the source back to :attr:`xmlnode`"""
self.data.shape = (-1,)
txtdata = ' '.join(map(str, self.data.tolist() ))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
node = self.xmlnode.find(tag('IDREF_array'))
node.text = txtdata
node.set('count', str(rawlen))
node.set('id', self.id+'-array' )
node = self.xmlnode.find('%s/%s'%(tag('technique_common'), tag('accessor')))
node.clear()
node.set('count', str(acclen))
node.set('source', '#'+self.id+'-array')
node.set('stride', str(len(self.components)))
for c in self.components:
node.append(E.param(type='IDREF', name=c))
self.xmlnode.set('id', self.id )
@staticmethod
def load( collada, localscope, node ):
sourceid = node.get('id')
arraynode = node.find(tag('IDREF_array'))
if arraynode is None: raise DaeIncompleteError('No IDREF_array in source node')
if arraynode.text is None:
values = []
else:
try: values = [v for v in arraynode.text.split()]
except ValueError: raise DaeMalformedError('Corrupted IDREF array')
data = numpy.array( values, dtype=numpy.string_ )
paramnodes = node.findall('%s/%s/%s'%(tag('technique_common'), tag('accessor'), tag('param')))
if not paramnodes: raise DaeIncompleteError('No accessor info in source node')
components = [ param.get('name') for param in paramnodes ]
return IDRefSource( sourceid, data, tuple(components), xmlnode=node )
def __str__(self): return '<IDRefSource size=%d>' % (len(self),)
def __repr__(self): return str(self)
class NameSource(Source):
"""Contains a source array of strings, as defined in the collada
<Name_array> inside a <source>.
If ``n`` is an instance of :class:`collada.source.NameSource`, then
``len(n)`` is the length of the shaped source. ``len(n)*len(n.components)``
would give you the number of values in the source. ``n[i]`` is the i\ :sup:`th`
item in the source array.
"""
def __init__(self, id, data, components, xmlnode=None):
"""Create a name source instance.
:param str id:
A unique string identifier for the source
:param numpy.array data:
Numpy array (unshaped) with the source values
:param tuple components:
Tuple of strings describing the semantic of the data,
e.g. ``('JOINT')`` would cause :attr:`data` to be
reshaped as ``(-1, 1)``
:param xmlnode:
When loaded, the xmlnode it comes from.
"""
self.id = id
"""The unique string identifier for the source"""
self.data = data
"""Numpy array with the source values. This will be shaped as ``(-1,N)`` where ``N = len(self.components)``"""
self.data.shape = (-1, len(components) )
self.components = components
"""Tuple of strings describing the semantic of the data, e.g. ``('JOINT')``"""
if xmlnode != None:
self.xmlnode = xmlnode
"""ElementTree representation of the source."""
else:
self.data.shape = (-1,)
txtdata = ' '.join(map(str, self.data.tolist() ))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
stridelen = len(self.components)
sourcename = "%s-array"%self.id
self.xmlnode = E.source(
E.Name_array(txtdata, count=str(rawlen), id=sourcename),
E.technique_common(
E.accessor(
*[E.param(type='Name', name=c) for c in self.components]
, **{'count':str(acclen), 'stride':str(stridelen), 'source':sourcename})
)
, id=self.id )
def __len__(self): return len(self.data)
def __getitem__(self, i): return self.data[i][0] if len(self.data[i])==1 else self.data[i]
def save(self):
"""Saves the source back to :attr:`xmlnode`"""
self.data.shape = (-1,)
txtdata = ' '.join(map(str, self.data.tolist() ))
rawlen = len( self.data )
self.data.shape = (-1, len(self.components) )
acclen = len( self.data )
node = self.xmlnode.find(tag('Name_array'))
node.text = txtdata
node.set('count', str(rawlen))
node.set('id', self.id+'-array' )
node = self.xmlnode.find('%s/%s'%(tag('technique_common'), tag('accessor')))
node.clear()
node.set('count', str(acclen))
node.set('source', '#'+self.id+'-array')
node.set('stride', str(len(self.components)))
for c in self.components:
node.append(E.param(type='IDREF', name=c))
self.xmlnode.set('id', self.id )
@staticmethod
def load( collada, localscope, node ):
sourceid = node.get('id')
arraynode = node.find(tag('Name_array'))
if arraynode is None: raise DaeIncompleteError('No Name_array in source node')
if arraynode.text is None:
values = []
else:
try: values = [v for v in arraynode.text.split()]
except ValueError: raise DaeMalformedError('Corrupted Name array')
data = numpy.array( values, dtype=numpy.string_ )
paramnodes = node.findall('%s/%s/%s'%(tag('technique_common'), tag('accessor'), tag('param')))
if not paramnodes: raise DaeIncompleteError('No accessor info in source node')
components = [ param.get('name') for param in paramnodes ]
return NameSource( sourceid, data, tuple(components), xmlnode=node )
def __str__(self): return '<NameSource size=%d>' % (len(self),)
def __repr__(self): return str(self)
|