Set Compound Layer Function Script DEV

Hello! I’m new in the community, I am trying to change Wall Compound Layer functions, My goal is to be able to change the wall type layer function into another layer function. I worked on a script based on different topics that I found on the forum (Set Wall Compound Layers Function), (Setting Compound Layer Function (Python Script)).

Can someone guide me please!

Here is the code

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
strc = MaterialFunctionAssignment.Structure
subs = MaterialFunctionAssignment.Substrate
ins = MaterialFunctionAssignment.Insulation
fin1 = MaterialFunctionAssignment.Finish1
fin2 = MaterialFunctionAssignment.Finish2
memb = MaterialFunctionAssignment.Membrane
strcd = MaterialFunctionAssignment.StructuralDeck
famtypes = UnwrapElement(IN[0])
indices = IN[1]
material_function_string = IN[2]
booleans = list()
counter = 0

TransactionManager.Instance.EnsureInTransaction(doc)
for ft in famtypes:
    try:
        cs = ft.GetCompoundStructure()
        if material_function_string == 'Structure [1]':
            cs.SetLayerFunction(indices[conter], strc)
        if material_function_string == 'Substrate [2]':   
            cs.SetLayerFunction(indices[conter], subs)
        if material_function_string == 'Thermal/Air Layer [3]': 
            cs.SetLayerFunction(indices[conter], ins)
        if material_function_string == 'Finish 1 [4]':
            cs.SetLayerFunction(indices[conter], fin1)
        if material_function_string == 'Finish 2 [5]':
            cs.SetLayerFunction(indices[conter], fin2)
        if material_function_string == 'Membrane Layer':
            cs.SetLayerFunction(indices[conter], memb)
        if material_function_string == 'Structural Deck [1]':
            cs.SetLayerFunction(indices[conter], strcd)
        ft.SetCompoundStructure()
        booleans.append(True)
    except:
        booleans.append(False)
    counter += 1
TransactionManager.Instance.TransactionTaskDone()