DragonFF
DragonFF copied to clipboard
Import Material names as the actual material, not the model name
When importing a DFF, i think its better to name the Materials after their texture name, not the model name. so when exporting a OBJ its using these for texturing.
this is how its right now the "Base Color" attribute shows the correct texture name being used. https://i.imgur.com/Tyaozrw.png
this is how it should be with the material named after the texture. https://i.imgur.com/gRn37zy.png
i noticed this being an issue when importing into another game engine. this way i have to rename every material like ws_bridgepavement to baybridge_sfse.2 to be recognized.
Would be cool for it to be a combo of both... Like, texture.object.# or object.texture.#.
im running this script after importing to fix this issue for me. material can have multiple textures this maybe the reason hes using the material name. id prefer the first textures name and if theres no texture, the model file name is fine.
import os
import re
# Set the material_index to 0 ( the first material )
for obj in bpy.context.selected_objects:
if obj.type == 'MESH' and obj.data.materials:
obj.active_material_index = 0
# Go through list of materials assigned to selected object
for material in obj.data.materials:
material_index = obj.active_material_index
old_name = material.name
try:
# Get its first material slot
material = obj.material_slots[material_index].material
# Get the nodes in the node tree
nodes = material.node_tree.nodes
# Get a principled node
principled = next(n for n in nodes if n.type == 'BSDF_PRINCIPLED')
# Get the slot for 'base color'
base_color = principled.inputs['Base Color'] #Or principled.inputs[0]
# Get the link
link = base_color.links[0]
link_node = link.from_node
# Rename the material to the image name excluding the extension
material.name = os.path.splitext( link_node.image.name )[0]
# Print the results
print( "Material Old Name:", old_name, )
print( "Material New Name:", material.name )
print( )
except:
print( 'not found' )
# Add 1 to the material_index count ( move to the next material )
obj.active_material_index +=1
print("FINISHED")```
How do I run this Script?
This issue is a big Problem in UE4: I have only one Texture and about 5 different Materials for it.
I have found a Similar Script on Stackexchange. It works for me but only for One Mesh at a Time. (Duplicate and Joining the Meshes is much faster) I've added "M_"+ to the new Material name to avoid Automatic Renaming in Unreal Engine 4 (Not Included in the Linked Script) https://blender.stackexchange.com/questions/122485/how-to-batch-rename-materials-or-auto-assign-them-to-the-name-of-the-texture-fil#new-answer
And a Second Script for ReAssign and Delete the Doubled Materials, it does work fine. (It's better to Run both scripts several Times to ReAssign and Delete ALL Materials) https://blender.stackexchange.com/a/119258/139581