From a42c94e8ea3c27bfa32e157034959dd4944c77c6 Mon Sep 17 00:00:00 2001 From: Sebastian Sille Date: Sat, 18 Feb 2023 19:22:17 +0100 Subject: [PATCH 1/3] io_scene_3ds: Update for Blender 3.x Pull Request #5 --- io_scene_3ds/__init__.py | 4 ++-- io_scene_3ds/export_3ds.py | 29 ++++++++++++++--------------- io_scene_3ds/import_3ds.py | 27 ++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/io_scene_3ds/__init__.py b/io_scene_3ds/__init__.py index 2ccb62e..3912ac1 100644 --- a/io_scene_3ds/__init__.py +++ b/io_scene_3ds/__init__.py @@ -32,8 +32,8 @@ import bpy bl_info = { "name": "Autodesk 3DS format", "author": "Bob Holcomb, Campbell Barton, Andreas Atteneder, Sebastian Schrand", - "version": (2, 1, 0), - "blender": (2, 82, 0), + "version": (2, 2, 0), + "blender": (3, 0, 0), "location": "File > Import", "description": "Import 3DS, meshes, uvs, materials, textures, " "cameras & lamps", diff --git a/io_scene_3ds/export_3ds.py b/io_scene_3ds/export_3ds.py index dd46b4d..2a22d74 100644 --- a/io_scene_3ds/export_3ds.py +++ b/io_scene_3ds/export_3ds.py @@ -645,8 +645,8 @@ def make_material_chunk(material, image): name_str = material.name if material else "None" - if image: - name_str += image.name + #if image: + # name_str += image.name name.add_variable("name", _3ds_string(sane_name(name_str))) material_chunk.add_subchunk(name) @@ -670,6 +670,7 @@ def make_material_chunk(material, image): 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) if wrap.base_color_texture: @@ -704,13 +705,10 @@ def make_material_chunk(material, image): normal = [wrap.normalmap_texture] bump = wrap.normalmap_strength b_pct = min(bump, 1) - bumpval = min(999, (bump * 100)) # 3ds max bump = 999 - strength = _3ds_chunk(MAT_BUMP_PERCENT) - strength.add_variable("bump_pct", _3ds_ushort(int(bumpval))) matmap = make_material_texture_chunk(MAT_BUMPMAP, normal, b_pct) if matmap: material_chunk.add_subchunk(matmap) - material_chunk.add_subchunk(strength) + material_chunk.add_subchunk(make_percent_subchunk(MAT_BUMP_PERCENT, b_pct)) if wrap.roughness_texture: roughness = [wrap.roughness_texture] @@ -720,7 +718,7 @@ def make_material_chunk(material, image): material_chunk.add_subchunk(matmap) if wrap.emission_color_texture: - e_pct = sum(wrap.emission_color[:]) * .25 + e_pct = wrap.emission_strength emission = [wrap.emission_color_texture] matmap = make_material_texture_chunk(MAT_SELFIMAP, emission, e_pct) if matmap: @@ -907,8 +905,8 @@ def make_faces_chunk(tri_list, mesh, materialDict): context_face_array = unique_mats[ma, img][1] except: name_str = ma if ma else "None" - if img: - name_str += img + #if img: + # name_str += img context_face_array = _3ds_array() unique_mats[ma, img] = _3ds_string(sane_name(name_str)), context_face_array @@ -1169,7 +1167,7 @@ def save(operator, ): import time - from bpy_extras.io_utils import create_derived_objects, free_derived_objects + #from bpy_extras.io_utils import create_derived_objects, free_derived_objects """Save the Blender scene to a 3ds file.""" @@ -1185,7 +1183,7 @@ def save(operator, scene = context.scene layer = context.view_layer - #depsgraph = context.evaluated_depsgraph_get() + depsgraph = context.evaluated_depsgraph_get() # Initialize the main chunk (primary): primary = _3ds_chunk(PRIMARY) @@ -1233,7 +1231,9 @@ def save(operator, for ob in objects: # get derived objects - free, derived = create_derived_objects(scene, ob) + #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 @@ -1242,7 +1242,6 @@ def save(operator, if ob.type not in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}: continue - #ob_derived_eval = ob_derived.evaluated_get(depsgraph) try: data = ob_derived.to_mesh() except: @@ -1286,8 +1285,8 @@ def save(operator, # ob_derived_eval.to_mesh_clear() - if free: - free_derived_objects(ob) + #if free: + # free_derived_objects(ob) # Make material chunks for all materials used in the meshes: for ma_image in materialDict.values(): diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py index 304ca70..8dfa475 100644 --- a/io_scene_3ds/import_3ds.py +++ b/io_scene_3ds/import_3ds.py @@ -63,6 +63,7 @@ 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 +MATSELFILPCT = 0xA084 # Self illumination strength (percent) MAT_WIRE = 0xA085 # Only render's wireframe MAT_TEXTURE_MAP = 0xA200 # This is a header for a new texture map @@ -465,6 +466,7 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE 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 @@ -667,6 +669,18 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE print("Cannot read material transparency") new_chunk.bytes_read += temp_chunk.bytes_read + elif new_chunk.ID == MATSELFILPCT: + 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_TEXTURE_MAP: read_texture(new_chunk, temp_chunk, "Diffuse", "COLOR") @@ -684,11 +698,18 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE read_texture(new_chunk, temp_chunk, "Bump", "NORMAL") elif new_chunk.ID == MAT_BUMP_PERCENT: - temp_data = file.read(SZ_U_SHORT) - new_chunk.bytes_read += SZ_U_SHORT - contextWrapper.normalmap_strength = (float(struct.unpack(' Date: Sat, 18 Feb 2023 19:27:03 +0100 Subject: [PATCH 2/3] Cleanup: remove trailing space Signed-off-by: Sebastian Sille --- io_scene_3ds/import_3ds.py | 1 - 1 file changed, 1 deletion(-) diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py index 8dfa475..dc876d6 100644 --- a/io_scene_3ds/import_3ds.py +++ b/io_scene_3ds/import_3ds.py @@ -709,7 +709,6 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE contextWrapper.normalmap_strength = float(struct.unpack('f', temp_data)[0]) new_chunk.bytes_read += temp_chunk.bytes_read - elif new_chunk.ID == MAT_SHIN_MAP: read_texture(new_chunk, temp_chunk, "Shininess", "ROUGHNESS") -- 2.30.2 From 591cd7116ea1a2fc8a1fad398159434e273a7adb Mon Sep 17 00:00:00 2001 From: Sebastian Sille Date: Sun, 19 Feb 2023 21:07:19 +0100 Subject: [PATCH 3/3] Added material shading method Materials will be copied to nodes for Phong and Gouraud shading Signed-off-by: Sebastian Sille --- io_scene_3ds/import_3ds.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/io_scene_3ds/import_3ds.py b/io_scene_3ds/import_3ds.py index dc876d6..b9e7e64 100644 --- a/io_scene_3ds/import_3ds.py +++ b/io_scene_3ds/import_3ds.py @@ -63,8 +63,9 @@ 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 -MATSELFILPCT = 0xA084 # Self illumination strength (percent) +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 @@ -669,7 +670,7 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE print("Cannot read material transparency") new_chunk.bytes_read += temp_chunk.bytes_read - elif new_chunk.ID == MATSELFILPCT: + 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) @@ -681,6 +682,21 @@ def process_next_chunk(context, file, previous_chunk, imported_objects, IMAGE_SE 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") -- 2.30.2