blend_render_info.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python3
  2. # ##### BEGIN GPL LICENSE BLOCK #####
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. #
  18. # ##### END GPL LICENSE BLOCK #####
  19. # <pep8 compliant>
  20. # This module can get render info without running from inside blender.
  21. #
  22. # This struct won't change according to Ton.
  23. # Note that the size differs on 32/64bit
  24. #
  25. # typedef struct BHead {
  26. # int code, len;
  27. # void *old;
  28. # int SDNAnr, nr;
  29. # } BHead;
  30. def read_blend_rend_chunk(path):
  31. import struct
  32. blendfile = open(path, "rb")
  33. head = blendfile.read(7)
  34. if head[0:2] == b'\x1f\x8b': # gzip magic
  35. import gzip
  36. blendfile.seek(0)
  37. blendfile = gzip.open(blendfile, "rb")
  38. head = blendfile.read(7)
  39. if head != b'BLENDER':
  40. print("not a blend file:", path)
  41. blendfile.close()
  42. return []
  43. is_64_bit = (blendfile.read(1) == b'-')
  44. # true for PPC, false for X86
  45. is_big_endian = (blendfile.read(1) == b'V')
  46. # Now read the bhead chunk!!!
  47. blendfile.read(3) # skip the version
  48. scenes = []
  49. sizeof_bhead = 24 if is_64_bit else 20
  50. while blendfile.read(4) == b'REND':
  51. sizeof_bhead_left = sizeof_bhead - 4
  52. struct.unpack('>i' if is_big_endian else '<i', blendfile.read(4))[0]
  53. sizeof_bhead_left -= 4
  54. # We don't care about the rest of the bhead struct
  55. blendfile.read(sizeof_bhead_left)
  56. # Now we want the scene name, start and end frame. this is 32bites long
  57. start_frame, end_frame = struct.unpack('>2i' if is_big_endian else '<2i', blendfile.read(8))
  58. scene_name = blendfile.read(64)
  59. scene_name = scene_name[:scene_name.index(b'\0')]
  60. try:
  61. scene_name = str(scene_name, "utf8")
  62. except TypeError:
  63. pass
  64. scenes.append((start_frame, end_frame, scene_name))
  65. blendfile.close()
  66. return scenes
  67. def main():
  68. import sys
  69. for arg in sys.argv[1:]:
  70. if arg.lower().endswith('.blend'):
  71. for value in read_blend_rend_chunk(arg):
  72. print("%d %d %s" % value)
  73. if __name__ == '__main__':
  74. main()