0

So basically this is the code I have written which is getting the required paired meshes of character in maya and I need to assign similar shader to Leg, Arm and Hand respectively.

And I was able to get meshes with different shaders and I assign the same to both of them.

but I am facing error while renaming them , the first time it's working fine but If i am trying to run this script again , it's chanding name with incremented counts like Hand_MAT1 & Hand_MATSG1.

I even tried deleting existing shaders but that doesn't works as well instead it deleted every information of those meshes.

How can i achieve it ? Any help would be great.

Thanks & Regards

import maya.cmds as cmds

transforms = cmds.ls(transforms=True)
shading_engines = {}
selected_body_parts = ["Arm","Leg","Hand"]
valid_failed_list = []
selected_transforms = []

for transform in transforms:
   if "LT_" in transform or "RT_" in transform:
       if "Hand" in transform or "Arm" in transform or "Leg" in transform:
            shapes = cmds.listRelatives(transform, shapes=True)
            if shapes:
               shading_engine = cmds.listConnections(shapes[0], type="shadingEngine")
               if shading_engine:
                    shading_engine_name = shading_engine[0]
                    shader_name = \
                            cmds.connectionInfo("%s.surfaceShader" % shading_engine_name, sfd=1).split(".")[0]
                    selected_transforms.append(transform)
                    shading_engines[transform] = [shading_engine[0], shader_name]


for transform in selected_transforms:
    if "LT_" in transform or "RT_" in transform:
                paired_transform = transform.replace("LT_", "RT_") if "LT_" in transform else transform.replace(
                    "RT_", "LT_")
                if paired_transform in shading_engines.keys():
                    if shading_engines[transform][0] != shading_engines[paired_transform][0] or \
                            shading_engines[transform][1] != shading_engines[paired_transform][1] or \
                            shading_engines[transform][0] != (transform.split('_')[1] + '_MATSG') or \
                            shading_engines[transform][1] != (transform.split('_')[1] + '_MAT'):
                        valid_failed_list.append(transform)
                        


for body_part in selected_transforms:
    first_shader = shading_engines.get(body_part)[0]
    for transform in valid_failed_list:
        if body_part.split('_')[1] in transform:
            shapes = cmds.listRelatives(transform,shapes=True)
            if shapes:
               cmds.sets(shapes[0],e=True,forceElement=first_shader)

for k, v in shading_engines.items():
            old_shading_engine_name = v[0]
            old_shader_name = v[1]
            body_part = k.split("_")[1]
            new_shading_engine_name = "{}_MATSG".format(body_part)
            new_shader_name = "{}_MAT".format(body_part)
            
            
            if cmds.objExists(old_shading_engine_name):
                cmds.rename(old_shading_engine_name, new_shading_engine_name)
            
            if cmds.objExists(old_shader_name):
                cmds.rename(old_shader_name, new_shader_name)
  • You should explaint which problems you have and post a full traceback of any error message. And you can simplify your code a bit because it doesn't matter if the parts have different shaders you can just get the first shader and assign it to all parts you want. The only difference is that it assigns the shader again to a geo it already has this shader but that doesn't matter. – haggi krey Feb 01 '23 at 14:11
  • @haggikrey I have updated the code with the specific snippet of code where i am facing issue and the error as well, also if you want to look at the whole code you can have a look here : https://drive.google.com/file/d/1ERujMjA7BIFCyL9Fpm3KgMh8rummu9SU/view Thanks – Kartikey Sinha Feb 02 '23 at 14:06
  • And you are sure that the object `Hand_MATSG1` exists? Did you execute a `ls "Hand_MATSG1"` for testing? – haggi krey Feb 02 '23 at 17:30
  • @haggikrey yes i did so basically as I am running this script sometimes it showing this error or passed argument is not a set error, I am trying it all over again , if there is any change i'll update it but I wasn't able to find the issue in there. – Kartikey Sinha Feb 03 '23 at 05:32
  • @haggikrey I have made the changes and edited the code here as well , hope I write it clear enough so that you can get the issue. – Kartikey Sinha Feb 03 '23 at 09:43

0 Answers0