Export/Import Skin weight tool (process to slow when Importing skin from dictionary)


#1

class Skin_Exporter():

def __init__(self):
    self.prev_progress = 0
    self.json_path = ""
    self.skin_cluster = ""

def json_reader(self,json_file,var_name=""):
    with open(json_file) as f:
        data = json.load(f)
        if var_name == "":
            return data
        return data[var_name]

def json_writer(self,json_file,data_to_write):
    with open(json_file, 'w') as f:
        json.dump(data_to_write, f, indent = 4)

def Save_Skin(self):
    geo_name = cmds.ls(sl= True)[0]
    vert_count = cmds.polyEvaluate(geo_name, v= True)
    vert_string = ".vtx[{}]"
    vert_geo = geo_name+vert_string
    skin_transforms = cmds.skinPercent(self.skin_cluster, vert_geo.format(0), q = True, t = None)
    skin_dict = {}
    for i in range(0, vert_count):
        self.progress_manager(i,vert_count)
        skin_weights = cmds.skinPercent( self.skin_cluster, vert_geo.format(i), q = True, v = True)
        skin_dict[vert_string.format(i)] = (zip(skin_transforms,skin_weights))

    
    self.json_writer(self.json_path, skin_dict)       
    print("skin has been saved at " + self.json_path)
    
def Load_Skin(self):
    geo_name = cmds.ls(sl= True)[0]
    skin_data = self.json_reader(self.json_path)
    for i, key in enumerate(skin_data.keys()):
        self.progress_manager(i,len(skin_data.keys()))
        cmds.skinPercent(self.skin_cluster, geo_name+key, tv =skin_data[key], zri = True)

    print("skin has been loaded")

def progress_manager(self, current_progress, max):
    cur_progress = str(round(current_progress/float(max)*100, 1)) + "%"
    if(cur_progress != self.prev_progress):
        self.prev_progress = cur_progress
        print(cur_progress)

everything seems to run pretty fast when saving skin information to JSON, roughly takes around 30 second for 20,000 verts. However when using the Load_Skin function, it will take around 10-15 minutes. Is there any other idea or way to apply skin weights that is faster than skinPercent? or maybe is there any redundancy i can cut on my code?

Thanks for the help!
-Brandon