2016-06-30 13:22:05 +02:00
|
|
|
import bpy
|
|
|
|
import json
|
|
|
|
import os
|
2016-07-10 00:51:39 +02:00
|
|
|
import glob
|
2016-07-20 17:33:17 +02:00
|
|
|
import lib.umsgpack
|
2016-08-12 02:29:09 +02:00
|
|
|
import platform
|
2016-10-15 12:17:33 +02:00
|
|
|
import zipfile
|
2016-11-21 16:49:32 +01:00
|
|
|
import re
|
2016-06-30 13:22:05 +02:00
|
|
|
|
2016-07-20 17:33:17 +02:00
|
|
|
def write_arm(filepath, output):
|
2016-10-15 12:17:33 +02:00
|
|
|
if filepath.endswith('.zip'):
|
|
|
|
with zipfile.ZipFile(filepath, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
2016-10-17 00:02:51 +02:00
|
|
|
if bpy.data.worlds['Arm'].arm_minimize:
|
2016-10-15 12:17:33 +02:00
|
|
|
zip_file.writestr('data.arm', lib.umsgpack.dumps(output))
|
|
|
|
else:
|
|
|
|
zip_file.writestr('data.arm', json.dumps(output, sort_keys=True, indent=4))
|
2016-07-20 17:33:17 +02:00
|
|
|
else:
|
2016-10-17 00:02:51 +02:00
|
|
|
if bpy.data.worlds['Arm'].arm_minimize:
|
2016-10-15 12:17:33 +02:00
|
|
|
with open(filepath, 'wb') as f:
|
|
|
|
f.write(lib.umsgpack.dumps(output))
|
|
|
|
else:
|
|
|
|
with open(filepath, 'w') as f:
|
|
|
|
f.write(json.dumps(output, sort_keys=True, indent=4))
|
2016-06-30 13:22:05 +02:00
|
|
|
|
2016-11-07 22:10:11 +01:00
|
|
|
def write_image(image, path, file_format='JPEG'):
|
|
|
|
# Convert image to compatible format
|
|
|
|
print('Armory Info: Writing ' + path)
|
|
|
|
ren = bpy.context.scene.render
|
|
|
|
orig_quality = ren.image_settings.quality
|
|
|
|
orig_file_format = ren.image_settings.file_format
|
|
|
|
|
|
|
|
ren.image_settings.quality = 90
|
|
|
|
ren.image_settings.file_format = file_format
|
|
|
|
|
|
|
|
image.save_render(path, bpy.context.scene)
|
|
|
|
|
|
|
|
ren.image_settings.quality = orig_quality
|
|
|
|
ren.image_settings.file_format = orig_file_format
|
|
|
|
|
2016-06-30 13:22:05 +02:00
|
|
|
def get_fp():
|
|
|
|
s = bpy.data.filepath.split(os.path.sep)
|
|
|
|
s.pop()
|
|
|
|
return os.path.sep.join(s)
|
|
|
|
|
2016-08-12 02:29:09 +02:00
|
|
|
def get_os():
|
|
|
|
s = platform.system()
|
|
|
|
if s == 'Windows':
|
|
|
|
return 'win'
|
|
|
|
elif s == 'Darwin':
|
|
|
|
return 'mac'
|
|
|
|
else:
|
|
|
|
return 'linux'
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
def get_sdk_path():
|
2016-07-10 00:51:39 +02:00
|
|
|
user_preferences = bpy.context.user_preferences
|
|
|
|
addon_prefs = user_preferences.addons['armory'].preferences
|
2016-11-23 15:34:59 +01:00
|
|
|
if with_krom() and addon_prefs.sdk_bundled:
|
2016-09-28 00:00:59 +02:00
|
|
|
if get_os() == 'mac':
|
2016-12-05 17:37:26 +01:00
|
|
|
# SDK on MacOS is located in .app folder due to security
|
2016-10-31 19:29:03 +01:00
|
|
|
# blender.app/Contents/MacOS/blender
|
2016-12-05 17:37:26 +01:00
|
|
|
return bpy.app.binary_path[:-22] + '/armsdk/'
|
2016-11-02 15:51:25 +01:00
|
|
|
elif get_os() == 'linux':
|
|
|
|
# blender
|
|
|
|
return bpy.app.binary_path[:-7] + '/armsdk/'
|
2016-09-28 00:00:59 +02:00
|
|
|
else:
|
2016-10-31 23:16:48 +01:00
|
|
|
# blender.exe
|
2016-11-01 12:02:58 +01:00
|
|
|
return bpy.app.binary_path[:-11] + '/armsdk/'
|
2016-09-28 00:00:59 +02:00
|
|
|
else:
|
|
|
|
return addon_prefs.sdk_path
|
|
|
|
|
|
|
|
def get_ffmpeg_path():
|
|
|
|
user_preferences = bpy.context.user_preferences
|
|
|
|
addon_prefs = user_preferences.addons['armory'].preferences
|
|
|
|
return addon_prefs.ffmpeg_path
|
|
|
|
|
|
|
|
def fetch_script_names():
|
2016-10-31 19:29:03 +01:00
|
|
|
if bpy.data.filepath == "":
|
|
|
|
return
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
sdk_path = get_sdk_path()
|
2016-09-02 23:11:04 +02:00
|
|
|
wrd = bpy.data.worlds['Arm']
|
2016-07-10 00:51:39 +02:00
|
|
|
wrd.bundled_scripts_list.clear()
|
|
|
|
os.chdir(sdk_path + '/armory/Sources/armory/trait')
|
|
|
|
for file in glob.glob('*.hx'):
|
|
|
|
wrd.bundled_scripts_list.add().name = file.rsplit('.')[0]
|
|
|
|
wrd.scripts_list.clear()
|
2016-10-17 00:02:51 +02:00
|
|
|
sources_path = get_fp() + '/Sources/' + wrd.arm_project_package
|
2016-07-10 00:51:39 +02:00
|
|
|
if os.path.isdir(sources_path):
|
|
|
|
os.chdir(sources_path)
|
|
|
|
for file in glob.glob('*.hx'):
|
|
|
|
wrd.scripts_list.add().name = file.rsplit('.')[0]
|
|
|
|
os.chdir(get_fp())
|
2016-07-17 23:29:30 +02:00
|
|
|
|
|
|
|
def to_hex(val):
|
|
|
|
return '#%02x%02x%02x%02x' % (int(val[3] * 255), int(val[0] * 255), int(val[1] * 255), int(val[2] * 255))
|
2016-07-27 14:25:01 +02:00
|
|
|
|
2016-09-02 23:11:04 +02:00
|
|
|
def color_to_int(val):
|
|
|
|
return (int(val[3] * 255) << 24) + (int(val[0] * 255) << 16) + (int(val[1] * 255) << 8) + int(val[2] * 255)
|
|
|
|
|
2016-07-27 14:25:01 +02:00
|
|
|
def safe_filename(s):
|
2016-08-22 21:56:28 +02:00
|
|
|
return s
|
|
|
|
|
2016-11-21 16:49:32 +01:00
|
|
|
def safefilename(s):
|
|
|
|
for c in r'[]/\;,><&*:%=+@!#^()|?^':
|
|
|
|
s = s.replace(c, '-')
|
|
|
|
return s
|
|
|
|
|
2016-12-07 10:19:45 +01:00
|
|
|
def safe_source_name(s):
|
|
|
|
return safefilename(s).replace('.', '_').replace(' ', '')
|
|
|
|
|
2016-08-22 21:56:28 +02:00
|
|
|
def safe_assetpath(s):
|
2016-11-24 23:24:55 +01:00
|
|
|
return s[2:] if s[:2] == '//' else s # Remove leading '//'
|
2016-08-22 21:56:28 +02:00
|
|
|
|
2016-09-14 11:49:32 +02:00
|
|
|
def extract_filename(s):
|
2016-11-21 16:49:32 +01:00
|
|
|
return os.path.basename(s)
|
2016-09-14 11:49:32 +02:00
|
|
|
|
2016-08-15 23:45:03 +02:00
|
|
|
def get_render_resolution(scene_index=0):
|
|
|
|
render = bpy.data.scenes[scene_index].render
|
|
|
|
scale = render.resolution_percentage / 100
|
|
|
|
return int(render.resolution_x * scale), int(render.resolution_y * scale)
|
2016-09-08 14:08:31 +02:00
|
|
|
|
|
|
|
def get_project_scene_name():
|
|
|
|
wrd = bpy.data.worlds['Arm']
|
2016-10-17 00:02:51 +02:00
|
|
|
if wrd.arm_play_active_scene:
|
2016-09-08 14:08:31 +02:00
|
|
|
return safe_filename(bpy.context.screen.scene.name)
|
|
|
|
else:
|
2016-10-17 00:02:51 +02:00
|
|
|
return safe_filename(wrd.arm_project_scene)
|
2016-09-12 02:24:20 +02:00
|
|
|
|
2016-11-23 15:34:59 +01:00
|
|
|
krom_found = False
|
|
|
|
def with_krom():
|
|
|
|
global krom_found
|
|
|
|
return krom_found
|
2016-09-12 02:24:20 +02:00
|
|
|
|
2016-11-24 17:35:12 +01:00
|
|
|
glslver = 110
|
2016-11-22 15:02:03 +01:00
|
|
|
def glsl_version():
|
|
|
|
global glslver
|
|
|
|
return glslver
|
|
|
|
|
2016-09-12 02:24:20 +02:00
|
|
|
def register():
|
2016-11-23 15:34:59 +01:00
|
|
|
global krom_found
|
2016-11-22 15:02:03 +01:00
|
|
|
global glslver
|
2016-09-12 02:24:20 +02:00
|
|
|
import importlib.util
|
2016-10-19 13:28:06 +02:00
|
|
|
if importlib.util.find_spec('barmory') != None:
|
2016-11-23 15:34:59 +01:00
|
|
|
krom_found = True
|
2016-11-22 15:02:03 +01:00
|
|
|
import bgl
|
2016-11-26 12:17:33 +01:00
|
|
|
glslver = int(bgl.glGetString(bgl.GL_SHADING_LANGUAGE_VERSION).split(' ', 1)[0].replace('.', ''))
|
2016-09-12 02:24:20 +02:00
|
|
|
|
|
|
|
def unregister():
|
|
|
|
pass
|