117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright (C) 2023 Ilia Kurochkin <brothermechanic@gmail.com>
|
|
#
|
|
# 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 3 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.
|
|
__version__ = '0.2'
|
|
import logging
|
|
import sys
|
|
import bpy
|
|
from bpy_extras.node_shader_utils import PrincipledBSDFWrapper
|
|
from utils.shininess_to_roughness import shiny_to_rough
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
|
|
def assign_materials(bobj, fem_mat):
|
|
''' Build Blender shader from FreeCAD's FEM material '''
|
|
fem_mat_name = fem_mat.Material['Name']
|
|
|
|
if fem_mat_name in bpy.data.materials:
|
|
# prepare for reimport
|
|
if len(bobj.material_slots) < 1:
|
|
bobj.data.materials.append(bpy.data.materials[fem_mat_name])
|
|
else:
|
|
bobj.material_slots[0].material = bpy.data.materials[fem_mat_name]
|
|
else:
|
|
if 'DiffuseColor' in fem_mat.Material.keys():
|
|
d_col_str = fem_mat.Material['DiffuseColor']
|
|
d_col4 = tuple(
|
|
map(float, d_col_str[1:-1].split(', ')))
|
|
d_col = d_col4[:-1]
|
|
else:
|
|
d_col = (0.5, 0.5, 0.5)
|
|
if 'Father' in fem_mat.Material.keys():
|
|
if fem_mat.Material['Father'] == 'Metal':
|
|
me = 1
|
|
else:
|
|
me = 0
|
|
else:
|
|
me = 0
|
|
if 'Shininess' in fem_mat.Material.keys():
|
|
shiny = float(fem_mat.Material['Shininess'])
|
|
if shiny == 0:
|
|
rg = 0.5
|
|
else:
|
|
rg = shiny_to_rough(shiny)
|
|
else:
|
|
rg = 0.5
|
|
if 'EmissiveColor' in fem_mat.Material.keys():
|
|
e_col_str = fem_mat.Material['EmissiveColor']
|
|
e_col4 = tuple(
|
|
map(float, e_col_str[1:-1].split(', ')))
|
|
e_col = e_col4[:-1]
|
|
else:
|
|
e_col = (0.0, 0.0, 0.0)
|
|
if 'Transparency' in fem_mat.Material.keys():
|
|
tr_str = fem_mat.Material['Transparency']
|
|
alpha = 1.0 - float(tr_str)
|
|
else:
|
|
alpha = 1.0
|
|
|
|
bmat = bpy.data.materials.new(name=fem_mat_name)
|
|
bmat.use_nodes = True
|
|
principled = PrincipledBSDFWrapper(bmat, is_readonly=False)
|
|
principled.base_color = d_col
|
|
principled.metallic = me
|
|
principled.roughness = rg
|
|
principled.emission_color = e_col
|
|
principled.alpha = alpha
|
|
# prepare for reimport
|
|
if len(bobj.material_slots) < 1:
|
|
bobj.data.materials.append(bmat)
|
|
else:
|
|
bobj.material_slots[0].material = bmat
|
|
|
|
logger.debug('Assign %s to object %s', fem_mat_name, bobj.name)
|
|
return bobj
|
|
|
|
|
|
def assign_black(bobj):
|
|
''' Set absolute black Blender shader '''
|
|
fem_mat_name = 'black_mat'
|
|
|
|
if fem_mat_name in bpy.data.materials:
|
|
# prepare for reimport
|
|
if len(bobj.material_slots) < 1:
|
|
bobj.data.materials.append(bpy.data.materials[fem_mat_name])
|
|
else:
|
|
bobj.material_slots[0].material = bpy.data.materials[fem_mat_name]
|
|
else:
|
|
bmat = bpy.data.materials.new(name=fem_mat_name)
|
|
bmat.use_nodes = True
|
|
bmat.diffuse_color = (0, 0, 0, 1)
|
|
bmat.node_tree.nodes.remove(bmat.node_tree.nodes['Principled BSDF'])
|
|
emission = bmat.node_tree.nodes.new(type='ShaderNodeEmission')
|
|
emission.location = 0, 300
|
|
emission.inputs['Color'].default_value = (0, 0, 0, 1)
|
|
emission.inputs['Strength'].default_value = 0
|
|
bmat.node_tree.links.new(
|
|
emission.outputs['Emission'],
|
|
bmat.node_tree.nodes['Material Output'].inputs['Surface'])
|
|
# prepare for reimport
|
|
if len(bobj.material_slots) < 1:
|
|
bobj.data.materials.append(bmat)
|
|
else:
|
|
bobj.material_slots[0].material = bmat
|
|
|
|
logger.debug('Assign %s to object %s', fem_mat_name, bobj.name)
|
|
return bobj
|