From 635aa6ab32efad0ef7169f9f697b126699045b7e Mon Sep 17 00:00:00 2001 From: Rami Tabbara Date: Mon, 15 Jan 2024 10:02:39 +0100 Subject: [PATCH] Blender 4.0 fixes (#88) * Need to bump Mitsuba dependency to 3.5.0 to benefit from version check fix * Renaming of some principled BSDF slots * Also some principled BSDF slots changed from float to color types --- .github/workflows/test.yml | 7 ++-- mitsuba-blender/__init__.py | 2 +- mitsuba-blender/io/exporter/geometry.py | 4 ++- mitsuba-blender/io/exporter/materials.py | 35 +++++++++++++++---- mitsuba-blender/io/importer/materials.py | 44 ++++++++++++++++++++---- 5 files changed, 74 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f4d6555..9262c2f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,11 +25,11 @@ jobs: environment: - { os: "ubuntu-latest", - mitsuba-version: "3.4.1" + mitsuba-version: "3.5.0" } - { os: "windows-latest", - mitsuba-version: "3.4.1" + mitsuba-version: "3.5.0" } blender: - { @@ -38,6 +38,9 @@ jobs: - { version: "3.6" } + - { + version: "4.0" + } steps: - name: Git checkout diff --git a/mitsuba-blender/__init__.py b/mitsuba-blender/__init__.py index 913c7b3..c972713 100644 --- a/mitsuba-blender/__init__.py +++ b/mitsuba-blender/__init__.py @@ -22,7 +22,7 @@ from . import io, engine -DEPS_MITSUBA_VERSION = '3.4.1' +DEPS_MITSUBA_VERSION = '3.5.0' def get_addon_preferences(context): return context.preferences.addons[__name__].preferences diff --git a/mitsuba-blender/io/exporter/geometry.py b/mitsuba-blender/io/exporter/geometry.py index 6b341e6..27daef0 100644 --- a/mitsuba-blender/io/exporter/geometry.py +++ b/mitsuba-blender/io/exporter/geometry.py @@ -26,7 +26,9 @@ def convert_mesh(export_ctx, b_mesh, matrix_world, name, mat_nr): 'type': 'blender', 'version': ".".join(map(str,bpy.app.version)) } - b_mesh.calc_normals() + + if bpy.app.version < (4, 0, 0): + b_mesh.calc_normals() # Compute the triangle tesselation b_mesh.calc_loop_triangles() diff --git a/mitsuba-blender/io/exporter/materials.py b/mitsuba-blender/io/exporter/materials.py index bf589b0..7ca698e 100644 --- a/mitsuba-blender/io/exporter/materials.py +++ b/mitsuba-blender/io/exporter/materials.py @@ -1,3 +1,4 @@ +import bpy import numpy as np from mathutils import Matrix from .export_context import Files @@ -260,18 +261,38 @@ def convert_mix_materials_cycles(export_ctx, current_node):#TODO: test and fix t def convert_principled_materials_cycles(export_ctx, current_node): params = {} + + if bpy.app.version >= (4, 0, 0): + specular_key = 'Specular IOR Level' + transmission_key = 'Transmission Weight' + sheen_key = 'Sheen Weight' + clearcoat_key = 'Coat Weight' + clearcoat_roughness_key = 'Coat Roughness' + else: + specular_key = 'Specular' + transmission_key = 'Transmission' + sheen_key = 'Sheen' + clearcoat_key = 'Clearcoat' + clearcoat_roughness_key = 'Clearcoat Roughness' + base_color = convert_color_texture_node(export_ctx, current_node.inputs['Base Color']) - specular = current_node.inputs['Specular'].default_value - specular_tint = convert_float_texture_node(export_ctx, current_node.inputs['Specular Tint']) - specular_trans = convert_float_texture_node(export_ctx, current_node.inputs['Transmission']) + specular = current_node.inputs[specular_key].default_value + if bpy.app.version >= (4, 0, 0): + specular_tint = convert_color_texture_node(export_ctx, current_node.inputs['Specular Tint']) + else: + specular_tint = convert_float_texture_node(export_ctx, current_node.inputs['Specular Tint']) + specular_trans = convert_float_texture_node(export_ctx, current_node.inputs[transmission_key]) ior = current_node.inputs['IOR'].default_value roughness = convert_float_texture_node(export_ctx, current_node.inputs['Roughness']) metallic = convert_float_texture_node(export_ctx, current_node.inputs['Metallic']) anisotropic = convert_float_texture_node(export_ctx, current_node.inputs['Anisotropic']) - sheen = convert_float_texture_node(export_ctx, current_node.inputs['Sheen']) - sheen_tint = convert_float_texture_node(export_ctx, current_node.inputs['Sheen Tint']) - clearcoat = convert_float_texture_node(export_ctx, current_node.inputs['Clearcoat']) - clearcoat_roughness = convert_float_texture_node(export_ctx, current_node.inputs['Clearcoat Roughness']) + sheen = convert_float_texture_node(export_ctx, current_node.inputs[sheen_key]) + if bpy.app.version >= (4, 0, 0): + sheen_tint = convert_color_texture_node(export_ctx, current_node.inputs['Sheen Tint']) + else: + sheen_tint = convert_float_texture_node(export_ctx, current_node.inputs['Sheen Tint']) + clearcoat = convert_float_texture_node(export_ctx, current_node.inputs[clearcoat_key]) + clearcoat_roughness = convert_float_texture_node(export_ctx, current_node.inputs[clearcoat_roughness_key]) params.update({ 'type': 'principled', diff --git a/mitsuba-blender/io/importer/materials.py b/mitsuba-blender/io/importer/materials.py index 2c6f78d..eedf480 100644 --- a/mitsuba-blender/io/importer/materials.py +++ b/mitsuba-blender/io/importer/materials.py @@ -247,6 +247,13 @@ def write_mi_rgb_property(mi_context, mi_mat, mi_prop_name, bl_mat_wrap, out_soc mi_prop_type = mi_mat.type(mi_prop_name) if mi_prop_type == Properties.Type.Color: write_mi_rgb_value(mi_context, list(mi_mat.get(mi_prop_name, default)), bl_mat_wrap, out_socket_id) + if mi_prop_type == Properties.Type.Float: + if mi_prop_name in mi_mat: + col_val = mi_mat.get(mi_prop_name) + col = [col_val, col_val, col_val] + else: + col = default + write_mi_rgb_value(mi_context, list(col), bl_mat_wrap, out_socket_id) elif mi_prop_type == Properties.Type.NamedReference: mi_texture_ref_id = mi_mat.get(mi_prop_name) mi_texture = mi_context.mi_scene_props.get_with_id_and_class(mi_texture_ref_id, 'Texture') @@ -331,19 +338,42 @@ def write_mi_emitter_bsdf(mi_context, bl_mat_wrap, out_socket_id, mi_emitter): ###################### def write_mi_principled_bsdf(mi_context, mi_mat, bl_mat_wrap, out_socket_id, mi_bump=None, mi_normal=None): + if bpy.app.version >= (4, 0, 0): + specular_key = 'Specular IOR Level' + transmission_key = 'Transmission Weight' + sheen_key = 'Sheen Weight' + clearcoat_key = 'Coat Weight' + clearcoat_roughness_key = 'Coat Roughness' + else: + specular_key = 'Specular' + transmission_key = 'Transmission' + sheen_key = 'Sheen' + clearcoat_key = 'Clearcoat' + clearcoat_roughness_key = 'Clearcoat Roughness' + + bl_principled = bl_mat_wrap.ensure_node_type([out_socket_id], 'ShaderNodeBsdfPrincipled', 'BSDF') bl_principled_wrap = bl_shader_utils.NodeMaterialWrapper(bl_mat_wrap.bl_mat, out_node=bl_principled) write_mi_rgb_property(mi_context, mi_mat, 'base_color', bl_principled_wrap, 'Base Color', [0.8, 0.8, 0.8]) - write_mi_float_property(mi_context, mi_mat, 'specular', bl_principled_wrap, 'Specular', 0.5) - write_mi_float_property(mi_context, mi_mat, 'spec_tint', bl_principled_wrap, 'Specular Tint', 0.0) - write_mi_float_property(mi_context, mi_mat, 'spec_trans', bl_principled_wrap, 'Transmission', 0.0) + write_mi_float_property(mi_context, mi_mat, 'specular', bl_principled_wrap, specular_key, 0.5) + if bpy.app.version >= (4, 0, 0): + write_mi_rgb_property(mi_context, mi_mat, 'spec_tint', bl_principled_wrap, 'Specular Tint', [0.0, 0.0, 0.0]) + else: + write_mi_float_property(mi_context, mi_mat, 'spec_tint', bl_principled_wrap, 'Specular Tint', 0.0) + + write_mi_float_property(mi_context, mi_mat, 'spec_trans', bl_principled_wrap, transmission_key, 0.0) write_mi_float_property(mi_context, mi_mat, 'metallic', bl_principled_wrap, 'Metallic', 0.0) write_mi_float_property(mi_context, mi_mat, 'anisotropic', bl_principled_wrap, 'Anisotropic', 0.0) write_mi_roughness_property(mi_context, mi_mat, 'roughness', bl_principled_wrap, 'Roughness', 0.4) - write_mi_float_property(mi_context, mi_mat, 'sheen', bl_principled_wrap, 'Sheen', 0.0) - write_mi_float_property(mi_context, mi_mat, 'sheen_tint', bl_principled_wrap, 'Sheen Tint', 0.5) - write_mi_float_property(mi_context, mi_mat, 'clearcoat', bl_principled_wrap, 'Clearcoat', 0.0) - write_mi_roughness_property(mi_context, mi_mat, 'clearcoat_gloss', bl_principled_wrap, 'Clearcoat Roughness', 0.03) + write_mi_float_property(mi_context, mi_mat, 'sheen', bl_principled_wrap, sheen_key, 0.0) + + if bpy.app.version >= (4, 0, 0): + write_mi_rgb_property(mi_context, mi_mat, 'sheen_tint', bl_principled_wrap, 'Sheen Tint', [0.5, 0.5, 0.5]) + else: + write_mi_float_property(mi_context, mi_mat, 'sheen_tint', bl_principled_wrap, 'Sheen Tint', 0.5) + + write_mi_float_property(mi_context, mi_mat, 'clearcoat', bl_principled_wrap, clearcoat_key, 0.0) + write_mi_roughness_property(mi_context, mi_mat, 'clearcoat_gloss', bl_principled_wrap, clearcoat_roughness_key, 0.03) # Write normal and bump maps write_mi_bump_and_normal_maps(mi_context, bl_principled_wrap, 'Normal', mi_bump=mi_bump, mi_normal=mi_normal) return True