Skip to content
Snippets Groups Projects
Commit d206e7f2 authored by Bastien Montagne's avatar Bastien Montagne
Browse files

Fix T58094: Failure to import .obj files without material data.

Not sure how we did this in old code, but we certainly cannot ignore
None (a.k.a. default place holder) material anymore... Do not generate
it when not needed, though!
parent 0645fd00
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
bl_info = { bl_info = {
"name": "Wavefront OBJ format", "name": "Wavefront OBJ format",
"author": "Campbell Barton, Bastien Montagne", "author": "Campbell Barton, Bastien Montagne",
"version": (3, 5, 1), "version": (3, 5, 2),
"blender": (2, 80, 0), "blender": (2, 80, 0),
"location": "File > Import-Export", "location": "File > Import-Export",
"description": "Import-Export OBJ, Import OBJ mesh, UV's, materials and textures", "description": "Import-Export OBJ, Import OBJ mesh, UV's, materials and textures",
......
...@@ -94,6 +94,7 @@ def create_materials(filepath, relpath, ...@@ -94,6 +94,7 @@ def create_materials(filepath, relpath,
assign colors and images to the materials from all referenced material libs assign colors and images to the materials from all referenced material libs
""" """
from math import sqrt from math import sqrt
from bpy_extras import node_shader_utils
DIR = os.path.dirname(filepath) DIR = os.path.dirname(filepath)
context_material_vars = set() context_material_vars = set()
...@@ -176,21 +177,19 @@ def create_materials(filepath, relpath, ...@@ -176,21 +177,19 @@ def create_materials(filepath, relpath,
else: else:
raise Exception("invalid type %r" % type) raise Exception("invalid type %r" % type)
# Add an MTL with the same name as the obj if no MTLs are spesified. # Try to find a MTL with the same name as the OBJ if no MTLs are specified.
temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + ".mtl" temp_mtl = os.path.splitext((os.path.basename(filepath)))[0] + ".mtl"
if os.path.exists(os.path.join(DIR, temp_mtl)): if os.path.exists(os.path.join(DIR, temp_mtl)):
material_libs.add(temp_mtl) material_libs.add(temp_mtl)
del temp_mtl del temp_mtl
# Create new materials # Create new materials
for name in unique_materials: # .keys() for name in unique_materials: # .keys()
if name is not None: ma_name = "Default OBJ" if name is None else name.decode('utf-8', "replace")
ma = unique_materials[name] = bpy.data.materials.new(name.decode('utf-8', "replace")) ma = unique_materials[name] = bpy.data.materials.new(ma_name)
from bpy_extras import node_shader_utils ma_wrap = node_shader_utils.PrincipledBSDFWrapper(ma, is_readonly=False)
ma_wrap = node_shader_utils.PrincipledBSDFWrapper(ma, is_readonly=False) nodal_material_wrap_map[ma] = ma_wrap
nodal_material_wrap_map[ma] = ma_wrap ma_wrap.use_nodes = True
ma_wrap.use_nodes = True
for libname in sorted(material_libs): for libname in sorted(material_libs):
# print(libname) # print(libname)
...@@ -479,7 +478,7 @@ def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP): ...@@ -479,7 +478,7 @@ def split_mesh(verts_loc, faces, unique_materials, filepath, SPLIT_OB_OR_GROUP):
face_vert_loc_indices[loop_idx] = map_index # remap to the local index face_vert_loc_indices[loop_idx] = map_index # remap to the local index
if context_material and context_material not in unique_materials_split: if context_material not in unique_materials_split:
unique_materials_split[context_material] = unique_materials[context_material] unique_materials_split[context_material] = unique_materials[context_material]
faces_split.append(face) faces_split.append(face)
...@@ -902,6 +901,7 @@ def load(context, ...@@ -902,6 +901,7 @@ def load(context,
context_parm = b'' # used by nurbs too but could be used elsewhere context_parm = b'' # used by nurbs too but could be used elsewhere
# Until we can use sets # Until we can use sets
use_default_material = False
unique_materials = {} unique_materials = {}
unique_smooth_groups = {} unique_smooth_groups = {}
# unique_obects= {} - no use for this variable since the objects are stored in the face. # unique_obects= {} - no use for this variable since the objects are stored in the face.
...@@ -982,6 +982,8 @@ def load(context, ...@@ -982,6 +982,8 @@ def load(context,
verts_loc_len = len(verts_loc) verts_loc_len = len(verts_loc)
verts_nor_len = len(verts_nor) verts_nor_len = len(verts_nor)
verts_tex_len = len(verts_tex) verts_tex_len = len(verts_tex)
if context_material is None:
use_default_material = True
# Else, use face_vert_loc_indices and face_vert_tex_indices previously defined and used the obj_face # Else, use face_vert_loc_indices and face_vert_tex_indices previously defined and used the obj_face
context_multi_line = b'f' if strip_slash(line_split) else b'' context_multi_line = b'f' if strip_slash(line_split) else b''
...@@ -1152,6 +1154,8 @@ def load(context, ...@@ -1152,6 +1154,8 @@ def load(context,
progress.step("Done, loading materials and images...") progress.step("Done, loading materials and images...")
if use_default_material:
unique_materials[None] = None
create_materials(filepath, relpath, material_libs, unique_materials, create_materials(filepath, relpath, material_libs, unique_materials,
use_image_search, float_func) use_image_search, float_func)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment