Help me before I come insane!

I imported a crowd sim from Anima. Once in Blender I get basic materials, just the diffuse. I found out that if I export the same model from Maya in FBX, I get the right materials with diffuse, roughness and normal map. Problem is, I have link the materials one charater at a time. I pick character A from the collada version, then the same one from Maya’s FBX and link the materials. Repete for every characters… There must be a way to script that.

The characters have the exact same names so it’s just a question of matching the pairs in the right order and link the material. Here’s a demo file with two characters if someone wants to give it a shot. I would know how to code it in Maya but not in Blender.

https://www.dropbox.com/s/r9ljvvvbeouahja/demoScene.rar?dl=0

Assuming that objects from maya and objects from colada have the same number of material slots:

import bpy, re
context = bpy.context

def get_base_name(full_name):
    index = re.findall(r'\d+', full_name)
    base_name = full_name[:-len(index[-1])]
    return base_name

sel_obs = context.selected_objects
link_from_obs = {get_base_name(ob.name):ob for ob in context.view_layer.objects if ob not in sel_obs and ob.type == 'MESH'}

for ob in sel_obs:
    link_from_ob = link_from_obs[get_base_name(ob.name)]
    for i, slot in enumerate(ob.material_slots):
        slot.material = link_from_ob.material_slots[i].material

Select objects from collada and run the script

1 Like

Once again, you saved the day! Thank you so much!

Oh! I tried the script and I go this message:

Python script failed, check the message in the system console. But I don’t see any messages in the console.

On my side, I made sure that the names are the same and they have the same number of materials.I selected the collada models and ran the script.

The message is found in the Window menu, last entry, Toggle System Console.

image

Ok! Ok, I thought it was the console we see in the scripting tab. So this is what I get:

KeyError: ‘C036_030_v03_cman0200m4_ANI_0000.’

I think I know what the problem is. When I import the FBX from maya, Blender adds .001 at the end of the name so that it doesn’t conflict with the other one with the same name.

Yes, in your demo scene objects were named
testBlender_ani_bwom0301m4_ANI_0000
testBlender_ani_bwom0301m4_ANI_00000

I didn’t thought about any .001
So try splitting name on the last "_" and link from the object with the part before "_" in the name

import bpy
context = bpy.context

def get_base_name(full_name):
    base_name = full_name.rpartition('_')[0]
    return base_name

sel_obs = [ob for ob in context.selected_objects if ob.type == 'MESH']
link_from_obs = {get_base_name(ob.name):ob for ob in context.view_layer.objects if ob not in sel_obs and ob.type == 'MESH'}

for ob in sel_obs:
    link_from_ob = link_from_obs[get_base_name(ob.name)]
    for i, slot in enumerate(ob.material_slots):
        slot.material = link_from_ob.material_slots[i].material
1 Like

Yep! That worked perfectly! Thanks again. :slight_smile: