blender.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Michal Proszek, 2014 (poxip)
  4. """
  5. Detect the version of Blender, path
  6. and install the extension:
  7. def options(opt):
  8. opt.load('blender')
  9. def configure(cnf):
  10. cnf.load('blender')
  11. def build(bld):
  12. bld(name='io_mesh_raw',
  13. feature='blender',
  14. files=['file1.py', 'file2.py']
  15. )
  16. If name variable is empty, files are installed in scripts/addons, otherwise scripts/addons/name
  17. Use ./waf configure --system to set the installation directory to system path
  18. """
  19. import os
  20. import re
  21. from getpass import getuser
  22. from waflib import Utils
  23. from waflib.TaskGen import feature
  24. from waflib.Configure import conf
  25. def options(opt):
  26. opt.add_option(
  27. '-s', '--system',
  28. dest='directory_system',
  29. default=False,
  30. action='store_true',
  31. help='determines installation directory (default: user)'
  32. )
  33. @conf
  34. def find_blender(ctx):
  35. '''Return version number of blender, if not exist return None'''
  36. blender = ctx.find_program('blender')
  37. output = ctx.cmd_and_log(blender + ['--version'])
  38. m = re.search(r'Blender\s*((\d+(\.|))*)', output)
  39. if not m:
  40. ctx.fatal('Could not retrieve blender version')
  41. try:
  42. blender_version = m.group(1)
  43. except IndexError:
  44. ctx.fatal('Could not retrieve blender version')
  45. ctx.env['BLENDER_VERSION'] = blender_version
  46. return blender
  47. @conf
  48. def configure_paths(ctx):
  49. """Setup blender paths"""
  50. # Get the username
  51. user = getuser()
  52. _platform = Utils.unversioned_sys_platform()
  53. config_path = {'user': '', 'system': ''}
  54. if _platform.startswith('linux'):
  55. config_path['user'] = '/home/%s/.config/blender/' % user
  56. config_path['system'] = '/usr/share/blender/'
  57. elif _platform == 'darwin':
  58. # MAC OS X
  59. config_path['user'] = \
  60. '/Users/%s/Library/Application Support/Blender/' % user
  61. config_path['system'] = '/Library/Application Support/Blender/'
  62. elif Utils.is_win32:
  63. # Windows
  64. appdata_path = ctx.getenv('APPDATA').replace('\\', '/')
  65. homedrive = ctx.getenv('HOMEDRIVE').replace('\\', '/')
  66. config_path['user'] = '%s/Blender Foundation/Blender/' % appdata_path
  67. config_path['system'] = \
  68. '%sAll Users/AppData/Roaming/Blender Foundation/Blender/' % homedrive
  69. else:
  70. ctx.fatal(
  71. 'Unsupported platform. '
  72. 'Available platforms: Linux, OSX, MS-Windows.'
  73. )
  74. blender_version = ctx.env['BLENDER_VERSION']
  75. config_path['user'] += blender_version + '/'
  76. config_path['system'] += blender_version + '/'
  77. ctx.env['BLENDER_CONFIG_DIR'] = os.path.abspath(config_path['user'])
  78. if ctx.options.directory_system:
  79. ctx.env['BLENDER_CONFIG_DIR'] = config_path['system']
  80. ctx.env['BLENDER_ADDONS_DIR'] = os.path.join(
  81. ctx.env['BLENDER_CONFIG_DIR'], 'scripts/addons'
  82. )
  83. Utils.check_dir(ctx.env['BLENDER_ADDONS_DIR'])
  84. def configure(ctx):
  85. ctx.find_blender()
  86. ctx.configure_paths()
  87. @feature('blender_list')
  88. def blender(self):
  89. # Two ways to install a blender extension: as a module or just .py files
  90. dest_dir = os.path.join(self.env.BLENDER_ADDONS_DIR, self.get_name())
  91. Utils.check_dir(dest_dir)
  92. self.add_install_files(install_to=dest_dir, install_from=getattr(self, 'files', '.'))