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