From 86bd3cc31b636070da779f21b1fdc128faf1fc5d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 24 May 2011 22:20:54 +0000 Subject: axis conversion for X3D and OBJ operators --- io_scene_obj/__init__.py | 77 +++++++++++++++++++++++++++++++++++++++++----- io_scene_obj/export_obj.py | 25 ++++++--------- io_scene_obj/import_obj.py | 24 ++++++++------- 3 files changed, 93 insertions(+), 33 deletions(-) (limited to 'io_scene_obj') diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py index b5f3cb9b..64d429d5 100644 --- a/io_scene_obj/__init__.py +++ b/io_scene_obj/__init__.py @@ -43,7 +43,7 @@ if "bpy" in locals(): import bpy from bpy.props import BoolProperty, FloatProperty, StringProperty, EnumProperty -from bpy_extras.io_utils import ExportHelper, ImportHelper, path_reference_mode +from bpy_extras.io_utils import ExportHelper, ImportHelper, path_reference_mode, axis_conversion class ImportOBJ(bpy.types.Operator, ImportHelper): @@ -64,8 +64,6 @@ class ImportOBJ(bpy.types.Operator, ImportHelper): use_groups_as_vgroups = BoolProperty(name="Poly Groups", description="Import OBJ groups as vertex groups.", default=False) - use_rotate_x90 = BoolProperty(name="-X90", description="Rotate X 90.", default=True) - global_clamp_size = FloatProperty(name="Clamp Scale", description="Clamp the size to this maximum (Zero to Disable)", min=0.0, max=1000.0, soft_min=0.0, soft_max=1000.0, default=0.0) use_image_search = BoolProperty(name="Image Search", description="Search subdirs for any assosiated images (Warning, may be slow)", default=True) split_mode = EnumProperty( @@ -75,12 +73,38 @@ class ImportOBJ(bpy.types.Operator, ImportHelper): ), ) + global_clamp_size = FloatProperty(name="Clamp Scale", description="Clamp the size to this maximum (Zero to Disable)", min=0.0, max=1000.0, soft_min=0.0, soft_max=1000.0, default=0.0) + global_axis_forward = EnumProperty( + name="Forward", + items=(('X', "X Forward", ""), + ('Y', "Y Forward", ""), + ('Z', "Z Forward", ""), + ('-X', "-X Forward", ""), + ('-Y', "-Y Forward", ""), + ('-Z', "-Z Forward", ""), + ), + default='-Z', + ) + + global_axis_up = EnumProperty( + name="Up", + items=(('X', "X Up", ""), + ('Y', "Y Up", ""), + ('Z', "Z Up", ""), + ('-X', "-X Up", ""), + ('-Y', "-Y Up", ""), + ('-Z', "-Z Up", ""), + ), + default='Y', + ) + # fake prop, only disables split. # keep_vertex_order = BoolProperty(name="Keep Vert Order", description="Keep vert and face order, disables split options, enable for morph targets", default= True) def execute(self, context): # print("Selected: " + context.active_object.name) from . import import_obj + from mathutils import Matrix if self.split_mode == 'OFF': self.use_split_objects = False @@ -88,7 +112,12 @@ class ImportOBJ(bpy.types.Operator, ImportHelper): else: self.use_groups_as_vgroups = False - return import_obj.load(self, context, **self.as_keywords(ignore=("filter_glob", "split_mode"))) + keywords = self.as_keywords(ignore=("global_axis_forward", "global_axis_up", "filter_glob", "split_mode")) + + global_matrix = axis_conversion(from_forward=self.global_axis_forward, from_up=self.global_axis_up).to_4x4() + keywords["global_matrix"] = global_matrix + + return import_obj.load(self, context, **keywords) def draw(self, context): layout = self.layout @@ -113,7 +142,8 @@ class ImportOBJ(bpy.types.Operator, ImportHelper): row = layout.split(percentage=0.67) row.prop(self, "global_clamp_size") - row.prop(self, "use_rotate_x90") + layout.prop(self, "global_axis_forward") + layout.prop(self, "global_axis_up") layout.prop(self, "use_image_search") @@ -138,7 +168,6 @@ class ExportOBJ(bpy.types.Operator, ExportHelper): # object group use_apply_modifiers = BoolProperty(name="Apply Modifiers", description="Apply modifiers (preview resolution)", default=True) - use_rotate_x90 = BoolProperty(name="Rotate X90", description="", default=True) # extra data group use_edges = BoolProperty(name="Edges", description="", default=True) @@ -157,11 +186,45 @@ class ExportOBJ(bpy.types.Operator, ExportHelper): group_by_material = BoolProperty(name="Material Groups", description="", default=False) keep_vertex_order = BoolProperty(name="Keep Vertex Order", description="", default=False) + global_scale = FloatProperty(name="Scale", description="Scale all data, (Note! some imports dont support scaled armatures)", min=0.01, max=1000.0, soft_min=0.01, soft_max=1000.0, default=1.0) + + global_axis_forward = EnumProperty( + name="Forward", + items=(('X', "X Forward", ""), + ('Y', "Y Forward", ""), + ('Z', "Z Forward", ""), + ('-X', "-X Forward", ""), + ('-Y', "-Y Forward", ""), + ('-Z', "-Z Forward", ""), + ), + default='-Z', + ) + + global_axis_up = EnumProperty( + name="Up", + items=(('X', "X Up", ""), + ('Y', "Y Up", ""), + ('Z', "Z Up", ""), + ('-X', "-X Up", ""), + ('-Y', "-Y Up", ""), + ('-Z', "-Z Up", ""), + ), + default='Y', + ) + path_mode = path_reference_mode def execute(self, context): from . import export_obj - return export_obj.save(self, context, **self.as_keywords(ignore=("check_existing", "filter_glob"))) + + from mathutils import Matrix + keywords = self.as_keywords(ignore=("global_axis_forward", "global_axis_up", "global_scale", "check_existing", "filter_glob")) + + global_matrix = Matrix() + global_matrix[0][0] = global_matrix[1][1] = global_matrix[2][2] = self.global_scale + global_matrix = global_matrix * axis_conversion(to_forward=self.global_axis_forward, to_up=self.global_axis_up).to_4x4() + keywords["global_matrix"] = global_matrix + return export_obj.save(self, context, **keywords) def menu_func_import(self, context): diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py index 03a866ca..7e2aa1ed 100644 --- a/io_scene_obj/export_obj.py +++ b/io_scene_obj/export_obj.py @@ -236,13 +236,13 @@ def write_file(filepath, objects, scene, EXPORT_UV=True, EXPORT_MTL=True, EXPORT_APPLY_MODIFIERS=True, - EXPORT_ROTX90=True, EXPORT_BLEN_OBS=True, EXPORT_GROUP_BY_OB=False, EXPORT_GROUP_BY_MAT=False, EXPORT_KEEP_VERT_ORDER=False, EXPORT_POLYGROUPS=False, EXPORT_CURVE_AS_NURBS=True, + EXPORT_GLOBAL_MATRIX=None, EXPORT_PATH_MODE='AUTO', ): ''' @@ -252,6 +252,9 @@ def write_file(filepath, objects, scene, write( 'c:\\test\\foobar.obj', Blender.Object.GetSelected() ) # Using default options. ''' + if EXPORT_GLOBAL_MATRIX is None: + EXPORT_GLOBAL_MATRIX = mathutils.Matrix() + # XXX import math @@ -303,9 +306,6 @@ def write_file(filepath, objects, scene, mtlfilepath = os.path.splitext(filepath)[0] + ".mtl" file.write('mtllib %s\n' % repr(os.path.basename(mtlfilepath))[1:-1]) # filepath can contain non utf8 chars, use repr - if EXPORT_ROTX90: - mat_xrot90 = mathutils.Matrix.Rotation(-math.pi / 2.0, 4, 'X') - # Initialize totals, these are updated each object totverts = totuvco = totno = 1 @@ -345,8 +345,7 @@ def write_file(filepath, objects, scene, # Nurbs curve support if EXPORT_CURVE_AS_NURBS and test_nurbs_compat(ob): - if EXPORT_ROTX90: - ob_mat = ob_mat * mat_xrot90 + ob_mat = ob_mat * EXPORT_GLOBAL_MATRIX totverts += write_nurb(file, ob, ob_mat) continue # END NURBS @@ -355,11 +354,7 @@ def write_file(filepath, objects, scene, continue me = ob.to_mesh(scene, EXPORT_APPLY_MODIFIERS, 'PREVIEW') - - if EXPORT_ROTX90: - me.transform(mat_xrot90 * ob_mat) - else: - me.transform(ob_mat) + me.transform(ob_mat * EXPORT_GLOBAL_MATRIX) # # Will work for non meshes now! :) # me= BPyMesh.getMeshFromObject(ob, containerMesh, EXPORT_APPLY_MODIFIERS, EXPORT_POLYGROUPS, scn) @@ -670,7 +665,6 @@ def _write(context, filepath, EXPORT_UV, # ok EXPORT_MTL, EXPORT_APPLY_MODIFIERS, # ok - EXPORT_ROTX90, # wrong EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, @@ -680,6 +674,7 @@ def _write(context, filepath, EXPORT_SEL_ONLY, # ok EXPORT_ALL_SCENES, # XXX not working atm EXPORT_ANIMATION, + EXPORT_GLOBAL_MATRIX, EXPORT_PATH_MODE, ): # Not used @@ -739,13 +734,13 @@ def _write(context, filepath, EXPORT_UV, EXPORT_MTL, EXPORT_APPLY_MODIFIERS, - EXPORT_ROTX90, EXPORT_BLEN_OBS, EXPORT_GROUP_BY_OB, EXPORT_GROUP_BY_MAT, EXPORT_KEEP_VERT_ORDER, EXPORT_POLYGROUPS, EXPORT_CURVE_AS_NURBS, + EXPORT_GLOBAL_MATRIX, EXPORT_PATH_MODE, ) @@ -771,7 +766,6 @@ def save(operator, context, filepath="", use_uvs=True, use_materials=True, use_apply_modifiers=True, - use_rotate_x90=True, use_blen_objects=True, group_by_object=False, group_by_material=False, @@ -781,6 +775,7 @@ def save(operator, context, filepath="", use_selection=True, use_all_scenes=False, use_animation=False, + global_matrix=None, path_mode='AUTO' ): @@ -792,7 +787,6 @@ def save(operator, context, filepath="", EXPORT_UV=use_uvs, EXPORT_MTL=use_materials, EXPORT_APPLY_MODIFIERS=use_apply_modifiers, - EXPORT_ROTX90=use_rotate_x90, EXPORT_BLEN_OBS=use_blen_objects, EXPORT_GROUP_BY_OB=group_by_object, EXPORT_GROUP_BY_MAT=group_by_material, @@ -802,6 +796,7 @@ def save(operator, context, filepath="", EXPORT_SEL_ONLY=use_selection, EXPORT_ALL_SCENES=use_all_scenes, EXPORT_ANIMATION=use_animation, + EXPORT_GLOBAL_MATRIX=global_matrix, EXPORT_PATH_MODE=path_mode, ) diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py index a9708121..23f42446 100644 --- a/io_scene_obj/import_obj.py +++ b/io_scene_obj/import_obj.py @@ -961,10 +961,13 @@ def get_float_func(filepath): line = line.lstrip() if line.startswith(b'v'): # vn vt v if b',' in line: + file.close() return lambda f: float(f.replace(b',', b'.')) elif b'.' in line: + file.close() return float + file.close() # incase all vert values were ints return float @@ -976,9 +979,10 @@ def load(operator, context, filepath, use_edges=True, use_split_objects=True, use_split_groups=True, - use_rotate_x90=True, use_image_search=True, - use_groups_as_vgroups=False): + use_groups_as_vgroups=False, + global_matrix=None, + ): ''' Called by the user interface or another script. load_obj(path) - should give acceptable results. @@ -989,6 +993,9 @@ def load(operator, context, filepath, filepath = os.fsencode(filepath) + if global_matrix is None: + global_matrix = mathutils.Matrix() + if use_split_objects or use_split_groups: use_groups_as_vgroups = False @@ -1039,8 +1046,7 @@ def load(operator, context, filepath, if line.startswith(b"v "): line_split = line.split() - # rotate X90: (x,-z,y) - verts_loc.append((float_func(line_split[1]), -float_func(line_split[3]), float_func(line_split[2]))) + verts_loc.append((float_func(line_split[1]), float_func(line_split[2]), float_func(line_split[3]))) elif line.startswith(b"vn "): pass @@ -1247,9 +1253,6 @@ def load(operator, context, filepath, print("%.4f sec" % (time_new - time_sub)) time_sub = time_new - if not use_rotate_x90: - verts_loc[:] = [(v[0], v[2], -v[1]) for v in verts_loc] - # deselect all if bpy.ops.object.select_all.poll(): bpy.ops.object.select_all(action='DESELECT') @@ -1278,6 +1281,9 @@ def load(operator, context, filepath, base = scene.objects.link(obj) base.select = True + # we could apply this anywhere before scaling. + obj.matrix_world = global_matrix + scene.update() axis_min = [1000000000] * 3 @@ -1303,10 +1309,6 @@ def load(operator, context, filepath, for obj in new_objects: obj.scale = scale, scale, scale - # Better rotate the vert locations - #if not use_rotate_x90: - # for ob in new_objects: - # ob.RotX = -1.570796326794896558 time_new = time.time() -- cgit v1.2.3