doc-python.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. lib3ddevil1/bindings
  2. Python interface to C library functions of lib3ddevil1.
  3. This component uses ctypes thus it must access a compiled .so file (internally
  4. a .dll for Windows). The .so may be located in the top level of the library
  5. directory or any directory in the environment variable's path.
  6. Note that there are two categories of objects in each python interface. One is
  7. derived from ctypes.Structure, and the other is a pythonic object. The former
  8. is for internal use and requires knowledge of both python, C, and ctypes. The
  9. latter is to be used by python users.
  10. Functions and Objects
  11. class pyPldHeader
  12. def __init__(self, filedata = None):
  13. parameters: file data, is a single line list of data.
  14. This function may raise RuntimeError when it fails to initialize
  15. based on file data.
  16. def show(self):
  17. This prints the contents of the object.
  18. def getnumoffsets(self):
  19. This returns the attribute numOffsets of a PldHeader.
  20. See details of the C struct in doc-pld.txt.
  21. def getoffsets(self):
  22. This returns a pythonic list of offsets.
  23. The offsets can be accessed via list comprehensions.
  24. def sizeofsector(self, i):
  25. parameter: i, an integer.
  26. This returns the size of the i-th sector in the .pld.
  27. If the result is -1, then specified i is out of range.
  28. class PyTexturePack
  29. def __init__(self, filedata):
  30. parameters: file data, a single line list of data.
  31. This function may raise RuntimeError when it fails to initialize
  32. based on file data.
  33. See details of the C struct in doc-tex.txt.
  34. def show(self):
  35. This prints the contents of the object.
  36. def getbatchnumber(self):
  37. This returns the attribute 'batchNumber'.
  38. def getfirstbatchoffset(self):
  39. This returns the attribute 'firstBatchOffset'.
  40. class pyTextureBatchDescriptor:
  41. def __init__(self, i, filedata):
  42. parameters: i, for i-th instance of TextureBatchDescriptor.
  43. filedata, a single line list of data.
  44. This function may raise RuntimeError when it fails to initialize
  45. based on file data.
  46. See details of the C struct in doc-tex.txt.
  47. def show(self):
  48. This prints the contents of the object.
  49. def getbatchidx(self):
  50. This returns the attribute 'batchidx'.
  51. def gethash(self):
  52. This returns the attribute 'hash'.
  53. def gettexnumber(self):
  54. This returns the attribute 'texNumber'.
  55. def gettexturesize(self):
  56. This returns the attribute 'textureSize'.
  57. class pyTextureBatch:
  58. def __init__(self, i, filedata):
  59. parameters: i, for i-th instance of TextureBatch.
  60. file data, a single line list of data.
  61. This function may raise RuntimeError when it fails to initialize
  62. based on file data.
  63. See details of the C struct in doc-tex.txt.
  64. def gettextures(self):
  65. This returns a pythonic list of texture data.
  66. The textures can be accessed via list comprehensions.
  67. class pyGeoHeader:
  68. def __init__(self, filedata):
  69. parameters: file data, a single line list of data.
  70. This function may raise RuntimeError when it fails to initialize
  71. based on file data.
  72. See details of the C struct in doc-geo.txt.
  73. def show(self):
  74. This prints the contents of the object.
  75. def getnummesh(self):
  76. This returns the attribute 'numMesh'.
  77. def getunknownb(self):
  78. This returns the attribute 'unknownNumberB'.
  79. def getunknownc(self):
  80. This returns the attribute 'unknownNumberC'.
  81. def getunknownd(self):
  82. This returns the attribute 'unknownNumberD'.
  83. def getpadding(self):
  84. This returns the attribute 'padding'.
  85. def getunknownoffset(self):
  86. This returns the attribute 'unknownOffset'.
  87. class pyMeshHeader:
  88. def __init__(self, i, filedata):
  89. def show(self):
  90. This prints the contents of the object.
  91. def getnumbatch(self):
  92. This returns the attribute 'numBatch'.
  93. def getnumvertex(self):
  94. This returns the attribute 'numVertex'.
  95. def getunknown(self):
  96. This returns the attribute 'u'.
  97. def getoffsetbatches(self):
  98. This returns the attribute 'offsetBatches'.
  99. def getflags(self):
  100. This returns the attribute 'flags'.
  101. class pyMesh:
  102. def __init__(self, i, filedata):
  103. parameters: i, for i-th instance of Mesh.
  104. filedata, a single line list of data.
  105. This function may raise RuntimeError when it fails to initialize
  106. based on file data.
  107. See details of the C struct in doc-geo.txt.
  108. def show(self):
  109. This prints the contents of the object.
  110. def getbatchdata(self):
  111. This returns the attribute 'bd'.
  112. It is a object without methods, and it's attributes are
  113. conventionally accessed with the '.' operator.
  114. def getpositions(self):
  115. This returns a pythonic list of 'positions'.
  116. The positions can be accessed with list comprehensions.
  117. A position is a object 'Coordinate' without methods.
  118. The attributes are x, y, and z to be accessed with the
  119. '.' operator.
  120. def getnormals(self):
  121. This returns a pythonic list of 'normals'.
  122. The normals can be accessed with list comprehensions.
  123. A normal is a object 'Coordinate' without methods.
  124. The attributes are x, y, and z to be accessed with the
  125. '.' operator.
  126. def getuvs(self):
  127. This returns a pythonic list of 'u'.
  128. The UVs can be accessed with list comprehensions.
  129. A UV has two attributes, 'u' and 'v'.
  130. def getboneindexes(self):
  131. This returns a pythonic list of 'bi'.
  132. The bone indices can be accessed with list comprehensions.
  133. A bone index is a fixed length array of 4 elements, unsigned byte.
  134. def getboneweights(self):
  135. This returns a pythonic list of 'bw'.
  136. The bone weights can be accessed with list comprehensions.
  137. A bone weight is effectively an integer in python.
  138. Example Logic: Extract Pld's
  139. with open("pl01.pld", "rb") as f:
  140. data = f.read()
  141. pld = pyPldHeader(data)
  142. filename = "demonstrate"
  143. i = 0
  144. for offset in pld.getoffsets():
  145. with open(filename + str(i), "wb") as fw:
  146. end = offset + pld.sizeofsector(i)
  147. chunk = data[offset:end]
  148. fw.write(chunk)
  149. i += 1
  150. Example Logic: Extract Textures from a Single Batch
  151. with open("pl01.pld_1.txp", "rb") as f:
  152. data = f.read()
  153. tp = pyTexturePack(data)
  154. filename = "texture" # part 1 of output file name
  155. id = 0 # part 2 of output file name
  156. # Iterate through all of the batches in the package.
  157. for i in range(0, tp.getbatchnumber()):
  158. # Get a batch.
  159. tx = pyTextureBatch(i, data)
  160. # Iterate through all textures in batch.
  161. for texture in tx.gettextures():
  162. with open(filename + str(id) + ".dds", "wb") as fw:
  163. fw.write(texture)
  164. id += 1
  165. Example Logic: Iterate through all MeshHeaders and Meshes:
  166. with open("pl00.pld_0", "rb") as f:
  167. # Get data to extract from.
  168. data = f.read()
  169. # We want to know how many meshes are in the file.
  170. gh = pyGeoHeader(data)
  171. # Show each MeshHeader
  172. for i in range(0, gh.getnummesh()):
  173. mh = pyMeshHeader(i, data)
  174. mh.show()
  175. # Get each Mesh
  176. for i in range(0, gh.getnummesh()):
  177. m = pyMesh(i, data)
  178. m.show()
  179. # Whatever you do with each of them is up to you.