Simplifying material check thanks to the teachings of Master Campbell
[blender-addons.git] / game_engine_save_as_runtime.py
blob9130ad0f6bfa4ebedb0c389431ebe5a24b21b5cf
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 bl_addon_info = {
20 'name': 'Save As Runtime',
21 'author': 'Mitchell Stokes (Moguri)',
22 'version': (0, 2, 2),
23 'blender': (2, 5, 4),
24 'api': 31667,
25 'location': 'File > Export',
26 'description': 'Bundle a .blend file with the Blenderplayer',
27 'warning': '',
28 'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/'\
29 'Scripts/Game_Engine/Save_As_Runtime',
30 'tracker_url': 'https://projects.blender.org/tracker/index.php?'\
31 'func=detail&aid=23564&group_id=153&atid=469',
32 'category': 'Game Engine'}
34 import bpy
35 import os
38 def WriteAppleRuntime(player_path, output_path):
39 # Use the system's cp command to preserve some meta-data
40 os.system('cp -R "%s" "%s"' % (player_path, output_path))
42 bpy.ops.save_as_mainfile(filepath=output_path+"/Contents/Resources/game.blend", copy=True)
45 def WriteRuntime(player_path, output_path):
46 import struct
48 # Check the paths
49 if not os.path.isfile(player_path):
50 print("The player could not be found! Runtime not saved.")
51 return
53 # Check if we're bundling a .app
54 if player_path.endswith('.app'):
55 WriteAppleRuntime(player_path, output_path)
56 return
58 # Get the player's binary and the offset for the blend
59 file = open(player_path, 'rb')
60 player_d = file.read()
61 offset = file.tell()
62 file.close()
64 # Create a tmp blend file
65 blend_path = bpy.path.clean_name(output_path)
66 bpy.ops.wm.save_as_mainfile(filepath=blend_path, copy=True)
67 blend_path += '.blend'
70 # Get the blend data (if compressed, uncompress the blend before embedding it)
71 blend_file = open(blend_path, 'rb')
72 head = blend_file.read(7)
73 blend_file.seek(0)
74 if head[0:2] == b'\x1f\x8b': # gzip magic
75 import gzip
76 blend_file.close()
77 blend_file = gzip.open(blend_path, 'rb')
78 blend_d = blend_file.read()
79 blend_file.close()
82 # Get rid of the tmp blend, we're done with it
83 os.remove(blend_path)
85 # Create a new file for the bundled runtime
86 output = open(output_path, 'wb')
88 # Write the player and blend data to the new runtime
89 output.write(player_d)
90 output.write(blend_d)
92 # Store the offset (an int is 4 bytes, so we split it up into 4 bytes and save it)
93 output.write(struct.pack('B', (offset>>24)&0xFF))
94 output.write(struct.pack('B', (offset>>16)&0xFF))
95 output.write(struct.pack('B', (offset>>8)&0xFF))
96 output.write(struct.pack('B', (offset>>0)&0xFF))
98 # Stuff for the runtime
99 output.write(b'BRUNTIME')
100 output.close()
102 # Make the runtime executable on Linux
103 if os.name == 'posix':
104 os.chmod(output_path, 0o755)
107 from bpy.props import *
110 class SaveAsRuntime(bpy.types.Operator):
111 bl_idname = "wm.save_as_runtime"
112 bl_label = "Save As Runtime"
113 bl_options = {'REGISTER'}
115 blender_bin_path = bpy.app.binary_path
116 blender_bin_dir = os.path.dirname(blender_bin_path)
117 ext = os.path.splitext(blender_bin_path)[-1]
119 default_player_path = os.path.join(blender_bin_dir, 'blenderplayer' + ext)
120 player_path = StringProperty(name="Player Path", description="The path to the player to use", default=default_player_path)
121 filepath = StringProperty(name="Output Path", description="Where to save the runtime", default="")
123 def execute(self, context):
124 import time
125 start_time = time.clock()
126 print("Saving runtime to", self.properties.filepath)
127 WriteRuntime(self.properties.player_path,
128 self.properties.filepath)
129 print("Finished in %.4fs" % (time.clock()-start_time))
130 return {'FINISHED'}
132 def invoke(self, context, event):
133 wm = context.window_manager
134 wm.add_fileselect(self)
135 return {'RUNNING_MODAL'}
138 def menu_func(self, context):
140 ext = os.path.splitext(bpy.app.binary_path)[-1]
141 default_blend_path = bpy.data.filepath.replace(".blend", ext)
142 self.layout.operator(SaveAsRuntime.bl_idname, text=SaveAsRuntime.bl_label).filepath = default_blend_path
145 def register():
146 bpy.types.INFO_MT_file_export.append(menu_func)
149 def unregister():
150 bpy.types.INFO_MT_file_export.remove(menu_func)
153 if __name__ == "__main__":
154 register()