diff --git a/io_scene_3ds/__init__.py b/io_scene_3ds/__init__.py deleted file mode 100644 index 9430ca410aeb3a7fbe48ac0f36537e9d81c54fdc..0000000000000000000000000000000000000000 --- a/io_scene_3ds/__init__.py +++ /dev/null @@ -1,175 +0,0 @@ -# ##### BEGIN GPL LICENSE BLOCK ##### -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -from bpy_extras.io_utils import ( - ImportHelper, - ExportHelper, - orientation_helper, - axis_conversion, -) -from bpy.props import ( - BoolProperty, - EnumProperty, - FloatProperty, - StringProperty, -) -import bpy -bl_info = { - "name": "Autodesk 3DS format", - "author": "Bob Holcomb, Campbell Barton, Andreas Atteneder, Sebastian Schrand", - "version": (2, 3, 1), - "blender": (3, 0, 0), - "location": "File > Import", - "description": "Import 3DS, meshes, uvs, materials, textures, " - "cameras & lamps", - "warning": "Images must be in file folder", - "doc_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/" - "Scripts/Import-Export/Autodesk_3DS", - "category": "Import-Export", -} - -if "bpy" in locals(): - import importlib - if "import_3ds" in locals(): - importlib.reload(import_3ds) - if "export_3ds" in locals(): - importlib.reload(export_3ds) - - -@orientation_helper(axis_forward='Y', axis_up='Z') -class Import3DS(bpy.types.Operator, ImportHelper): - """Import from 3DS file format (.3ds)""" - bl_idname = "import_scene.autodesk_3ds" - bl_label = 'Import 3DS' - bl_options = {'UNDO'} - - filename_ext = ".3ds" - filter_glob: StringProperty(default="*.3ds", options={'HIDDEN'}) - - constrain_size: FloatProperty( - name="Size Constraint", - description="Scale the model by 10 until it reaches the " - "size constraint (0 to disable)", - min=0.0, max=1000.0, - soft_min=0.0, soft_max=1000.0, - default=10.0, - ) - use_image_search: BoolProperty( - name="Image Search", - description="Search subdirectories for any associated images " - "(Warning, may be slow)", - default=True, - ) - use_apply_transform: BoolProperty( - name="Apply Transform", - description="Workaround for object transformations " - "importing incorrectly", - default=True, - ) - - read_keyframe: bpy.props.BoolProperty( - name="Read Keyframe", - description="Read the keyframe data", - default=True, - ) - - def execute(self, context): - from . import import_3ds - - keywords = self.as_keywords(ignore=("axis_forward", - "axis_up", - "filter_glob", - )) - - global_matrix = axis_conversion(from_forward=self.axis_forward, - from_up=self.axis_up, - ).to_4x4() - keywords["global_matrix"] = global_matrix - - return import_3ds.load(self, context, **keywords) - - -@orientation_helper(axis_forward='Y', axis_up='Z') -class Export3DS(bpy.types.Operator, ExportHelper): - """Export to 3DS file format (.3ds)""" - bl_idname = "export_scene.autodesk_3ds" - bl_label = 'Export 3DS' - - filename_ext = ".3ds" - filter_glob: StringProperty( - default="*.3ds", - options={'HIDDEN'}, - ) - - use_selection: BoolProperty( - name="Selection Only", - description="Export selected objects only", - default=False, - ) - - def execute(self, context): - from . import export_3ds - - keywords = self.as_keywords(ignore=("axis_forward", - "axis_up", - "filter_glob", - "check_existing", - )) - global_matrix = axis_conversion(to_forward=self.axis_forward, - to_up=self.axis_up, - ).to_4x4() - keywords["global_matrix"] = global_matrix - - return export_3ds.save(self, context, **keywords) - - -# Add to a menu -def menu_func_export(self, context): - self.layout.operator(Export3DS.bl_idname, text="3D Studio (.3ds)") - - -def menu_func_import(self, context): - self.layout.operator(Import3DS.bl_idname, text="3D Studio (.3ds)") - - -def register(): - bpy.utils.register_class(Import3DS) - bpy.utils.register_class(Export3DS) - - bpy.types.TOPBAR_MT_file_import.append(menu_func_import) - bpy.types.TOPBAR_MT_file_export.append(menu_func_export) - - -def unregister(): - bpy.utils.unregister_class(Import3DS) - bpy.utils.unregister_class(Export3DS) - - bpy.types.TOPBAR_MT_file_import.remove(menu_func_import) - bpy.types.TOPBAR_MT_file_export.remove(menu_func_export) - -# NOTES: -# why add 1 extra vertex? and remove it when done? - -# "Answer - eekadoodle - would need to re-order UV's without this since face -# order isnt always what we give blender, BMesh will solve :D" -# -# disabled scaling to size, this requires exposing bb (easy) and understanding -# how it works (needs some time) - - -if __name__ == "__main__": - register() diff --git a/io_scene_3ds/export_3ds.py b/io_scene_3ds/export_3ds.py deleted file mode 100644 index 55053222adaf7868b50031449543f8242324a449..0000000000000000000000000000000000000000 --- a/io_scene_3ds/export_3ds.py +++ /dev/null @@ -1,1454 +0,0 @@ -# ##### BEGIN GPL LICENSE BLOCK ##### -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -# Script copyright (C) Bob Holcomb -# Contributors: Campbell Barton, Bob Holcomb, Richard Lärkäng, Damien McGinnes, Mark Stijnman, Sebastian Sille - -""" -Exporting is based on 3ds loader from www.gametutorials.com(Thanks DigiBen) and using information -from the lib3ds project (http://lib3ds.sourceforge.net/) sourcecode. -""" - -import bpy -import math -import struct -import mathutils -import bpy_extras -from bpy_extras import node_shader_utils - -###################################################### -# Data Structures -###################################################### - -# Some of the chunks that we will export -# ----- Primary Chunk, at the beginning of each file -PRIMARY = 0x4D4D - -# ------ Main Chunks -VERSION = 0x0002 # This gives the version of the .3ds file -KFDATA = 0xB000 # This is the header for all of the key frame info - -# ------ sub defines of OBJECTINFO -OBJECTINFO = 0x3D3D # Main mesh object chunk before the material and object information -MESHVERSION = 0x3D3E # This gives the version of the mesh -AMBIENTLIGHT = 0x2100 # The color of the ambient light -MATERIAL = 45055 # 0xAFFF // This stored the texture info -OBJECT = 16384 # 0x4000 // This stores the faces, vertices, etc... - -# >------ sub defines of MATERIAL -MATNAME = 0xA000 # This holds the material name -MATAMBIENT = 0xA010 # Ambient color of the object/material -MATDIFFUSE = 0xA020 # This holds the color of the object/material -MATSPECULAR = 0xA030 # Specular color of the object/material -MATSHINESS = 0xA040 # Specular intensity of the object/material (percent) -MATSHIN2 = 0xA041 # Reflection of the object/material (percent) -MATSHIN3 = 0xA042 # metallic/mirror of the object/material (percent) -MATTRANS = 0xA050 # Transparency value (100-OpacityValue) (percent) -MATSELFILPCT = 0xA084 # Self illumination strength (percent) -MATSHADING = 0xA100 # Material shading method - -MAT_DIFFUSEMAP = 0xA200 # This is a header for a new diffuse texture -MAT_SPECMAP = 0xA204 # head for specularity map -MAT_OPACMAP = 0xA210 # head for opacity map -MAT_REFLMAP = 0xA220 # head for reflect map -MAT_BUMPMAP = 0xA230 # head for normal map -MAT_BUMP_PERCENT = 0xA252 # Normalmap strength (percent) -MAT_TEX2MAP = 0xA33A # head for secondary texture -MAT_SHINMAP = 0xA33C # head for roughness map -MAT_SELFIMAP = 0xA33D # head for emission map - -# >------ sub defines of MAT_MAP -MATMAPFILE = 0xA300 # This holds the file name of a texture -MAT_MAP_TILING = 0xa351 # 2nd bit (from LSB) is mirror UV flag -MAT_MAP_TEXBLUR = 0xA353 # Texture blurring factor -MAT_MAP_USCALE = 0xA354 # U axis scaling -MAT_MAP_VSCALE = 0xA356 # V axis scaling -MAT_MAP_UOFFSET = 0xA358 # U axis offset -MAT_MAP_VOFFSET = 0xA35A # V axis offset -MAT_MAP_ANG = 0xA35C # UV rotation around the z-axis in rad -MAP_COL1 = 0xA360 # Tint Color1 -MAP_COL2 = 0xA362 # Tint Color2 -MAP_RCOL = 0xA364 # Red tint -MAP_GCOL = 0xA366 # Green tint -MAP_BCOL = 0xA368 # Blue tint - -RGB = 0x0010 # RGB float -RGB1 = 0x0011 # RGB Color1 -RGB2 = 0x0012 # RGB Color2 -PCT = 0x0030 # Percent chunk -MASTERSCALE = 0x0100 # Master scale factor - -# >------ sub defines of OBJECT -OBJECT_MESH = 0x4100 # This lets us know that we are reading a new object -OBJECT_LIGHT = 0x4600 # This lets us know we are reading a light object -OBJECT_CAMERA = 0x4700 # This lets us know we are reading a camera object - -# >------ Sub defines of LIGHT -LIGHT_MULTIPLIER = 0x465B # The light energy factor -LIGHT_SPOTLIGHT = 0x4610 # The target of a spotlight -LIGHT_SPOTROLL = 0x4656 # The roll angle of the spot - -# >------ sub defines of CAMERA -OBJECT_CAM_RANGES = 0x4720 # The camera range values - -# >------ sub defines of OBJECT_MESH -OBJECT_VERTICES = 0x4110 # The objects vertices -OBJECT_VERTFLAGS = 0x4111 # The objects vertex flags -OBJECT_FACES = 0x4120 # The objects faces -OBJECT_MATERIAL = 0x4130 # This is found if the object has a material, either texture map or color -OBJECT_UV = 0x4140 # The UV texture coordinates -OBJECT_SMOOTH = 0x4150 # The objects smooth groups -OBJECT_TRANS_MATRIX = 0x4160 # The Object Matrix - -# >------ sub defines of KFDATA -KFDATA_KFHDR = 0xB00A -KFDATA_KFSEG = 0xB008 -KFDATA_KFCURTIME = 0xB009 -KFDATA_OBJECT_NODE_TAG = 0xB002 - -# >------ sub defines of OBJECT_NODE_TAG -OBJECT_NODE_ID = 0xB030 -OBJECT_NODE_HDR = 0xB010 -OBJECT_PIVOT = 0xB013 -OBJECT_INSTANCE_NAME = 0xB011 -POS_TRACK_TAG = 0xB020 -ROT_TRACK_TAG = 0xB021 -SCL_TRACK_TAG = 0xB022 - - -# So 3ds max can open files, limit names to 12 in length -# this is very annoying for filenames! -name_unique = [] # stores str, ascii only -name_mapping = {} # stores {orig: byte} mapping - - -def sane_name(name): - name_fixed = name_mapping.get(name) - if name_fixed is not None: - return name_fixed - - # strip non ascii chars - new_name_clean = new_name = name.encode("ASCII", "replace").decode("ASCII")[:12] - i = 0 - - while new_name in name_unique: - new_name = new_name_clean + ".%.3d" % i - i += 1 - - # note, appending the 'str' version. - name_unique.append(new_name) - name_mapping[name] = new_name = new_name.encode("ASCII", "replace") - return new_name - - -def uv_key(uv): - return round(uv[0], 6), round(uv[1], 6) - - -# size defines: -SZ_SHORT = 2 -SZ_INT = 4 -SZ_FLOAT = 4 - - -class _3ds_ushort(object): - """Class representing a short (2-byte integer) for a 3ds file. - *** This looks like an unsigned short H is unsigned from the struct docs - Cam***""" - __slots__ = ("value", ) - - def __init__(self, val=0): - self.value = val - - def get_size(self): - return SZ_SHORT - - def write(self, file): - file.write(struct.pack("<H", self.value)) - - def __str__(self): - return str(self.value) - - -class _3ds_uint(object): - """Class representing an int (4-byte integer) for a 3ds file.""" - __slots__ = ("value", ) - - def __init__(self, val): - self.value = val - - def get_size(self): - return SZ_INT - - def write(self, file): - file.write(struct.pack("<I", self.value)) - - def __str__(self): - return str(self.value) - - -class _3ds_float(object): - """Class representing a 4-byte IEEE floating point number for a 3ds file.""" - __slots__ = ("value", ) - - def __init__(self, val): - self.value = val - - def get_size(self): - return SZ_FLOAT - - def write(self, file): - file.write(struct.pack("<f", self.value)) - - def __str__(self): - return str(self.value) - - -class _3ds_string(object): - """Class representing a zero-terminated string for a 3ds file.""" - __slots__ = ("value", ) - - def __init__(self, val): - assert(type(val) == bytes) - self.value = val - - def get_size(self): - return (len(self.value) + 1) - - def write(self, file): - binary_format = "<%ds" % (len(self.value) + 1) - file.write(struct.pack(binary_format, self.value)) - - def __str__(self): - return str(self.value) - - -class _3ds_point_3d(object): - """Class representing a three-dimensional point for a 3ds file.""" - __slots__ = "x", "y", "z" - - def __init__(self, point): - self.x, self.y, self.z = point - - def get_size(self): - return 3 * SZ_FLOAT - - def write(self, file): - file.write(struct.pack('<3f', self.x, self.y, self.z)) - - def __str__(self): - return '(%f, %f, %f)' % (self.x, self.y, self.z) - - -# Used for writing a track -''' -class _3ds_point_4d(object): - """Class representing a four-dimensional point for a 3ds file, for instance a quaternion.""" - __slots__ = "x","y","z","w" - def __init__(self, point=(0.0,0.0,0.0,0.0)): - self.x, self.y, self.z, self.w = point - - def get_size(self): - return 4*SZ_FLOAT - - def write(self,file): - data=struct.pack('<4f', self.x, self.y, self.z, self.w) - file.write(data) - - def __str__(self): - return '(%f, %f, %f, %f)' % (self.x, self.y, self.z, self.w) -''' - - -class _3ds_point_uv(object): - """Class representing a UV-coordinate for a 3ds file.""" - __slots__ = ("uv", ) - - def __init__(self, point): - self.uv = point - - def get_size(self): - return 2 * SZ_FLOAT - - def write(self, file): - data = struct.pack('<2f', self.uv[0], self.uv[1]) - file.write(data) - - def __str__(self): - return '(%g, %g)' % self.uv - - -class _3ds_float_color(object): - """Class representing a rgb float color for a 3ds file.""" - __slots__ = "r", "g", "b" - - def __init__(self, col): - self.r, self.g, self.b = col - - def get_size(self): - return 3 * SZ_FLOAT - - def write(self, file): - file.write(struct.pack('3f', self.r, self.g, self.b)) - - def __str__(self): - return '{%f, %f, %f}' % (self.r, self.g, self.b) - - -class _3ds_rgb_color(object): - """Class representing a (24-bit) rgb color for a 3ds file.""" - __slots__ = "r", "g", "b" - - def __init__(self, col): - self.r, self.g, self.b = col - - def get_size(self): - return 3 - - def write(self, file): - file.write(struct.pack('<3B', int(255 * self.r), int(255 * self.g), int(255 * self.b))) - - def __str__(self): - return '{%f, %f, %f}' % (self.r, self.g, self.b) - - -class _3ds_face(object): - """Class representing a face for a 3ds file.""" - __slots__ = ("vindex", "flag") - - def __init__(self, vindex, flag): - self.vindex = vindex - self.flag = flag - - def get_size(self): - return 4 * SZ_SHORT - - # no need to validate every face vert. the oversized array will - # catch this problem - - def write(self, file): - # The last short is used for face flags - file.write(struct.pack("<4H", self.vindex[0], self.vindex[1], self.vindex[2], self.flag)) - - def __str__(self): - return "[%d %d %d %d]" % (self.vindex[0], self.vindex[1], self.vindex[2], self.flag) - - -class _3ds_array(object): - """Class representing an array of variables for a 3ds file. - - Consists of a _3ds_ushort to indicate the number of items, followed by the items themselves. - """ - __slots__ = "values", "size" - - def __init__(self): - self.values = [] - self.size = SZ_SHORT - - # add an item: - def add(self, item): - self.values.append(item) - self.size += item.get_size() - - def get_size(self): - return self.size - - def validate(self): - return len(self.values) <= 65535 - - def write(self, file): - _3ds_ushort(len(self.values)).write(file) - for value in self.values: - value.write(file) - - # To not overwhelm the output in a dump, a _3ds_array only - # outputs the number of items, not all of the actual items. - def __str__(self): - return '(%d items)' % len(self.values) - - -class _3ds_named_variable(object): - """Convenience class for named variables.""" - - __slots__ = "value", "name" - - def __init__(self, name, val=None): - self.name = name - self.value = val - - def get_size(self): - if self.value is None: - return 0 - else: - return self.value.get_size() - - def write(self, file): - if self.value is not None: - self.value.write(file) - - def dump(self, indent): - if self.value is not None: - print(indent * " ", - self.name if self.name else "[unnamed]", - " = ", - self.value) - - -# the chunk class -class _3ds_chunk(object): - """Class representing a chunk in a 3ds file. - - Chunks contain zero or more variables, followed by zero or more subchunks. - """ - __slots__ = "ID", "size", "variables", "subchunks" - - def __init__(self, chunk_id=0): - self.ID = _3ds_ushort(chunk_id) - self.size = _3ds_uint(0) - self.variables = [] - self.subchunks = [] - - def add_variable(self, name, var): - """Add a named variable. - - The name is mostly for debugging purposes.""" - self.variables.append(_3ds_named_variable(name, var)) - - def add_subchunk(self, chunk): - """Add a subchunk.""" - self.subchunks.append(chunk) - - def get_size(self): - """Calculate the size of the chunk and return it. - - The sizes of the variables and subchunks are used to determine this chunk\'s size.""" - tmpsize = self.ID.get_size() + self.size.get_size() - for variable in self.variables: - tmpsize += variable.get_size() - for subchunk in self.subchunks: - tmpsize += subchunk.get_size() - self.size.value = tmpsize - return self.size.value - - def validate(self): - for var in self.variables: - func = getattr(var.value, "validate", None) - if (func is not None) and not func(): - return False - - for chunk in self.subchunks: - func = getattr(chunk, "validate", None) - if (func is not None) and not func(): - return False - - return True - - def write(self, file): - """Write the chunk to a file. - - Uses the write function of the variables and the subchunks to do the actual work.""" - # write header - self.ID.write(file) - self.size.write(file) - for variable in self.variables: - variable.write(file) - for subchunk in self.subchunks: - subchunk.write(file) - - def dump(self, indent=0): - """Write the chunk to a file. - - Dump is used for debugging purposes, to dump the contents of a chunk to the standard output. - Uses the dump function of the named variables and the subchunks to do the actual work.""" - print(indent * " ", - "ID=%r" % hex(self.ID.value), - "size=%r" % self.get_size()) - for variable in self.variables: - variable.dump(indent + 1) - for subchunk in self.subchunks: - subchunk.dump(indent + 1) - - -###################################################### -# EXPORT -###################################################### - -def get_material_image(material): - """ Get images from paint slots.""" - if material: - pt = material.paint_active_slot - tex = material.texture_paint_images - if pt < len(tex): - slot = tex[pt] - if slot.type == 'IMAGE': - return slot - - -def get_uv_image(ma): - """ Get image from material wrapper.""" - if ma and ma.use_nodes: - ma_wrap = node_shader_utils.PrincipledBSDFWrapper(ma) - ma_tex = ma_wrap.base_color_texture - if ma_tex and ma_tex.image is not None: - return ma_tex.image - else: - return get_material_image(ma) - - -def make_material_subchunk(chunk_id, color): - """Make a material subchunk. - - Used for color subchunks, such as diffuse color or ambient color subchunks.""" - mat_sub = _3ds_chunk(chunk_id) - col1 = _3ds_chunk(RGB1) - col1.add_variable("color1", _3ds_rgb_color(color)) - mat_sub.add_subchunk(col1) - # optional: - #col2 = _3ds_chunk(RGB1) - #col2.add_variable("color2", _3ds_rgb_color(color)) - # mat_sub.add_subchunk(col2) - return mat_sub - - -def make_percent_subchunk(chunk_id, percent): - """Make a percentage based subchunk.""" - pct_sub = _3ds_chunk(chunk_id) - pcti = _3ds_chunk(PCT) - pcti.add_variable("percent", _3ds_ushort(int(round(percent * 100, 0)))) - pct_sub.add_subchunk(pcti) - return pct_sub - - -def make_texture_chunk(chunk_id, images): - """Make Material Map texture chunk.""" - # Add texture percentage value (100 = 1.0) - ma_sub = make_percent_subchunk(chunk_id, 1) - has_entry = False - - def add_image(img): - filename = bpy.path.basename(image.filepath) - ma_sub_file = _3ds_chunk(MATMAPFILE) - ma_sub_file.add_variable("image", _3ds_string(sane_name(filename))) - ma_sub.add_subchunk(ma_sub_file) - - for image in images: - add_image(image) - has_entry = True - - return ma_sub if has_entry else None - - -def make_material_texture_chunk(chunk_id, texslots, pct): - """Make Material Map texture chunk given a seq. of `MaterialTextureSlot`'s - Paint slots are optionally used as image source if no nodes are - used. No additional filtering for mapping modes is done, all - slots are written "as is".""" - # Add texture percentage value - mat_sub = make_percent_subchunk(chunk_id, pct) - has_entry = False - - def add_texslot(texslot): - image = texslot.image - - filename = bpy.path.basename(image.filepath) - mat_sub_file = _3ds_chunk(MATMAPFILE) - mat_sub_file.add_variable("mapfile", _3ds_string(sane_name(filename))) - mat_sub.add_subchunk(mat_sub_file) - for link in texslot.socket_dst.links: - socket = link.from_socket.identifier - - maptile = 0 - - # no perfect mapping for mirror modes - 3DS only has uniform mirror w. repeat=2 - if texslot.extension == 'EXTEND': - maptile |= 0x1 - # CLIP maps to 3DS' decal flag - elif texslot.extension == 'CLIP': - maptile |= 0x10 - - mat_sub_tile = _3ds_chunk(MAT_MAP_TILING) - mat_sub_tile.add_variable("tiling", _3ds_ushort(maptile)) - mat_sub.add_subchunk(mat_sub_tile) - - if socket == 'Alpha': - mat_sub_alpha = _3ds_chunk(MAP_TILING) - alphaflag = 0x40 # summed area sampling 0x20 - mat_sub_alpha.add_variable("alpha", _3ds_ushort(alphaflag)) - mat_sub.add_subchunk(mat_sub_alpha) - if texslot.socket_dst.identifier in {'Base Color', 'Specular'}: - mat_sub_tint = _3ds_chunk(MAP_TILING) # RGB tint 0x200 - tint = 0x80 if texslot.image.colorspace_settings.name == 'Non-Color' else 0x200 - mat_sub_tint.add_variable("tint", _3ds_ushort(tint)) - mat_sub.add_subchunk(mat_sub_tint) - - mat_sub_texblur = _3ds_chunk(MAT_MAP_TEXBLUR) # Based on observation this is usually 1.0 - mat_sub_texblur.add_variable("maptexblur", _3ds_float(1.0)) - mat_sub.add_subchunk(mat_sub_texblur) - - mat_sub_uscale = _3ds_chunk(MAT_MAP_USCALE) - mat_sub_uscale.add_variable("mapuscale", _3ds_float(round(texslot.scale[0], 6))) - mat_sub.add_subchunk(mat_sub_uscale) - - mat_sub_vscale = _3ds_chunk(MAT_MAP_VSCALE) - mat_sub_vscale.add_variable("mapvscale", _3ds_float(round(texslot.scale[1], 6))) - mat_sub.add_subchunk(mat_sub_vscale) - - mat_sub_uoffset = _3ds_chunk(MAT_MAP_UOFFSET) - mat_sub_uoffset.add_variable("mapuoffset", _3ds_float(round(texslot.translation[0], 6))) - mat_sub.add_subchunk(mat_sub_uoffset) - - mat_sub_voffset = _3ds_chunk(MAT_MAP_VOFFSET) - mat_sub_voffset.add_variable("mapvoffset", _3ds_float(round(texslot.translation[1], 6))) - mat_sub.add_subchunk(mat_sub_voffset) - - mat_sub_angle = _3ds_chunk(MAT_MAP_ANG) - mat_sub_angle.add_variable("mapangle", _3ds_float(round(texslot.rotation[2], 6))) - mat_sub.add_subchunk(mat_sub_angle) - - if texslot.socket_dst.identifier in {'Base Color', 'Specular'}: - rgb = _3ds_chunk(MAP_COL1) # Add tint color - base = texslot.owner_shader.material.diffuse_color[:3] - spec = texslot.owner_shader.material.specular_color[:] - rgb.add_variable("mapcolor", _3ds_rgb_color(spec if texslot.socket_dst.identifier == 'Specular' else base)) - mat_sub.add_subchunk(rgb) - - # store all textures for this mapto in order. This at least is what - # the 3DS exporter did so far, afaik most readers will just skip - # over 2nd textures. - for slot in texslots: - if slot.image is not None: - add_texslot(slot) - has_entry = True - - return mat_sub if has_entry else None - - -def make_material_chunk(material, image): - """Make a material chunk out of a blender material. - Shading method is required for 3ds max, 0 for wireframe. - 0x1 for flat, 0x2 for gouraud, 0x3 for phong and 0x4 for metal.""" - material_chunk = _3ds_chunk(MATERIAL) - name = _3ds_chunk(MATNAME) - shading = _3ds_chunk(MATSHADING) - - name_str = material.name if material else "None" - - #if image: - # name_str += image.name - - name.add_variable("name", _3ds_string(sane_name(name_str))) - material_chunk.add_subchunk(name) - - if not material: - shading.add_variable("shading", _3ds_ushort(1)) # Flat shading - material_chunk.add_subchunk(make_material_subchunk(MATAMBIENT, (0.0, 0.0, 0.0))) - material_chunk.add_subchunk(make_material_subchunk(MATDIFFUSE, (0.8, 0.8, 0.8))) - material_chunk.add_subchunk(make_material_subchunk(MATSPECULAR, (1.0, 1.0, 1.0))) - material_chunk.add_subchunk(make_percent_subchunk(MATSHINESS, .2)) - material_chunk.add_subchunk(make_percent_subchunk(MATSHIN2, 1)) - material_chunk.add_subchunk(shading) - - elif material and material.use_nodes: - wrap = node_shader_utils.PrincipledBSDFWrapper(material) - shading.add_variable("shading", _3ds_ushort(3)) # Phong shading - material_chunk.add_subchunk(make_material_subchunk(MATAMBIENT, wrap.emission_color[:3])) - material_chunk.add_subchunk(make_material_subchunk(MATDIFFUSE, wrap.base_color[:3])) - material_chunk.add_subchunk(make_material_subchunk(MATSPECULAR, material.specular_color[:])) - material_chunk.add_subchunk(make_percent_subchunk(MATSHINESS, 1 - wrap.roughness)) - material_chunk.add_subchunk(make_percent_subchunk(MATSHIN2, wrap.specular)) - material_chunk.add_subchunk(make_percent_subchunk(MATSHIN3, wrap.metallic)) - material_chunk.add_subchunk(make_percent_subchunk(MATTRANS, 1 - wrap.alpha)) - material_chunk.add_subchunk(make_percent_subchunk(MATSELFILPCT, wrap.emission_strength)) - material_chunk.add_subchunk(shading) - - primary_tex = False - - if wrap.base_color_texture: - d_pct = 0.7 + sum(wrap.base_color[:]) * 0.1 - color = [wrap.base_color_texture] - matmap = make_material_texture_chunk(MAT_DIFFUSEMAP, color, d_pct) - if matmap: - material_chunk.add_subchunk(matmap) - primary_tex = True - - if wrap.specular_texture: - spec = [wrap.specular_texture] - s_pct = material.specular_intensity - matmap = make_material_texture_chunk(MAT_SPECMAP, spec, s_pct) - if matmap: - material_chunk.add_subchunk(matmap) - - if wrap.alpha_texture: - alpha = [wrap.alpha_texture] - a_pct = material.diffuse_color[3] - matmap = make_material_texture_chunk(MAT_OPACMAP, alpha, a_pct) - if matmap: - material_chunk.add_subchunk(matmap) - - if wrap.metallic_texture: - metallic = [wrap.metallic_texture] - m_pct = material.metallic - matmap = make_material_texture_chunk(MAT_REFLMAP, metallic, m_pct) - if matmap: - material_chunk.add_subchunk(matmap) - - if wrap.normalmap_texture: - normal = [wrap.normalmap_texture] - b_pct = wrap.normalmap_strength - matmap = make_material_texture_chunk(MAT_BUMPMAP, normal, b_pct) - if matmap: - material_chunk.add_subchunk(matmap) - material_chunk.add_subchunk(make_percent_subchunk(MAT_BUMP_PERCENT, b_pct)) - - if wrap.roughness_texture: - roughness = [wrap.roughness_texture] - r_pct = 1 - material.roughness - matmap = make_material_texture_chunk(MAT_SHINMAP, roughness, r_pct) - if matmap: - material_chunk.add_subchunk(matmap) - - if wrap.emission_color_texture: - e_pct = wrap.emission_strength - emission = [wrap.emission_color_texture] - matmap = make_material_texture_chunk(MAT_SELFIMAP, emission, e_pct) - if matmap: - material_chunk.add_subchunk(matmap) - - # make sure no textures are lost. Everything that doesn't fit - # into a channel is exported as secondary texture - diffuse = [] - - for link in wrap.material.node_tree.links: - if link.from_node.type == 'TEX_IMAGE' and link.to_node.type == 'MIX_RGB': - diffuse = [link.from_node.image] - - if diffuse: - if primary_tex == False: - matmap = make_texture_chunk(MAT_DIFFUSEMAP, diffuse) - else: - matmap = make_texture_chunk(MAT_TEX2MAP, diffuse) - if matmap: - material_chunk.add_subchunk(matmap) - - else: - shading.add_variable("shading", _3ds_ushort(2)) # Gouraud shading - material_chunk.add_subchunk(make_material_subchunk(MATAMBIENT, material.line_color[:3])) - material_chunk.add_subchunk(make_material_subchunk(MATDIFFUSE, material.diffuse_color[:3])) - material_chunk.add_subchunk(make_material_subchunk(MATSPECULAR, material.specular_color[:])) - material_chunk.add_subchunk(make_percent_subchunk(MATSHINESS, 1 - material.roughness)) - material_chunk.add_subchunk(make_percent_subchunk(MATSHIN2, material.specular_intensity)) - material_chunk.add_subchunk(make_percent_subchunk(MATSHIN3, material.metallic)) - material_chunk.add_subchunk(make_percent_subchunk(MATTRANS, 1 - material.diffuse_color[3])) - material_chunk.add_subchunk(shading) - - slots = [get_material_image(material)] # can be None - - if image: - material_chunk.add_subchunk(make_texture_chunk(MAT_DIFFUSEMAP, slots)) - - return material_chunk - - -class tri_wrapper(object): - """Class representing a triangle. - Used when converting faces to triangles""" - - __slots__ = "vertex_index", "ma", "image", "faceuvs", "offset", "flag", "group" - - def __init__(self, vindex=(0, 0, 0), ma=None, image=None, faceuvs=None, flag=0, group=0): - self.vertex_index = vindex - self.ma = ma - self.image = image - self.faceuvs = faceuvs - self.offset = [0, 0, 0] # offset indices - self.flag = flag - self.group = group - - -def extract_triangles(mesh): - """Extract triangles from a mesh.""" - - mesh.calc_loop_triangles() - (polygroup, count) = mesh.calc_smooth_groups(use_bitflags=True) - - tri_list = [] - do_uv = bool(mesh.uv_layers) - - img = None - for i, face in enumerate(mesh.loop_triangles): - f_v = face.vertices - v1, v2, v3 = f_v[0], f_v[1], f_v[2] - uf = mesh.uv_layers.active.data if do_uv else None - - if do_uv: - f_uv = [uf[lp].uv for lp in face.loops] - for ma in mesh.materials: - img = get_uv_image(ma) if uf else None - if img is not None: - img = img.name - uv1, uv2, uv3 = f_uv[0], f_uv[1], f_uv[2] - - """Flag 0x1 sets CA edge visible, Flag 0x2 sets BC edge visible, Flag 0x4 sets AB edge visible - Flag 0x8 indicates a U axis texture wrap and Flag 0x10 indicates a V axis texture wrap - In Blender we use the edge CA, BC, and AB flags for sharp edges flags""" - a_b = mesh.edges[mesh.loops[face.loops[0]].edge_index] - b_c = mesh.edges[mesh.loops[face.loops[1]].edge_index] - c_a = mesh.edges[mesh.loops[face.loops[2]].edge_index] - - if v3 == 0: - a_b, b_c, c_a = c_a, a_b, b_c - - faceflag = 0 - if c_a.use_edge_sharp: - faceflag = faceflag + 0x1 - if b_c.use_edge_sharp: - faceflag = faceflag + 0x2 - if a_b.use_edge_sharp: - faceflag = faceflag + 0x4 - - smoothgroup = polygroup[face.polygon_index] - - if len(f_v)==3: - if v3 == 0: - v1, v2, v3 = v3, v1, v2 - if do_uv: - uv1, uv2, uv3 = uv3, uv1, uv2 - new_tri = tri_wrapper((v1, v2, v3), face.material_index, img) - if (do_uv): - new_tri.faceuvs = uv_key(uv1), uv_key(uv2), uv_key(uv3) - new_tri.flag = faceflag - new_tri.group = smoothgroup if face.use_smooth else 0 - tri_list.append(new_tri) - - return tri_list - - -def remove_face_uv(verts, tri_list): - """Remove face UV coordinates from a list of triangles. - Since 3ds files only support one pair of uv coordinates for each vertex, face uv coordinates - need to be converted to vertex uv coordinates. That means that vertices need to be duplicated when - there are multiple uv coordinates per vertex.""" - - # initialize a list of UniqueLists, one per vertex: - #uv_list = [UniqueList() for i in xrange(len(verts))] - unique_uvs = [{} for i in range(len(verts))] - - # for each face uv coordinate, add it to the UniqueList of the vertex - for tri in tri_list: - for i in range(3): - # store the index into the UniqueList for future reference: - # offset.append(uv_list[tri.vertex_index[i]].add(_3ds_point_uv(tri.faceuvs[i]))) - - context_uv_vert = unique_uvs[tri.vertex_index[i]] - uvkey = tri.faceuvs[i] - - offset_index__uv_3ds = context_uv_vert.get(uvkey) - - if not offset_index__uv_3ds: - offset_index__uv_3ds = context_uv_vert[uvkey] = len(context_uv_vert), _3ds_point_uv(uvkey) - - tri.offset[i] = offset_index__uv_3ds[0] - - # At this point, each vertex has a UniqueList containing every uv coordinate that is associated with it - # only once. - - # Now we need to duplicate every vertex as many times as it has uv coordinates and make sure the - # faces refer to the new face indices: - vert_index = 0 - vert_array = _3ds_array() - uv_array = _3ds_array() - index_list = [] - for i, vert in enumerate(verts): - index_list.append(vert_index) - - pt = _3ds_point_3d(vert.co) # reuse, should be ok - uvmap = [None] * len(unique_uvs[i]) - for ii, uv_3ds in unique_uvs[i].values(): - # add a vertex duplicate to the vertex_array for every uv associated with this vertex: - vert_array.add(pt) - # add the uv coordinate to the uv array: - # This for loop does not give uv's ordered by ii, so we create a new map - # and add the uv's later - # uv_array.add(uv_3ds) - uvmap[ii] = uv_3ds - - # Add the uv's in the correct order - for uv_3ds in uvmap: - # add the uv coordinate to the uv array: - uv_array.add(uv_3ds) - - vert_index += len(unique_uvs[i]) - - # Make sure the triangle vertex indices now refer to the new vertex list: - for tri in tri_list: - for i in range(3): - tri.offset[i] += index_list[tri.vertex_index[i]] - tri.vertex_index = tri.offset - - return vert_array, uv_array, tri_list - - -def make_faces_chunk(tri_list, mesh, materialDict): - """Make a chunk for the faces. - Also adds subchunks assigning materials to all faces.""" - do_smooth = False - use_smooth = [poly.use_smooth for poly in mesh.polygons] - if True in use_smooth: - do_smooth = True - - materials = mesh.materials - if not materials: - ma = None - - face_chunk = _3ds_chunk(OBJECT_FACES) - face_list = _3ds_array() - - if mesh.uv_layers: - # Gather materials used in this mesh - mat/image pairs - unique_mats = {} - for i, tri in enumerate(tri_list): - face_list.add(_3ds_face(tri.vertex_index, tri.flag)) - - if materials: - ma = materials[tri.ma] - if ma: - ma = ma.name - - img = tri.image - - try: - context_face_array = unique_mats[ma, img][1] - except: - name_str = ma if ma else "None" - #if img: - # name_str += img - - context_face_array = _3ds_array() - unique_mats[ma, img] = _3ds_string(sane_name(name_str)), context_face_array - - context_face_array.add(_3ds_ushort(i)) - # obj_material_faces[tri.ma].add(_3ds_ushort(i)) - - face_chunk.add_variable("faces", face_list) - for ma_name, ma_faces in unique_mats.values(): - obj_material_chunk = _3ds_chunk(OBJECT_MATERIAL) - obj_material_chunk.add_variable("name", ma_name) - obj_material_chunk.add_variable("face_list", ma_faces) - face_chunk.add_subchunk(obj_material_chunk) - - else: - obj_material_faces = [] - obj_material_names = [] - for m in materials: - if m: - obj_material_names.append(_3ds_string(sane_name(m.name))) - obj_material_faces.append(_3ds_array()) - n_materials = len(obj_material_names) - - for i, tri in enumerate(tri_list): - face_list.add(_3ds_face(tri.vertex_index, tri.flag)) - if (tri.ma < n_materials): - obj_material_faces[tri.ma].add(_3ds_ushort(i)) - - face_chunk.add_variable("faces", face_list) - for i in range(n_materials): - obj_material_chunk = _3ds_chunk(OBJECT_MATERIAL) - obj_material_chunk.add_variable("name", obj_material_names[i]) - obj_material_chunk.add_variable("face_list", obj_material_faces[i]) - face_chunk.add_subchunk(obj_material_chunk) - - if do_smooth: - obj_smooth_chunk = _3ds_chunk(OBJECT_SMOOTH) - for i, tri in enumerate(tri_list): - obj_smooth_chunk.add_variable("face_" + str(i), _3ds_uint(tri.group)) - face_chunk.add_subchunk(obj_smooth_chunk) - - return face_chunk - - -def make_vert_chunk(vert_array): - """Make a vertex chunk out of an array of vertices.""" - vert_chunk = _3ds_chunk(OBJECT_VERTICES) - vert_chunk.add_variable("vertices", vert_array) - return vert_chunk - - -def make_uv_chunk(uv_array): - """Make a UV chunk out of an array of UVs.""" - uv_chunk = _3ds_chunk(OBJECT_UV) - uv_chunk.add_variable("uv coords", uv_array) - return uv_chunk - - -''' -def make_matrix_4x3_chunk(matrix): - matrix_chunk = _3ds_chunk(OBJECT_TRANS_MATRIX) - for vec in matrix.col: - for f in vec[:3]: - matrix_chunk.add_variable("matrix_f", _3ds_float(f)) - return matrix_chunk -''' - - -def make_mesh_chunk(ob, mesh, matrix, materialDict, translation): - """Make a chunk out of a Blender mesh.""" - - # Extract the triangles from the mesh: - tri_list = extract_triangles(mesh) - - if mesh.uv_layers: - # Remove the face UVs and convert it to vertex UV: - vert_array, uv_array, tri_list = remove_face_uv(mesh.vertices, tri_list) - else: - # Add the vertices to the vertex array: - vert_array = _3ds_array() - for vert in mesh.vertices: - vert_array.add(_3ds_point_3d(vert.co)) - # no UV at all: - uv_array = None - - # create the chunk: - mesh_chunk = _3ds_chunk(OBJECT_MESH) - - # add vertex chunk: - mesh_chunk.add_subchunk(make_vert_chunk(vert_array)) - - # add faces chunk: - mesh_chunk.add_subchunk(make_faces_chunk(tri_list, mesh, materialDict)) - - # if available, add uv chunk: - if uv_array: - mesh_chunk.add_subchunk(make_uv_chunk(uv_array)) - - # mesh_chunk.add_subchunk(make_matrix_4x3_chunk(matrix)) - - # create transformation matrix chunk - matrix_chunk = _3ds_chunk(OBJECT_TRANS_MATRIX) - obj_matrix = matrix.transposed().to_3x3() - - if ob.parent is None: - obj_translate = translation[ob.name] - - else: # Calculate child matrix translation relative to parent - obj_translate = translation[ob.name].cross(-1 * translation[ob.parent.name]) - - matrix_chunk.add_variable("xx", _3ds_float(obj_matrix[0].to_tuple(6)[0])) - matrix_chunk.add_variable("xy", _3ds_float(obj_matrix[0].to_tuple(6)[1])) - matrix_chunk.add_variable("xz", _3ds_float(obj_matrix[0].to_tuple(6)[2])) - matrix_chunk.add_variable("yx", _3ds_float(obj_matrix[1].to_tuple(6)[0])) - matrix_chunk.add_variable("yy", _3ds_float(obj_matrix[1].to_tuple(6)[1])) - matrix_chunk.add_variable("yz", _3ds_float(obj_matrix[1].to_tuple(6)[2])) - matrix_chunk.add_variable("zx", _3ds_float(obj_matrix[2].to_tuple(6)[0])) - matrix_chunk.add_variable("zy", _3ds_float(obj_matrix[2].to_tuple(6)[1])) - matrix_chunk.add_variable("zz", _3ds_float(obj_matrix[2].to_tuple(6)[2])) - matrix_chunk.add_variable("tx", _3ds_float(obj_translate.to_tuple(6)[0])) - matrix_chunk.add_variable("ty", _3ds_float(obj_translate.to_tuple(6)[1])) - matrix_chunk.add_variable("tz", _3ds_float(obj_translate.to_tuple(6)[2])) - - mesh_chunk.add_subchunk(matrix_chunk) - - return mesh_chunk - - -''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX -def make_kfdata(start=0, stop=0, curtime=0): - """Make the basic keyframe data chunk""" - kfdata = _3ds_chunk(KFDATA) - - kfhdr = _3ds_chunk(KFDATA_KFHDR) - kfhdr.add_variable("revision", _3ds_ushort(0)) - # Not really sure what filename is used for, but it seems it is usually used - # to identify the program that generated the .3ds: - kfhdr.add_variable("filename", _3ds_string("Blender")) - kfhdr.add_variable("animlen", _3ds_uint(stop-start)) - - kfseg = _3ds_chunk(KFDATA_KFSEG) - kfseg.add_variable("start", _3ds_uint(start)) - kfseg.add_variable("stop", _3ds_uint(stop)) - - kfcurtime = _3ds_chunk(KFDATA_KFCURTIME) - kfcurtime.add_variable("curtime", _3ds_uint(curtime)) - - kfdata.add_subchunk(kfhdr) - kfdata.add_subchunk(kfseg) - kfdata.add_subchunk(kfcurtime) - return kfdata - -def make_track_chunk(ID, obj): - """Make a chunk for track data. - - Depending on the ID, this will construct a position, rotation or scale track.""" - track_chunk = _3ds_chunk(ID) - track_chunk.add_variable("track_flags", _3ds_ushort()) - track_chunk.add_variable("unknown", _3ds_uint()) - track_chunk.add_variable("unknown", _3ds_uint()) - track_chunk.add_variable("nkeys", _3ds_uint(1)) - # Next section should be repeated for every keyframe, but for now, animation is not actually supported. - track_chunk.add_variable("tcb_frame", _3ds_uint(0)) - track_chunk.add_variable("tcb_flags", _3ds_ushort()) - if obj.type=='Empty': - if ID==POS_TRACK_TAG: - # position vector: - track_chunk.add_variable("position", _3ds_point_3d(obj.getLocation())) - elif ID==ROT_TRACK_TAG: - # rotation (quaternion, angle first, followed by axis): - q = obj.getEuler().to_quaternion() # XXX, todo! - track_chunk.add_variable("rotation", _3ds_point_4d((q.angle, q.axis[0], q.axis[1], q.axis[2]))) - elif ID==SCL_TRACK_TAG: - # scale vector: - track_chunk.add_variable("scale", _3ds_point_3d(obj.getSize())) - else: - # meshes have their transformations applied before - # exporting, so write identity transforms here: - if ID==POS_TRACK_TAG: - # position vector: - track_chunk.add_variable("position", _3ds_point_3d((0.0,0.0,0.0))) - elif ID==ROT_TRACK_TAG: - # rotation (quaternion, angle first, followed by axis): - track_chunk.add_variable("rotation", _3ds_point_4d((0.0, 1.0, 0.0, 0.0))) - elif ID==SCL_TRACK_TAG: - # scale vector: - track_chunk.add_variable("scale", _3ds_point_3d((1.0, 1.0, 1.0))) - - return track_chunk - -def make_kf_obj_node(obj, name_to_id): - """Make a node chunk for a Blender object. - - Takes the Blender object as a parameter. Object id's are taken from the dictionary name_to_id. - Blender Empty objects are converted to dummy nodes.""" - - name = obj.name - # main object node chunk: - kf_obj_node = _3ds_chunk(KFDATA_OBJECT_NODE_TAG) - # chunk for the object id: - obj_id_chunk = _3ds_chunk(OBJECT_NODE_ID) - # object id is from the name_to_id dictionary: - obj_id_chunk.add_variable("node_id", _3ds_ushort(name_to_id[name])) - - # object node header: - obj_node_header_chunk = _3ds_chunk(OBJECT_NODE_HDR) - # object name: - if obj.type == 'Empty': - # Empties are called "$$$DUMMY" and use the OBJECT_INSTANCE_NAME chunk - # for their name (see below): - obj_node_header_chunk.add_variable("name", _3ds_string("$$$DUMMY")) - else: - # Add the name: - obj_node_header_chunk.add_variable("name", _3ds_string(sane_name(name))) - # Add Flag variables (not sure what they do): - obj_node_header_chunk.add_variable("flags1", _3ds_ushort(0)) - obj_node_header_chunk.add_variable("flags2", _3ds_ushort(0)) - - # Check parent-child relationships: - parent = obj.parent - if (parent is None) or (parent.name not in name_to_id): - # If no parent, or the parents name is not in the name_to_id dictionary, - # parent id becomes -1: - obj_node_header_chunk.add_variable("parent", _3ds_ushort(-1)) - else: - # Get the parent's id from the name_to_id dictionary: - obj_node_header_chunk.add_variable("parent", _3ds_ushort(name_to_id[parent.name])) - - # Add pivot chunk: - obj_pivot_chunk = _3ds_chunk(OBJECT_PIVOT) - obj_pivot_chunk.add_variable("pivot", _3ds_point_3d(obj.getLocation())) - kf_obj_node.add_subchunk(obj_pivot_chunk) - - # add subchunks for object id and node header: - kf_obj_node.add_subchunk(obj_id_chunk) - kf_obj_node.add_subchunk(obj_node_header_chunk) - - # Empty objects need to have an extra chunk for the instance name: - if obj.type == 'Empty': - obj_instance_name_chunk = _3ds_chunk(OBJECT_INSTANCE_NAME) - obj_instance_name_chunk.add_variable("name", _3ds_string(sane_name(name))) - kf_obj_node.add_subchunk(obj_instance_name_chunk) - - # Add track chunks for position, rotation and scale: - kf_obj_node.add_subchunk(make_track_chunk(POS_TRACK_TAG, obj)) - kf_obj_node.add_subchunk(make_track_chunk(ROT_TRACK_TAG, obj)) - kf_obj_node.add_subchunk(make_track_chunk(SCL_TRACK_TAG, obj)) - - return kf_obj_node -''' - - -def save(operator, - context, filepath="", - use_selection=True, - global_matrix=None, - ): - - import time - #from bpy_extras.io_utils import create_derived_objects, free_derived_objects - - """Save the Blender scene to a 3ds file.""" - - # Time the export - duration = time.time() - # Blender.Window.WaitCursor(1) - - if global_matrix is None: - global_matrix = mathutils.Matrix() - - if bpy.ops.object.mode_set.poll(): - bpy.ops.object.mode_set(mode='OBJECT') - - scene = context.scene - layer = context.view_layer - depsgraph = context.evaluated_depsgraph_get() - - # Initialize the main chunk (primary): - primary = _3ds_chunk(PRIMARY) - # Add version chunk: - version_chunk = _3ds_chunk(VERSION) - version_chunk.add_variable("version", _3ds_uint(3)) - primary.add_subchunk(version_chunk) - - # Init main object info chunk: - object_info = _3ds_chunk(OBJECTINFO) - mesh_version = _3ds_chunk(MESHVERSION) - mesh_version.add_variable("mesh", _3ds_uint(3)) - object_info.add_subchunk(mesh_version) - - # Add MASTERSCALE element - mscale = _3ds_chunk(MASTERSCALE) - mscale.add_variable("scale", _3ds_float(1)) - object_info.add_subchunk(mscale) - - # Add AMBIENT color - if scene.world is not None: - ambient_chunk = _3ds_chunk(AMBIENTLIGHT) - ambient_light = _3ds_chunk(RGB) - ambient_light.add_variable("ambient", _3ds_float_color(scene.world.color)) - ambient_chunk.add_subchunk(ambient_light) - object_info.add_subchunk(ambient_chunk) - - ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX - # init main key frame data chunk: - kfdata = make_kfdata() - ''' - - # Make a list of all materials used in the selected meshes (use a dictionary, - # each material is added once): - materialDict = {} - mesh_objects = [] - - if use_selection: - objects = [ob for ob in scene.objects if not ob.hide_viewport and ob.select_get(view_layer=layer)] - else: - objects = [ob for ob in scene.objects if not ob.hide_viewport] - - light_objects = [ob for ob in objects if ob.type == 'LIGHT'] - camera_objects = [ob for ob in objects if ob.type == 'CAMERA'] - - for ob in objects: - # get derived objects - #free, derived = create_derived_objects(scene, ob) - derived_dict = bpy_extras.io_utils.create_derived_objects(depsgraph, [ob]) - derived = derived_dict.get(ob) - - if derived is None: - continue - - for ob_derived, mtx in derived: - if ob.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}: - continue - - try: - data = ob_derived.to_mesh() - except: - data = None - - if data: - matrix = global_matrix @ mtx - data.transform(matrix) - mesh_objects.append((ob_derived, data, matrix)) - ma_ls = data.materials - ma_ls_len = len(ma_ls) - - # get material/image tuples. - if data.uv_layers: - if not ma_ls: - ma = ma_name = None - - for f, uf in zip(data.polygons, data.uv_layers.active.data): - if ma_ls: - ma_index = f.material_index - if ma_index >= ma_ls_len: - ma_index = f.material_index = 0 - ma = ma_ls[ma_index] - ma_name = None if ma is None else ma.name - # else there already set to none - - img = get_uv_image(ma) - img_name = None if img is None else img.name - - materialDict.setdefault((ma_name, img_name), (ma, img)) - - else: - for ma in ma_ls: - if ma: # material may be None so check its not. - materialDict.setdefault((ma.name, None), (ma, None)) - - # Why 0 Why! - for f in data.polygons: - if f.material_index >= ma_ls_len: - f.material_index = 0 - - # ob_derived_eval.to_mesh_clear() - - #if free: - # free_derived_objects(ob) - - # Make material chunks for all materials used in the meshes: - for ma_image in materialDict.values(): - object_info.add_subchunk(make_material_chunk(ma_image[0], ma_image[1])) - - # Give all objects a unique ID and build a dictionary from object name to object id: - translation = {} # collect translation for transformation matrix - #name_to_id = {} - for ob, data, matrix in mesh_objects: - translation[ob.name] = ob.location - #name_to_id[ob.name]= len(name_to_id) - """ - #for ob in empty_objects: - # name_to_id[ob.name]= len(name_to_id) - """ - - # Create object chunks for all meshes: - i = 0 - for ob, mesh, matrix in mesh_objects: - # create a new object chunk - object_chunk = _3ds_chunk(OBJECT) - - # set the object name - object_chunk.add_variable("name", _3ds_string(sane_name(ob.name))) - - # make a mesh chunk out of the mesh: - object_chunk.add_subchunk(make_mesh_chunk(ob, mesh, matrix, materialDict, translation)) - - # ensure the mesh has no over sized arrays - # skip ones that do!, otherwise we cant write since the array size wont - # fit into USHORT. - if object_chunk.validate(): - object_info.add_subchunk(object_chunk) - else: - operator.report({'WARNING'}, "Object %r can't be written into a 3DS file") - - ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX - # make a kf object node for the object: - kfdata.add_subchunk(make_kf_obj_node(ob, name_to_id)) - ''' - - # if not blender_mesh.users: - # bpy.data.meshes.remove(blender_mesh) - #blender_mesh.vertices = None - - i += i - - # Create chunks for all empties: - ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX - for ob in empty_objects: - # Empties only require a kf object node: - kfdata.add_subchunk(make_kf_obj_node(ob, name_to_id)) - pass - ''' - - # Create light object chunks - for ob in light_objects: - object_chunk = _3ds_chunk(OBJECT) - light_chunk = _3ds_chunk(OBJECT_LIGHT) - color_float_chunk = _3ds_chunk(RGB) - energy_factor = _3ds_chunk(LIGHT_MULTIPLIER) - object_chunk.add_variable("light", _3ds_string(sane_name(ob.name))) - light_chunk.add_variable("location", _3ds_point_3d(ob.location)) - color_float_chunk.add_variable("color", _3ds_float_color(ob.data.color)) - energy_factor.add_variable("energy", _3ds_float(ob.data.energy * .001)) - light_chunk.add_subchunk(color_float_chunk) - light_chunk.add_subchunk(energy_factor) - - if ob.data.type == 'SPOT': - cone_angle = math.degrees(ob.data.spot_size) - hotspot = cone_angle - (ob.data.spot_blend * math.floor(cone_angle)) - hypo = math.copysign(math.sqrt(pow(ob.location[0], 2) + pow(ob.location[1], 2)), ob.location[1]) - pos_x = ob.location[0] + (ob.location[1] * math.tan(ob.rotation_euler[2])) - pos_y = ob.location[1] + (ob.location[0] * math.tan(math.radians(90) - ob.rotation_euler[2])) - pos_z = hypo * math.tan(math.radians(90) - ob.rotation_euler[0]) - spotlight_chunk = _3ds_chunk(LIGHT_SPOTLIGHT) - spot_roll_chunk = _3ds_chunk(LIGHT_SPOTROLL) - spotlight_chunk.add_variable("target", _3ds_point_3d((pos_x, pos_y, pos_z))) - spotlight_chunk.add_variable("hotspot", _3ds_float(round(hotspot, 4))) - spotlight_chunk.add_variable("angle", _3ds_float(round(cone_angle, 4))) - spot_roll_chunk.add_variable("roll", _3ds_float(round(ob.rotation_euler[1], 6))) - spotlight_chunk.add_subchunk(spot_roll_chunk) - light_chunk.add_subchunk(spotlight_chunk) - - # Add light to object info - object_chunk.add_subchunk(light_chunk) - object_info.add_subchunk(object_chunk) - - # Create camera object chunks - for ob in camera_objects: - object_chunk = _3ds_chunk(OBJECT) - camera_chunk = _3ds_chunk(OBJECT_CAMERA) - diagonal = math.copysign(math.sqrt(pow(ob.location[0], 2) + pow(ob.location[1], 2)), ob.location[1]) - focus_x = ob.location[0] + (ob.location[1] * math.tan(ob.rotation_euler[2])) - focus_y = ob.location[1] + (ob.location[0] * math.tan(math.radians(90) - ob.rotation_euler[2])) - focus_z = diagonal * math.tan(math.radians(90) - ob.rotation_euler[0]) - object_chunk.add_variable("camera", _3ds_string(sane_name(ob.name))) - camera_chunk.add_variable("location", _3ds_point_3d(ob.location)) - camera_chunk.add_variable("target", _3ds_point_3d((focus_x, focus_y, focus_z))) - camera_chunk.add_variable("roll", _3ds_float(round(ob.rotation_euler[1], 6))) - camera_chunk.add_variable("lens", _3ds_float(ob.data.lens)) - object_chunk.add_subchunk(camera_chunk) - object_info.add_subchunk(object_chunk) - - # Add main object info chunk to primary chunk: - primary.add_subchunk(object_info) - - ''' # COMMENTED OUT FOR 2.42 RELEASE!! CRASHES 3DS MAX - # Add main keyframe data chunk to primary chunk: - primary.add_subchunk(kfdata) - ''' - - # At this point, the chunk hierarchy is completely built. - - # Check the size: - primary.get_size() - # Open the file for writing: - file = open(filepath, 'wb') - - # Recursively write the chunks to file: - primary.write(file) - - # Close the file: - file.close() - - # Clear name mapping vars, could make locals too - del name_unique[:] - name_mapping.clear() - - # Debugging only: report the exporting time: - # Blender.Window.WaitCursor(0) - print("3ds export time: %.2f" % (time.time() - duration)) - - # Debugging only: dump the chunk hierarchy: - # primary.dump() - - return {'FINISHED'} diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py deleted file mode 100644 index 833c43a481d581dc9d69c214dc1a97b6b574415f..0000000000000000000000000000000000000000 --- a/io_scene_3ds/import_3ds.py +++ /dev/null @@ -1,1271 +0,0 @@ -# ##### BEGIN GPL LICENSE BLOCK ##### -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -# -# ##### END GPL LICENSE BLOCK ##### - -# Script copyright (C) Bob Holcomb -# Contributors: Bob Holcomb, Richard L?rk?ng, Damien McGinnes, Sebastian Sille -# Campbell Barton, Mario Lapin, Dominique Lorre, Andreas Atteneder - -import os -import time -import struct -import bpy -import math -import mathutils -from bpy_extras.node_shader_utils import PrincipledBSDFWrapper - -BOUNDS_3DS = [] - - -###################################################### -# Data Structures -###################################################### - -# Some of the chunks that we will see -# ----- Primary Chunk, at the beginning of each file -PRIMARY = 0x4D4D - -# ------ Main Chunks -OBJECTINFO = 0x3D3D # This gives the version of the mesh and is found right before the material and object information -VERSION = 0x0002 # This gives the version of the .3ds file -EDITKEYFRAME = 0xB000 # This is the header for all of the key frame info - -# ------ Data Chunks, used for various attributes -PERCENTAGE_SHORT = 0x30 -PERCENTAGE_FLOAT = 0x31 - -# ------ sub defines of OBJECTINFO -MATERIAL = 0xAFFF # This stored the texture info -OBJECT = 0x4000 # This stores the faces, vertices, etc... - -# >------ sub defines of MATERIAL -# ------ sub defines of MATERIAL_BLOCK -MAT_NAME = 0xA000 # This holds the material name -MAT_AMBIENT = 0xA010 # Ambient color of the object/material -MAT_DIFFUSE = 0xA020 # This holds the color of the object/material -MAT_SPECULAR = 0xA030 # Specular color of the object/material -MAT_SHINESS = 0xA040 # Roughness of the object/material (percent) -MAT_SHIN2 = 0xA041 # Shininess of the object/material (percent) -MAT_SHIN3 = 0xA042 # Reflection of the object/material (percent) -MAT_TRANSPARENCY = 0xA050 # Transparency value of material (percent) -MAT_SELF_ILLUM = 0xA080 # Self Illumination value of material -MAT_SELF_ILPCT = 0xA084 # Self illumination strength (percent) -MAT_WIRE = 0xA085 # Only render's wireframe -MAT_SHADING = 0xA100 # Material shading method - -MAT_TEXTURE_MAP = 0xA200 # This is a header for a new texture map -MAT_SPECULAR_MAP = 0xA204 # This is a header for a new specular map -MAT_OPACITY_MAP = 0xA210 # This is a header for a new opacity map -MAT_REFLECTION_MAP = 0xA220 # This is a header for a new reflection map -MAT_BUMP_MAP = 0xA230 # This is a header for a new bump map -MAT_BUMP_PERCENT = 0xA252 # Normalmap strength (percent) -MAT_TEX2_MAP = 0xA33A # This is a header for a secondary texture -MAT_SHIN_MAP = 0xA33C # This is a header for a new roughness map -MAT_SELFI_MAP = 0xA33D # This is a header for a new emission map -MAT_MAP_FILEPATH = 0xA300 # This holds the file name of the texture - -MAT_MAP_TILING = 0xa351 # 2nd bit (from LSB) is mirror UV flag -MAT_MAP_USCALE = 0xA354 # U axis scaling -MAT_MAP_VSCALE = 0xA356 # V axis scaling -MAT_MAP_UOFFSET = 0xA358 # U axis offset -MAT_MAP_VOFFSET = 0xA35A # V axis offset -MAT_MAP_ANG = 0xA35C # UV rotation around the z-axis in rad -MAT_MAP_COL1 = 0xA360 # Map Color1 -MAT_MAP_COL2 = 0xA362 # Map Color2 -MAT_MAP_RCOL = 0xA364 # Red mapping -MAT_MAP_GCOL = 0xA366 # Green mapping -MAT_MAP_BCOL = 0xA368 # Blue mapping -MAT_FLOAT_COLOR = 0x0010 # color defined as 3 floats -MAT_24BIT_COLOR = 0x0011 # color defined as 3 bytes - -# >------ sub defines of OBJECT -OBJECT_MESH = 0x4100 # This lets us know that we are reading a new object -OBJECT_LIGHT = 0x4600 # This lets un know we are reading a light object -OBJECT_LIGHT_SPOT = 0x4610 # The light is a spotloght. -OBJECT_LIGHT_OFF = 0x4620 # The light off. -OBJECT_LIGHT_ATTENUATE = 0x4625 -OBJECT_LIGHT_RAYSHADE = 0x4627 -OBJECT_LIGHT_SHADOWED = 0x4630 -OBJECT_LIGHT_LOCAL_SHADOW = 0x4640 -OBJECT_LIGHT_LOCAL_SHADOW2 = 0x4641 -OBJECT_LIGHT_SEE_CONE = 0x4650 -OBJECT_LIGHT_SPOT_RECTANGULAR = 0x4651 -OBJECT_LIGHT_SPOT_OVERSHOOT = 0x4652 -OBJECT_LIGHT_SPOT_PROJECTOR = 0x4653 -OBJECT_LIGHT_EXCLUDE = 0x4654 -OBJECT_LIGHT_RANGE = 0x4655 -OBJECT_LIGHT_ROLL = 0x4656 -OBJECT_LIGHT_SPOT_ASPECT = 0x4657 -OBJECT_LIGHT_RAY_BIAS = 0x4658 -OBJECT_LIGHT_INNER_RANGE = 0x4659 -OBJECT_LIGHT_OUTER_RANGE = 0x465A -OBJECT_LIGHT_MULTIPLIER = 0x465B -OBJECT_LIGHT_AMBIENT_LIGHT = 0x4680 - -OBJECT_CAMERA = 0x4700 # This lets un know we are reading a camera object - -# >------ sub defines of CAMERA -OBJECT_CAM_RANGES = 0x4720 # The camera range values - -# >------ sub defines of OBJECT_MESH -OBJECT_VERTICES = 0x4110 # The objects vertices -OBJECT_VERTFLAGS = 0x4111 # The objects vertex flags -OBJECT_FACES = 0x4120 # The objects faces -OBJECT_MATERIAL = 0x4130 # This is found if the object has a material, either texture map or color -OBJECT_UV = 0x4140 # The UV texture coordinates -OBJECT_SMOOTH = 0x4150 # The Object smooth groups -OBJECT_TRANS_MATRIX = 0x4160 # The Object Matrix - -# >------ sub defines of EDITKEYFRAME -KFDATA_AMBIENT = 0xB001 -KFDATA_OBJECT = 0xB002 -KFDATA_CAMERA = 0xB003 -KFDATA_TARGET = 0xB004 -KFDATA_LIGHT = 0xB005 -KFDATA_L_TARGET = 0xB006 -KFDATA_SPOTLIGHT = 0xB007 -KFDATA_KFSEG = 0xB008 -# KFDATA_CURTIME = 0xB009 -# KFDATA_KFHDR = 0xB00A -# >------ sub defines of KEYFRAME_NODE -OBJECT_NODE_HDR = 0xB010 -OBJECT_INSTANCE_NAME = 0xB011 -# OBJECT_PRESCALE = 0xB012 -OBJECT_PIVOT = 0xB013 -# OBJECT_BOUNDBOX = 0xB014 -# MORPH_SMOOTH = 0xB015 -POS_TRACK_TAG = 0xB020 -ROT_TRACK_TAG = 0xB021 -SCL_TRACK_TAG = 0xB022 -FOV_TRACK_TAG = 0xB023 -ROLL_TRACK_TAG = 0xB024 -COL_TRACK_TAG = 0xB025 -# MORPH_TRACK_TAG = 0xB026 -# HOTSPOT_TRACK_TAG = 0xB027 -# FALLOFF_TRACK_TAG = 0xB028 -# HIDE_TRACK_TAG = 0xB029 -# OBJECT_NODE_ID = 0xB030 - -ROOT_OBJECT = 0xFFFF - -global scn -scn = None - -object_dictionary = {} -object_matrix = {} - - -class Chunk: - __slots__ = ( - "ID", - "length", - "bytes_read", - ) - # we don't read in the bytes_read, we compute that - binary_format = "<HI" - - def __init__(self): - self.ID = 0 - self.length = 0 - self.bytes_read = 0 - - def dump(self): - print('ID: ', self.ID) - print('ID in hex: ', hex(self.ID)) - print('length: ', self.length) - print('bytes_read: ', self.bytes_read) - - -def read_chunk(file, chunk): - temp_data = file.read(struct.calcsize(chunk.binary_format)) - data = struct.unpack(chunk.binary_format, temp_data) - chunk.ID = data[0] - chunk.length = data[1] - # update the bytes read function - chunk.bytes_read = 6 - - # if debugging - # chunk.dump() - - -def read_string(file): - # read in the characters till we get a null character - s = [] - while True: - c = file.read(1) - if c == b'\x00': - break - s.append(c) - # print('string: ', s) - - # Remove the null character from the string - # print("read string", s) - return str(b''.join(s), "utf-8", "replace"), len(s) + 1 - -###################################################### -# IMPORT -###################################################### - - -def process_next_object_chunk(file, previous_chunk): - new_chunk = Chunk() - - while (previous_chunk.bytes_read < previous_chunk.length): - # read the next chunk - read_chunk(file, new_chunk) - - -def skip_to_end(file, skip_chunk): - buffer_size = skip_chunk.length - skip_chunk.bytes_read - binary_format = "%ic" % buffer_size - file.read(struct.calcsize(binary_format)) - skip_chunk.bytes_read += buffer_size - - -def add_texture_to_material(image, contextWrapper, pct, extend, alpha, scale, offset, angle, tintcolor, mapto): - shader = contextWrapper.node_principled_bsdf - nodetree = contextWrapper.material.node_tree - shader.location = (-300, 0) - nodes = nodetree.nodes - links = nodetree.links - - if mapto == 'COLOR': - mixer = nodes.new(type='ShaderNodeMixRGB') - mixer.label = "Mixer" - mixer.inputs[0].default_value = pct / 100 - mixer.inputs[1].default_value = tintcolor[:3] + [1] if tintcolor else shader.inputs['Base Color'].default_value[:] - contextWrapper._grid_to_location(1, 2, dst_node=mixer, ref_node=shader) - img_wrap = contextWrapper.base_color_texture - links.new(img_wrap.node_image.outputs['Color'], mixer.inputs[2]) - links.new(mixer.outputs['Color'], shader.inputs['Base Color']) - elif mapto == 'SPECULARITY': - img_wrap = contextWrapper.specular_texture - elif mapto == 'ALPHA': - shader.location = (0, -300) - img_wrap = contextWrapper.alpha_texture - elif mapto == 'METALLIC': - shader.location = (300, 300) - img_wrap = contextWrapper.metallic_texture - elif mapto == 'ROUGHNESS': - shader.location = (300, 0) - img_wrap = contextWrapper.roughness_texture - elif mapto == 'EMISSION': - shader.location = (-300, -600) - img_wrap = contextWrapper.emission_color_texture - elif mapto == 'NORMAL': - shader.location = (300, 300) - img_wrap = contextWrapper.normalmap_texture - elif mapto == 'TEXTURE': - img_wrap = nodes.new(type='ShaderNodeTexImage') - img_wrap.label = image.name - contextWrapper._grid_to_location(0, 2, dst_node=img_wrap, ref_node=shader) - for node in nodes: - if node.label == 'Mixer': - spare = node.inputs[1] if node.inputs[1].is_linked is False else node.inputs[2] - socket = spare if spare.is_linked is False else node.inputs[0] - links.new(img_wrap.outputs['Color'], socket) - if node.type == 'TEX_COORD': - links.new(node.outputs['UV'], img_wrap.inputs['Vector']) - if shader.inputs['Base Color'].is_linked is False: - links.new(img_wrap.outputs['Color'], shader.inputs['Base Color']) - - img_wrap.image = image - img_wrap.extension = 'REPEAT' - - if mapto != 'TEXTURE': - img_wrap.scale = scale - img_wrap.translation = offset - img_wrap.rotation[2] = angle - - if extend == 'mirror': - # 3DS mirror flag can be emulated by these settings (at least so it seems) - # TODO: bring back mirror - pass - # texture.repeat_x = texture.repeat_y = 2 - # texture.use_mirror_x = texture.use_mirror_y = True - elif extend == 'decal': - # 3DS' decal mode maps best to Blenders EXTEND - img_wrap.extension = 'EXTEND' - elif extend == 'noWrap': - img_wrap.extension = 'CLIP' - if alpha == 'alpha': - for link in links: - if link.from_node.type == 'TEX_IMAGE' and link.to_node.type == 'MIX_RGB': - tex = link.from_node.image.name - own_node = img_wrap.node_image - own_map = img_wrap.node_mapping - if tex == image.name: - links.new(link.from_node.outputs['Alpha'], img_wrap.socket_dst) - nodes.remove(own_map) - nodes.remove(own_node) - for imgs in bpy.data.images: - if imgs.name[-3:].isdigit(): - if not imgs.users: - bpy.data.images.remove(imgs) - else: - links.new(img_wrap.node_image.outputs['Alpha'], img_wrap.socket_dst) - - shader.location = (300, 300) - contextWrapper._grid_to_location(1, 0, dst_node=contextWrapper.node_out, ref_node=shader) - - -def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SEARCH, KEYFRAME): - from bpy_extras.image_utils import load_image - - contextObName = None - contextLamp = None - contextCamera = None - contextMaterial = None - contextWrapper = None - contextMatrix = None - contextMesh_vertls = None - contextMesh_facels = None - contextMesh_flag = None - contextMeshMaterials = [] - contextMesh_smooth = None - contextMeshUV = None - - #TEXTURE_DICT = {} - MATDICT = {} - - # Localspace variable names, faster. - SZ_FLOAT = struct.calcsize('f') - SZ_2FLOAT = struct.calcsize('2f') - SZ_3FLOAT = struct.calcsize('3f') - SZ_4FLOAT = struct.calcsize('4f') - SZ_U_SHORT = struct.calcsize('H') - SZ_4U_SHORT = struct.calcsize('4H') - SZ_4x3MAT = struct.calcsize('ffffffffffff') - - object_list = [] # for hierarchy - object_parent = [] # index of parent in hierarchy, 0xFFFF = no parent - pivot_list = [] # pivots with hierarchy handling - - def putContextMesh( - context, - myContextMesh_vertls, - myContextMesh_facels, - myContextMesh_flag, - myContextMeshMaterials, - myContextMesh_smooth, - ): - bmesh = bpy.data.meshes.new(contextObName) - - if myContextMesh_facels is None: - myContextMesh_facels = [] - - if myContextMesh_vertls: - - bmesh.vertices.add(len(myContextMesh_vertls) // 3) - bmesh.vertices.foreach_set("co", myContextMesh_vertls) - - nbr_faces = len(myContextMesh_facels) - bmesh.polygons.add(nbr_faces) - bmesh.loops.add(nbr_faces * 3) - eekadoodle_faces = [] - for v1, v2, v3 in myContextMesh_facels: - eekadoodle_faces.extend((v3, v1, v2) if v3 == 0 else (v1, v2, v3)) - bmesh.polygons.foreach_set("loop_start", range(0, nbr_faces * 3, 3)) - bmesh.polygons.foreach_set("loop_total", (3,) * nbr_faces) - bmesh.loops.foreach_set("vertex_index", eekadoodle_faces) - - if bmesh.polygons and contextMeshUV: - bmesh.uv_layers.new() - uv_faces = bmesh.uv_layers.active.data[:] - else: - uv_faces = None - - for mat_idx, (matName, faces) in enumerate(myContextMeshMaterials): - if matName is None: - bmat = None - else: - bmat = MATDICT.get(matName) - # in rare cases no materials defined. - - bmesh.materials.append(bmat) # can be None - for fidx in faces: - bmesh.polygons[fidx].material_index = mat_idx - - if uv_faces: - uvl = bmesh.uv_layers.active.data[:] - for fidx, pl in enumerate(bmesh.polygons): - face = myContextMesh_facels[fidx] - v1, v2, v3 = face - - # eekadoodle - if v3 == 0: - v1, v2, v3 = v3, v1, v2 - - uvl[pl.loop_start].uv = contextMeshUV[v1 * 2: (v1 * 2) + 2] - uvl[pl.loop_start + 1].uv = contextMeshUV[v2 * 2: (v2 * 2) + 2] - uvl[pl.loop_start + 2].uv = contextMeshUV[v3 * 2: (v3 * 2) + 2] - # always a tri - - bmesh.validate() - bmesh.update() - - ob = bpy.data.objects.new(contextObName, bmesh) - object_dictionary[contextObName] = ob - context.view_layer.active_layer_collection.collection.objects.link(ob) - imported_objects.append(ob) - - if myContextMesh_flag: - # Bit 0 (0x1) sets edge CA visible, Bit 1 (0x2) sets edge BC visible and Bit 2 (0x4) sets edge AB visible - # In Blender we use sharp edges for those flags - for f, pl in enumerate(bmesh.polygons): - face = myContextMesh_facels[f] - faceflag = myContextMesh_flag[f] - edge_ab = bmesh.edges[bmesh.loops[pl.loop_start].edge_index] - edge_bc = bmesh.edges[bmesh.loops[pl.loop_start + 1].edge_index] - edge_ca = bmesh.edges[bmesh.loops[pl.loop_start + 2].edge_index] - if face[2] == 0: - edge_ab, edge_bc, edge_ca = edge_ca, edge_ab, edge_bc - if faceflag == 1: - edge_ca.use_edge_sharp = True - elif faceflag == 2: - edge_bc.use_edge_sharp = True - elif faceflag == 3: - edge_ca.use_edge_sharp = True - edge_bc.use_edge_sharp = True - elif faceflag == 4: - edge_ab.use_edge_sharp = True - elif faceflag == 5: - edge_ca.use_edge_sharp = True - edge_ab.use_edge_sharp = True - elif faceflag == 6: - edge_bc.use_edge_sharp = True - edge_ab.use_edge_sharp = True - elif faceflag == 7: - edge_bc.use_edge_sharp = True - edge_ab.use_edge_sharp = True - edge_ca.use_edge_sharp = True - - if myContextMesh_smooth: - for f, pl in enumerate(bmesh.polygons): - smoothface = myContextMesh_smooth[f] - if smoothface > 0: - bmesh.polygons[f].use_smooth = True - - if contextMatrix: - ob.matrix_local = contextMatrix - object_matrix[ob] = contextMatrix.copy() - - # a spare chunk - new_chunk = Chunk() - temp_chunk = Chunk() - - CreateBlenderObject = False - CreateLightObject = False - CreateCameraObject = False - - def read_float_color(temp_chunk): - temp_data = file.read(SZ_3FLOAT) - temp_chunk.bytes_read += SZ_3FLOAT - return [float(col) for col in struct.unpack('<3f', temp_data)] - - def read_float(temp_chunk): - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - return struct.unpack('<f', temp_data)[0] - - def read_short(temp_chunk): - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - return struct.unpack('<H', temp_data)[0] - - def read_byte_color(temp_chunk): - temp_data = file.read(struct.calcsize('3B')) - temp_chunk.bytes_read += 3 - return [float(col) / 255 for col in struct.unpack('<3B', temp_data)] - - def read_texture(new_chunk, temp_chunk, name, mapto): - uscale, vscale, uoffset, voffset, angle = 1.0, 1.0, 0.0, 0.0, 0.0 - contextWrapper.use_nodes = True - tintcolor = None - extend = 'wrap' - alpha = False - pct = 50 - - contextWrapper.emission_color = contextMaterial.line_color[:3] - contextWrapper.emission_strength = contextMaterial.line_priority / 100 - contextWrapper.base_color = contextMaterial.diffuse_color[:3] - contextWrapper.specular = contextMaterial.specular_intensity - contextWrapper.roughness = contextMaterial.roughness - contextWrapper.metallic = contextMaterial.metallic - contextWrapper.alpha = contextMaterial.diffuse_color[3] - - while (new_chunk.bytes_read < new_chunk.length): - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - pct = read_short(temp_chunk) - - elif temp_chunk.ID == MAT_MAP_FILEPATH: - texture_name, read_str_len = read_string(file) - img = load_image(texture_name, dirname, place_holder=False, recursive=IMAGE_SEARCH, check_existing=True) - temp_chunk.bytes_read += read_str_len # plus one for the null character that gets removed - - elif temp_chunk.ID == MAT_MAP_USCALE: - uscale = read_float(temp_chunk) - elif temp_chunk.ID == MAT_MAP_VSCALE: - vscale = read_float(temp_chunk) - elif temp_chunk.ID == MAT_MAP_UOFFSET: - uoffset = read_float(temp_chunk) - elif temp_chunk.ID == MAT_MAP_VOFFSET: - voffset = read_float(temp_chunk) - - elif temp_chunk.ID == MAT_MAP_TILING: - tiling = read_short(temp_chunk) - if tiling & 0x1: - extend = 'decal' - elif tiling & 0x2: - extend = 'mirror' - elif tiling & 0x8: - extend = 'invert' - elif tiling & 0x10: - extend = 'noWrap' - elif tiling & 0x20: - alpha = 'sat' - elif tiling & 0x40: - alpha = 'alpha' - elif tiling & 0x80: - tint = 'tint' - elif tiling & 0x100: - tint = 'noAlpha' - elif tiling & 0x200: - tint = 'RGBtint' - - elif temp_chunk.ID == MAT_MAP_ANG: - angle = read_float(temp_chunk) - print("\nwarning: UV angle mapped to z-rotation") - - elif temp_chunk.ID == MAT_MAP_COL1: - tintcolor = read_byte_color(temp_chunk) - - skip_to_end(file, temp_chunk) - new_chunk.bytes_read += temp_chunk.bytes_read - - # add the map to the material in the right channel - if img: - add_texture_to_material(img, contextWrapper, pct, extend, alpha, (uscale, vscale, 1), - (uoffset, voffset, 0), angle, tintcolor, mapto) - - dirname = os.path.dirname(file.name) - - # loop through all the data for this chunk (previous chunk) and see what it is - while (previous_chunk.bytes_read < previous_chunk.length): - read_chunk(file, new_chunk) - - # is it a Version chunk? - if new_chunk.ID == VERSION: - # read in the version of the file - temp_data = file.read(struct.calcsize('I')) - version = struct.unpack('<I', temp_data)[0] - new_chunk.bytes_read += 4 # read the 4 bytes for the version number - # this loader works with version 3 and below, but may not with 4 and above - if version > 3: - print('\tNon-Fatal Error: Version greater than 3, may not load correctly: ', version) - - # is it an object info chunk? - elif new_chunk.ID == OBJECTINFO: - process_next_chunk(context, file, new_chunk, imported_objects, IMAGE_SEARCH, KEYFRAME) - - # keep track of how much we read in the main chunk - new_chunk.bytes_read += temp_chunk.bytes_read - - # is it an object chunk? - elif new_chunk.ID == OBJECT: - - if CreateBlenderObject: - putContextMesh( - context, - contextMesh_vertls, - contextMesh_facels, - contextMesh_flag, - contextMeshMaterials, - contextMesh_smooth, - ) - contextMesh_vertls = [] - contextMesh_facels = [] - contextMeshMaterials = [] - contextMesh_flag = None - contextMesh_smooth = None - contextMeshUV = None - # Reset matrix - contextMatrix = None - - CreateBlenderObject = True - contextObName, read_str_len = read_string(file) - new_chunk.bytes_read += read_str_len - - # is it a material chunk? - elif new_chunk.ID == MATERIAL: - contextMaterial = bpy.data.materials.new('Material') - contextWrapper = PrincipledBSDFWrapper(contextMaterial, is_readonly=False, use_nodes=False) - - elif new_chunk.ID == MAT_NAME: - material_name, read_str_len = read_string(file) - - # plus one for the null character that ended the string - new_chunk.bytes_read += read_str_len - contextMaterial.name = material_name.rstrip() # remove trailing whitespace - MATDICT[material_name] = contextMaterial - - elif new_chunk.ID == MAT_AMBIENT: - read_chunk(file, temp_chunk) - # only available color is emission color - if temp_chunk.ID == MAT_FLOAT_COLOR: - contextMaterial.line_color[:3] = read_float_color(temp_chunk) - elif temp_chunk.ID == MAT_24BIT_COLOR: - contextMaterial.line_color[:3] = read_byte_color(temp_chunk) - else: - skip_to_end(file, temp_chunk) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_DIFFUSE: - read_chunk(file, temp_chunk) - if temp_chunk.ID == MAT_FLOAT_COLOR: - contextMaterial.diffuse_color[:3] = read_float_color(temp_chunk) - elif temp_chunk.ID == MAT_24BIT_COLOR: - contextMaterial.diffuse_color[:3] = read_byte_color(temp_chunk) - else: - skip_to_end(file, temp_chunk) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SPECULAR: - read_chunk(file, temp_chunk) - # Specular color is available - if temp_chunk.ID == MAT_FLOAT_COLOR: - contextMaterial.specular_color = read_float_color(temp_chunk) - elif temp_chunk.ID == MAT_24BIT_COLOR: - contextMaterial.specular_color = read_byte_color(temp_chunk) - else: - skip_to_end(file, temp_chunk) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SHINESS: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextMaterial.roughness = 1 - (float(struct.unpack('<H', temp_data)[0]) / 100) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextMaterial.roughness = 1 - float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SHIN2: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextMaterial.specular_intensity = (float(struct.unpack('<H', temp_data)[0]) / 100) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextMaterial.specular_intensity = float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SHIN3: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextMaterial.metallic = (float(struct.unpack('<H', temp_data)[0]) / 100) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextMaterial.metallic = float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_TRANSPARENCY: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextMaterial.diffuse_color[3] = 1 - (float(struct.unpack('<H', temp_data)[0]) / 100) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextMaterial.diffuse_color[3] = 1 - float(struct.unpack('f', temp_data)[0]) - else: - print("Cannot read material transparency") - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SELF_ILPCT: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextMaterial.line_priority = int(struct.unpack('H', temp_data)[0]) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextMaterial.line_priority = (float(struct.unpack('f', temp_data)[0]) * 100) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SHADING: - shading = read_short(new_chunk) - if shading >= 2: - contextWrapper.use_nodes = True - contextWrapper.emission_color = contextMaterial.line_color[:3] - contextWrapper.emission_strength = contextMaterial.line_priority / 100 - contextWrapper.base_color = contextMaterial.diffuse_color[:3] - contextWrapper.specular = contextMaterial.specular_intensity - contextWrapper.roughness = contextMaterial.roughness - contextWrapper.metallic = contextMaterial.metallic - contextWrapper.alpha = contextMaterial.diffuse_color[3] - contextWrapper.use_nodes = False - if shading >= 3: - contextWrapper.use_nodes = True - - elif new_chunk.ID == MAT_TEXTURE_MAP: - read_texture(new_chunk, temp_chunk, "Diffuse", "COLOR") - - elif new_chunk.ID == MAT_SPECULAR_MAP: - read_texture(new_chunk, temp_chunk, "Specular", "SPECULARITY") - - elif new_chunk.ID == MAT_OPACITY_MAP: - contextMaterial.blend_method = 'BLEND' - read_texture(new_chunk, temp_chunk, "Opacity", "ALPHA") - - elif new_chunk.ID == MAT_REFLECTION_MAP: - read_texture(new_chunk, temp_chunk, "Reflect", "METALLIC") - - elif new_chunk.ID == MAT_BUMP_MAP: - read_texture(new_chunk, temp_chunk, "Bump", "NORMAL") - - elif new_chunk.ID == MAT_BUMP_PERCENT: - read_chunk(file, temp_chunk) - if temp_chunk.ID == PERCENTAGE_SHORT: - temp_data = file.read(SZ_U_SHORT) - temp_chunk.bytes_read += SZ_U_SHORT - contextWrapper.normalmap_strength = (float(struct.unpack('<H', temp_data)[0]) / 100) - elif temp_chunk.ID == PERCENTAGE_FLOAT: - temp_data = file.read(SZ_FLOAT) - temp_chunk.bytes_read += SZ_FLOAT - contextWrapper.normalmap_strength = float(struct.unpack('f', temp_data)[0]) - else: - skip_to_end(file, temp_chunk) - new_chunk.bytes_read += temp_chunk.bytes_read - - elif new_chunk.ID == MAT_SHIN_MAP: - read_texture(new_chunk, temp_chunk, "Shininess", "ROUGHNESS") - - elif new_chunk.ID == MAT_SELFI_MAP: - read_texture(new_chunk, temp_chunk, "Emit", "EMISSION") - - elif new_chunk.ID == MAT_TEX2_MAP: - read_texture(new_chunk, temp_chunk, "Tex", "TEXTURE") - - # mesh chunk - elif new_chunk.ID == OBJECT_MESH: - pass - - elif new_chunk.ID == OBJECT_VERTICES: - """Worldspace vertex locations""" - - temp_data = file.read(SZ_U_SHORT) - num_verts = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += 2 - contextMesh_vertls = struct.unpack('<%df' % (num_verts * 3), file.read(SZ_3FLOAT * num_verts)) - new_chunk.bytes_read += SZ_3FLOAT * num_verts - # dummyvert is not used atm! - - elif new_chunk.ID == OBJECT_FACES: - temp_data = file.read(SZ_U_SHORT) - num_faces = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += 2 - temp_data = file.read(SZ_4U_SHORT * num_faces) - new_chunk.bytes_read += SZ_4U_SHORT * num_faces # 4 short ints x 2 bytes each - contextMesh_facels = struct.unpack('<%dH' % (num_faces * 4), temp_data) - contextMesh_flag = [contextMesh_facels[i] for i in range(3, (num_faces * 4) + 3, 4)] - contextMesh_facels = [contextMesh_facels[i - 3:i] for i in range(3, (num_faces * 4) + 3, 4)] - - elif new_chunk.ID == OBJECT_MATERIAL: - material_name, read_str_len = read_string(file) - new_chunk.bytes_read += read_str_len # remove 1 null character. - temp_data = file.read(SZ_U_SHORT) - num_faces_using_mat = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * num_faces_using_mat) - new_chunk.bytes_read += SZ_U_SHORT * num_faces_using_mat - temp_data = struct.unpack("<%dH" % (num_faces_using_mat), temp_data) - contextMeshMaterials.append((material_name, temp_data)) - # look up the material in all the materials - - elif new_chunk.ID == OBJECT_SMOOTH: - temp_data = file.read(struct.calcsize('I') * num_faces) - smoothgroup = struct.unpack('<%dI' % (num_faces), temp_data) - new_chunk.bytes_read += struct.calcsize('I') * num_faces - contextMesh_smooth = smoothgroup - - elif new_chunk.ID == OBJECT_UV: - temp_data = file.read(SZ_U_SHORT) - num_uv = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += 2 - temp_data = file.read(SZ_2FLOAT * num_uv) - new_chunk.bytes_read += SZ_2FLOAT * num_uv - contextMeshUV = struct.unpack('<%df' % (num_uv * 2), temp_data) - - elif new_chunk.ID == OBJECT_TRANS_MATRIX: - # How do we know the matrix size? 54 == 4x4 48 == 4x3 - temp_data = file.read(SZ_4x3MAT) - data = list(struct.unpack('<ffffffffffff', temp_data)) - new_chunk.bytes_read += SZ_4x3MAT - contextMatrix = mathutils.Matrix( - (data[:3] + [0], data[3:6] + [0], data[6:9] + [0], data[9:] + [1])).transposed() - - elif contextObName and new_chunk.ID == OBJECT_LIGHT: # Basic lamp support. - # no lamp in dict that would be confusing - # ...why not? just set CreateBlenderObject to False - newLamp = bpy.data.lights.new("Lamp", 'POINT') - contextLamp = bpy.data.objects.new(contextObName, newLamp) - context.view_layer.active_layer_collection.collection.objects.link(contextLamp) - imported_objects.append(contextLamp) - object_dictionary[contextObName] = contextLamp - temp_data = file.read(SZ_3FLOAT) - contextLamp.location = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - contextMatrix = None # Reset matrix - CreateBlenderObject = False - CreateLightObject = True - - elif CreateLightObject and new_chunk.ID == MAT_FLOAT_COLOR: # color - temp_data = file.read(SZ_3FLOAT) - contextLamp.data.color = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - elif CreateLightObject and new_chunk.ID == OBJECT_LIGHT_MULTIPLIER: # intensity - temp_data = file.read(SZ_FLOAT) - contextLamp.data.energy = (float(struct.unpack('f', temp_data)[0]) * 1000) - new_chunk.bytes_read += SZ_FLOAT - - elif CreateLightObject and new_chunk.ID == OBJECT_LIGHT_SPOT: # spotlight - temp_data = file.read(SZ_3FLOAT) - contextLamp.data.type = 'SPOT' - spot = mathutils.Vector(struct.unpack('<3f', temp_data)) - aim = contextLamp.location + spot - hypo = math.copysign(math.sqrt(pow(aim[1], 2) + pow(aim[0], 2)), aim[1]) - track = math.copysign(math.sqrt(pow(hypo, 2) + pow(spot[2], 2)), aim[1]) - angle = math.radians(90) - math.copysign(math.acos(hypo / track), aim[2]) - contextLamp.rotation_euler[0] = -1 * math.copysign(angle, aim[1]) - contextLamp.rotation_euler[2] = -1 * (math.radians(90) - math.acos(aim[0] / hypo)) - new_chunk.bytes_read += SZ_3FLOAT - temp_data = file.read(SZ_FLOAT) # hotspot - hotspot = float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += SZ_FLOAT - temp_data = file.read(SZ_FLOAT) # angle - beam_angle = float(struct.unpack('f', temp_data)[0]) - contextLamp.data.spot_size = math.radians(beam_angle) - contextLamp.data.spot_blend = (1.0 - (hotspot / beam_angle)) * 2 - new_chunk.bytes_read += SZ_FLOAT - elif CreateLightObject and new_chunk.ID == OBJECT_LIGHT_ROLL: # roll - temp_data = file.read(SZ_FLOAT) - contextLamp.rotation_euler[1] = float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += SZ_FLOAT - - elif contextObName and new_chunk.ID == OBJECT_CAMERA and CreateCameraObject is False: # Basic camera support - camera = bpy.data.cameras.new("Camera") - contextCamera = bpy.data.objects.new(contextObName, camera) - context.view_layer.active_layer_collection.collection.objects.link(contextCamera) - imported_objects.append(contextCamera) - object_dictionary[contextObName] = contextCamera - temp_data = file.read(SZ_3FLOAT) - contextCamera.location = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - temp_data = file.read(SZ_3FLOAT) - target = mathutils.Vector(struct.unpack('<3f', temp_data)) - cam = contextCamera.location + target - focus = math.copysign(math.sqrt(pow(cam[1], 2) + pow(cam[0], 2)), cam[1]) - new_chunk.bytes_read += SZ_3FLOAT - temp_data = file.read(SZ_FLOAT) # triangulating camera angles - direction = math.copysign(math.sqrt(pow(focus, 2) + pow(target[2], 2)), cam[1]) - pitch = math.radians(90) - math.copysign(math.acos(focus / direction), cam[2]) - contextCamera.rotation_euler[0] = -1 * math.copysign(pitch, cam[1]) - contextCamera.rotation_euler[1] = float(struct.unpack('f', temp_data)[0]) - contextCamera.rotation_euler[2] = -1 * (math.radians(90) - math.acos(cam[0] / focus)) - new_chunk.bytes_read += SZ_FLOAT - temp_data = file.read(SZ_FLOAT) - contextCamera.data.lens = float(struct.unpack('f', temp_data)[0]) - new_chunk.bytes_read += SZ_FLOAT - contextMatrix = None # Reset matrix - CreateBlenderObject = False - CreateCameraObject = True - - elif new_chunk.ID == EDITKEYFRAME: - pass - - elif new_chunk.ID == KFDATA_KFSEG: - temp_data = file.read(struct.calcsize('I')) - start = struct.unpack('<I', temp_data)[0] - new_chunk.bytes_read += 4 - context.scene.frame_start = start - temp_data = file.read(struct.calcsize('I')) - stop = struct.unpack('<I', temp_data)[0] - new_chunk.bytes_read += 4 - context.scene.frame_end = stop - - # including these here means their EK_OB_NODE_HEADER are scanned - # another object is being processed - elif new_chunk.ID in {KFDATA_OBJECT, KFDATA_AMBIENT, KFDATA_CAMERA, KFDATA_OBJECT, KFDATA_TARGET, KFDATA_LIGHT, KFDATA_L_TARGET, }: - child = None - - elif new_chunk.ID == OBJECT_NODE_HDR: - object_name, read_str_len = read_string(file) - new_chunk.bytes_read += read_str_len - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += 4 - temp_data = file.read(SZ_U_SHORT) - hierarchy = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += 2 - child = object_dictionary.get(object_name) - - if child is None: # and object_name != '$AMBIENT$': - child = bpy.data.objects.new(object_name, None) # create an empty object - context.view_layer.active_layer_collection.collection.objects.link(child) - imported_objects.append(child) - - object_list.append(child) - object_parent.append(hierarchy) - pivot_list.append(mathutils.Vector((0.0, 0.0, 0.0))) - - elif new_chunk.ID == OBJECT_INSTANCE_NAME: - object_name, read_str_len = read_string(file) - if child.name == '$$$DUMMY': - child.name = object_name - else: - child.name += "." + object_name - object_dictionary[object_name] = child - new_chunk.bytes_read += read_str_len - - elif new_chunk.ID == OBJECT_PIVOT: # pivot - temp_data = file.read(SZ_3FLOAT) - pivot = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - pivot_list[len(pivot_list) - 1] = mathutils.Vector(pivot) - - elif KEYFRAME and new_chunk.ID == POS_TRACK_TAG: # translation - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_3FLOAT) - loc = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - if nframe == 0: - child.location = loc - - elif KEYFRAME and new_chunk.ID == ROT_TRACK_TAG and child.type == 'MESH': # rotation - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_4FLOAT) - rad, axis_x, axis_y, axis_z = struct.unpack("<4f", temp_data) - new_chunk.bytes_read += SZ_4FLOAT - if nframe == 0: - child.rotation_euler = mathutils.Quaternion( - (axis_x, axis_y, axis_z), -rad).to_euler() # why negative? - - elif KEYFRAME and new_chunk.ID == SCL_TRACK_TAG and child.type == 'MESH': # scale - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_3FLOAT) - sca = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - if nframe == 0: - child.scale = sca - - elif KEYFRAME and new_chunk.ID == COL_TRACK_TAG and child.type == 'LIGHT': # color - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_3FLOAT) - rgb = struct.unpack('<3f', temp_data) - new_chunk.bytes_read += SZ_3FLOAT - if nframe == 0: - child.data.color = rgb - - elif new_chunk.ID == FOV_TRACK_TAG and child.type == 'CAMERA': # Field of view - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_FLOAT) - fov = struct.unpack('<f', temp_data)[0] - new_chunk.bytes_read += SZ_FLOAT - if nframe == 0: - child.data.angle = math.radians(fov) - - elif new_chunk.ID == ROLL_TRACK_TAG and child.type == 'CAMERA': # Roll angle - new_chunk.bytes_read += SZ_U_SHORT * 5 - temp_data = file.read(SZ_U_SHORT * 5) - temp_data = file.read(SZ_U_SHORT) - nkeys = struct.unpack('<H', temp_data)[0] - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT * 2 - for i in range(nkeys): - temp_data = file.read(SZ_U_SHORT) - nframe = struct.unpack('<H', temp_data)[0] - new_chunk.bytes_read += SZ_U_SHORT - temp_data = file.read(SZ_U_SHORT * 2) - new_chunk.bytes_read += SZ_U_SHORT * 2 - temp_data = file.read(SZ_FLOAT) - roll = struct.unpack('<f', temp_data)[0] - new_chunk.bytes_read += SZ_FLOAT - if nframe == 0: - child.rotation_euler[1] = math.radians(roll) - - else: - buffer_size = new_chunk.length - new_chunk.bytes_read - binary_format = "%ic" % buffer_size - temp_data = file.read(struct.calcsize(binary_format)) - new_chunk.bytes_read += buffer_size - - # update the previous chunk bytes read - previous_chunk.bytes_read += new_chunk.bytes_read - - # FINISHED LOOP - # There will be a number of objects still not added - if CreateBlenderObject: - if CreateLightObject or CreateCameraObject: - pass - else: - putContextMesh(context, contextMesh_vertls, contextMesh_facels, contextMesh_flag, contextMeshMaterials, contextMesh_smooth) - - # Assign parents to objects - # check _if_ we need to assign first because doing so recalcs the depsgraph - for ind, ob in enumerate(object_list): - parent = object_parent[ind] - if parent == ROOT_OBJECT: - if ob.parent is not None: - ob.parent = ROOT_OBJECT - else: - if ob.parent != object_list[parent]: - if ob == object_list[parent]: - print(' warning: Cannot assign self to parent ', ob) - else: - ob.parent = object_list[parent] - - # pivot_list[ind] += pivot_list[parent] # XXX, not sure this is correct, should parent space matrix be applied before combining? - # fix pivots - for ind, ob in enumerate(object_list): - if ob.type == 'MESH': - pivot = pivot_list[ind] - pivot_matrix = object_matrix.get(ob, mathutils.Matrix()) # unlikely to fail - pivot_matrix = mathutils.Matrix.Translation(-1 * pivot) - #pivot_matrix = mathutils.Matrix.Translation(pivot_matrix.to_3x3() @ -pivot) - ob.data.transform(pivot_matrix) - - -def load_3ds(filepath, - context, - IMPORT_CONSTRAIN_BOUNDS=10.0, - IMAGE_SEARCH=True, - KEYFRAME=True, - APPLY_MATRIX=True, - global_matrix=None): - # global SCN - - # XXX - # if BPyMessages.Error_NoFile(filepath): - # return - - print("importing 3DS: %r..." % (filepath), end="") - - if bpy.ops.object.select_all.poll(): - bpy.ops.object.select_all(action='DESELECT') - - time1 = time.time() -# time1 = Blender.sys.time() - - current_chunk = Chunk() - - file = open(filepath, 'rb') - - # here we go! - # print 'reading the first chunk' - read_chunk(file, current_chunk) - if current_chunk.ID != PRIMARY: - print('\tFatal Error: Not a valid 3ds file: %r' % filepath) - file.close() - return - - if IMPORT_CONSTRAIN_BOUNDS: - BOUNDS_3DS[:] = [1 << 30, 1 << 30, 1 << 30, -1 << 30, -1 << 30, -1 << 30] - else: - del BOUNDS_3DS[:] - - # IMAGE_SEARCH - - # fixme, make unglobal, clear in case - object_dictionary.clear() - object_matrix.clear() - - scn = context.scene - - imported_objects = [] # Fill this list with objects - process_next_chunk(context, file, current_chunk, imported_objects, IMAGE_SEARCH, KEYFRAME) - - # fixme, make unglobal - object_dictionary.clear() - object_matrix.clear() - - # Link the objects into this scene. - # Layers = scn.Layers - - # REMOVE DUMMYVERT, - remove this in the next release when blenders internal are fixed. - - if APPLY_MATRIX: - for ob in imported_objects: - if ob.type == 'MESH': - me = ob.data - me.transform(ob.matrix_local.inverted()) - - # print(imported_objects) - if global_matrix: - for ob in imported_objects: - if ob.type == 'MESH' and ob.parent is None: - ob.matrix_world = ob.matrix_world @ global_matrix - - for ob in imported_objects: - ob.select_set(True) - - # Done DUMMYVERT - """ - if IMPORT_AS_INSTANCE: - name = filepath.split('\\')[-1].split('/')[-1] - # Create a group for this import. - group_scn = Scene.New(name) - for ob in imported_objects: - group_scn.link(ob) # dont worry about the layers - - grp = Blender.Group.New(name) - grp.objects = imported_objects - - grp_ob = Object.New('Empty', name) - grp_ob.enableDupGroup = True - grp_ob.DupGroup = grp - scn.link(grp_ob) - grp_ob.Layers = Layers - grp_ob.sel = 1 - else: - # Select all imported objects. - for ob in imported_objects: - scn.link(ob) - ob.Layers = Layers - ob.sel = 1 - """ - - context.view_layer.update() - - axis_min = [1000000000] * 3 - axis_max = [-1000000000] * 3 - global_clamp_size = IMPORT_CONSTRAIN_BOUNDS - if global_clamp_size != 0.0: - # Get all object bounds - for ob in imported_objects: - for v in ob.bound_box: - for axis, value in enumerate(v): - if axis_min[axis] > value: - axis_min[axis] = value - if axis_max[axis] < value: - axis_max[axis] = value - - # Scale objects - max_axis = max(axis_max[0] - axis_min[0], - axis_max[1] - axis_min[1], - axis_max[2] - axis_min[2]) - scale = 1.0 - - while global_clamp_size < max_axis * scale: - scale = scale / 10.0 - - scale_mat = mathutils.Matrix.Scale(scale, 4) - - for obj in imported_objects: - if obj.parent is None: - obj.matrix_world = scale_mat @ obj.matrix_world - - # Select all new objects. - print(" done in %.4f sec." % (time.time() - time1)) - file.close() - - -def load(operator, - context, - filepath="", - constrain_size=0.0, - use_image_search=True, - read_keyframe=True, - use_apply_transform=True, - global_matrix=None, - ): - - load_3ds(filepath, - context, - IMPORT_CONSTRAIN_BOUNDS=constrain_size, - IMAGE_SEARCH=use_image_search, - KEYFRAME=read_keyframe, - APPLY_MATRIX=use_apply_transform, - global_matrix=global_matrix, - ) - - return {'FINISHED'}