Fix for invalid break statement outside loop.
[blender-addons-contrib.git] / io_scene_fpx / fpx_utils.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
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.
7 #
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.
12 #
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 ###############################################################################
22 #234567890123456789012345678901234567890123456789012345678901234567890123456789
23 #--------1---------2---------3---------4---------5---------6---------7---------
24
25
26 # ##### BEGIN COPYRIGHT BLOCK #####
27 #
28 # initial script copyright (c)2013 Alexander Nussbaumer
29 #
30 # ##### END COPYRIGHT BLOCK #####
31
32
33 #import blender stuff
34 from bpy import (
35         ops,
36         )
37 from bpy.path import (
38         resolve_ncase,
39         )
40 from os import (
41         path,
42         )
43
44
45 class FpxUtilities:
46
47     ###########################################################################
48     TAG_NAME = "raw_dump"
49
50     @staticmethod
51     def str_begin_tag(value):
52         print("<{}>".format(value))
53
54     @staticmethod
55     def str_end_tag(value):
56         print("</{}>".format(value))
57
58     @staticmethod
59     def dump_bin(dump, address=0, comment="", max_size=0x0000000FFFFF, sector_list=None, sector_size=None, add_tags=True):
60         if dump is None:
61             return
62         tag_name = FpxUtilities.TAG_NAME
63         gap_hex = "|| "
64         default_marker_left = ' ('
65         default_marker_right = ')  '
66         view_output = []
67         view_chr = []
68         max_index = len(dump) - 1
69         last_sector_view_address = -1
70
71         if max_index > max_size:
72             max_index = max_size
73
74         if add_tags:
75             FpxUtilities.str_begin_tag(tag_name)
76
77         for index, value in enumerate(dump):
78             view_address = index + address
79             view_address_16 = view_address & 15
80
81             # cut of value to char
82             if isinstance(value, str):
83                 if (value >= chr(32) and value < chr(127)):
84                     value_chr = value
85                 else:
86                     value_chr = '.'
87             else:
88                 if (value >= 32 and value < 127):
89                     value_chr = chr(value)
90                 else:
91                     value_chr = '.'
92             if (value >= 32 and value < 127):
93                 value_chr = chr(value)
94             else:
95                 value_chr = '.'
96
97             if index == 0 or view_address_16 == 0:
98                 # show address
99                 if sector_list is not None and sector_size is not None:
100                     # address must be 0
101                     sector_index = view_address // sector_size
102                     sector_view_address = ((view_address % sector_size) + sector_list[sector_index]) & ~15
103                     if index and sector_view_address != (last_sector_view_address & ~15) + 16:
104                         marker_left = '[['
105                         marker_right = ']]]'
106                     else:
107                         marker_left = default_marker_left
108                         marker_right = default_marker_right
109                     view_output.append("{:08X}{}{:08X}{}".format(sector_view_address, marker_left, index, marker_right))
110                     last_sector_view_address = sector_view_address
111                 else:
112                     view_output.append("{:08X}{}{:08X}{}".format(view_address_16, default_marker_left, index, default_marker_right))
113
114             if index == 0 and view_address_16 != 0:
115                 # fill gap between 'address' and  'data'
116                 # and between 'data' and 'char'
117                 for n in range(view_address_16):
118                     view_output.append(gap_hex)
119                     view_chr.append(" ")
120
121             if isinstance(value, str):
122                 view_output.append("{:02X} ".format(ord(value)))
123             else:
124                 view_output.append("{:02X} ".format(value))
125             view_chr.append(value_chr)
126
127             if index >= max_index or view_address_16 == 15:
128                 if view_address_16 != 15:
129                     # fill gap between 'data' and 'char'
130                     for n in range(view_address_16, 15):
131                         view_output.append(gap_hex)
132
133                 # merge all
134                 view_output.append(" ")
135                 view_output.append("".join(view_chr))
136                 view_chr = []
137
138                 if comment:
139                     if view_address_16 != 15:
140                         # fill gap between 'char' and 'comment'
141                         for n in range(view_address_16, 15):
142                             view_output.append(" ")
143                     view_output.append("  ")
144                     view_output.append(comment)
145                     comment = None
146
147                 if index < max_index:
148                     view_output.append("\n")
149
150                 if index >= max_index:
151                     break
152
153         print("".join(view_output))
154
155         if add_tags:
156             FpxUtilities.str_end_tag(tag_name)
157
158
159     ###########################################################################
160     @staticmethod
161     def toGoodName(s):
162         if not s:
163             return s
164
165         sx = []
166         for c in s:
167             if (
168                     (c != '_') and (c != '.') and
169                     (c != '[') and (c != ']') and
170                     (c != '(') and (c != ')') and
171                     (c != '{') and (c != '}') and
172                     (c < '0' or c > '9') and
173                     (c < 'A' or c > 'Z') and
174                     (c < 'a' or c > 'z')
175                     ):
176                 c = '_'
177             sx.append(c)
178         return str().join(sx).lower().strip(". ")
179
180     ###########################################################################
181     @staticmethod
182     def toGoodFilePath(s):
183         """ source path/filenames are based on windows systems """
184         if not s:
185             return s
186
187         # detecting custom operating system
188         if path.sep != '\\':
189             # replace windows sep to custom os sep
190             s = s.replace('\\', path.sep)
191
192             # find and cutoff drive letter
193             i = s.find(':')
194             if i > -1:
195                 s = s[i + 1:]
196
197         # try to handle case sensitive names in case of such os
198         s = resolve_ncase(s)
199
200         return s
201
202     ###########################################################################
203     @staticmethod
204     def enable_edit_mode(enable, blender_context):
205         if blender_context.active_object is None or not blender_context.active_object.type in {'CURVE', 'MESH', 'ARMATURE', }:
206             return
207
208         if enable:
209             modeString = 'EDIT'
210         else:
211             modeString = 'OBJECT'
212
213         if ops.object.mode_set.poll():
214             ops.object.mode_set(mode=modeString)
215
216     ###########################################################################
217     @staticmethod
218     def select_all(select):
219         if select:
220             actionString = 'SELECT'
221         else:
222             actionString = 'DESELECT'
223
224         if ops.object.select_all.poll():
225             ops.object.select_all(action=actionString)
226         elif ops.curve.select_all.poll():
227             ops.curve.select_all(action=actionString)
228         elif ops.mesh.select_all.poll():
229             ops.mesh.select_all(action=actionString)
230         elif ops.pose.select_all.poll():
231             ops.pose.select_all(action=actionString)
232
233
234     ###########################################################################
235     @staticmethod
236     def set_scene_to_metric(blender_context):
237         # set metrics
238         blender_context.scene.unit_settings.system = 'METRIC'
239         blender_context.scene.unit_settings.system_rotation = 'DEGREES'
240         blender_context.scene.unit_settings.scale_length = 0.001 # 1.0mm
241         blender_context.scene.unit_settings.use_separate = False
242
243         blender_context.tool_settings.normal_size = 1.0 # 1.0mm
244
245         # set all 3D views to texture shaded
246         # and set up the clipping
247         for screen in blender_context.blend_data.screens:
248             for area in screen.areas:
249                 if (area.type != 'VIEW_3D'):
250                     continue
251
252                 for space in area.spaces:
253                     if (space.type != 'VIEW_3D'):
254                         continue
255                     #adjust clipping to new units
256                     space.clip_start = 0.1 # 0.1mm
257                     space.clip_end = 1000000.0 # 1km
258
259                     #space.viewport_shade = 'SOLID'
260                     #space.show_textured_solid = True
261                     space.show_backface_culling = True
262
263             screen.scene.tool_settings.normal_size = 10
264
265             #screen.scene.game_settings.material_mode = 'MULTITEXTURE'
266
267
268     ###########################################################################
269     @staticmethod
270     def set_scene_to_default(blender_scene):
271         # set default
272         blender_scene.unit_settings.system = 'NONE'
273         blender_scene.unit_settings.system_rotation = 'DEGREES'
274         blender_scene.unit_settings.scale_length = 1.0
275         blender_scene.unit_settings.use_separate = False
276
277
278     ###########################################################################
279
280 ###############################################################################
281
282
283 ###############################################################################
284 #234567890123456789012345678901234567890123456789012345678901234567890123456789
285 #--------1---------2---------3---------4---------5---------6---------7---------
286 # ##### END OF FILE #####