def execute(self, context): obj = context.active_object current_idx = obj.active_material_index next_index = current_idx + 1 try: FnMaterial.swap_materials(obj, current_idx, next_index, reverse=True, swap_slots=True) except MaterialNotFoundError: self.report({'ERROR'}, 'Materials not found') return {'CANCELLED'} obj.active_material_index = next_index return {'FINISHED'}
def execute(self, context): obj = context.active_object root = mmd_model.Model.findRoot(obj) rig = mmd_model.Model(root) for meshObj in rig.meshes(): mats_to_delete = [] for mat in meshObj.data.materials: if mat and "_temp" in mat.name: mats_to_delete.append(mat) for temp_mat in reversed(mats_to_delete): base_mat_name = temp_mat.name.split('_temp')[0] try: FnMaterial.swap_materials(meshObj, temp_mat.name, base_mat_name) except MaterialNotFoundError: self.report({'WARNING'}, 'Base material for %s was not found' % temp_mat.name) else: temp_idx = meshObj.data.materials.find(temp_mat.name) mat = meshObj.data.materials.pop(index=temp_idx) if mat.users < 1: bpy.data.materials.remove(mat) return {'FINISHED'}
def execute(self, context): obj = context.active_object root = mmd_model.Model.findRoot(obj) rig = mmd_model.Model(root) mmd_root = root.mmd_root morph = mmd_root.material_morphs[mmd_root.active_morph] mat_data = morph.data[morph.active_data] meshObj = rig.findMesh(mat_data.related_mesh) if meshObj is None: self.report({'ERROR'}, "The model mesh can't be found") return {'CANCELLED'} base_mat = meshObj.data.materials.get(mat_data.material, None) if base_mat is None: self.report({'ERROR'}, 'Material "%s" not found' % mat_data.material) return {'CANCELLED'} work_mat_name = base_mat.name + '_temp' if work_mat_name in bpy.data.materials: self.report({'ERROR'}, 'Temporary material "%s" is in use' % work_mat_name) return {'CANCELLED'} work_mat = base_mat.copy() work_mat.name = work_mat_name meshObj.data.materials.append(work_mat) FnMaterial.swap_materials(meshObj, base_mat.name, work_mat.name) base_mmd_mat = base_mat.mmd_material work_mmd_mat = work_mat.mmd_material work_mmd_mat.material_id = -1 # Apply the offsets if mat_data.offset_type == "MULT": diffuse_offset = multiply_vector_components( base_mmd_mat.diffuse_color, mat_data.diffuse_color[0:3]) specular_offset = multiply_vector_components( base_mmd_mat.specular_color, mat_data.specular_color) edge_offset = multiply_vector_components(base_mmd_mat.edge_color, mat_data.edge_color) ambient_offset = multiply_vector_components( base_mmd_mat.ambient_color, mat_data.ambient_color) work_mmd_mat.diffuse_color = diffuse_offset work_mmd_mat.alpha *= mat_data.diffuse_color[3] work_mmd_mat.specular_color = specular_offset work_mmd_mat.shininess *= mat_data.shininess work_mmd_mat.ambient_color = ambient_offset work_mmd_mat.edge_color = edge_offset work_mmd_mat.edge_weight *= mat_data.edge_weight elif mat_data.offset_type == "ADD": diffuse_offset = Vector(base_mmd_mat.diffuse_color) + Vector( mat_data.diffuse_color[0:3]) specular_offset = Vector(base_mmd_mat.specular_color) + Vector( mat_data.specular_color) edge_offset = Vector(base_mmd_mat.edge_color) + Vector( mat_data.edge_color) ambient_offset = Vector(base_mmd_mat.ambient_color) + Vector( mat_data.ambient_color) work_mmd_mat.diffuse_color = list(diffuse_offset) work_mmd_mat.alpha += mat_data.diffuse_color[3] work_mmd_mat.specular_color = list(specular_offset) work_mmd_mat.shininess += mat_data.shininess work_mmd_mat.ambient_color = list(ambient_offset) work_mmd_mat.edge_color = list(edge_offset) work_mmd_mat.edge_weight += mat_data.edge_weight return {'FINISHED'}
def execute(self, context): obj = context.active_object root = mmd_model.Model.findRoot(obj) rig = mmd_model.Model(root) mmd_root = root.mmd_root morph = mmd_root.material_morphs[mmd_root.active_morph] mat_data = morph.data[morph.active_data] meshObj = rig.findMesh(mat_data.related_mesh) if meshObj is None: self.report({'ERROR'}, "The model mesh can't be found") return {'CANCELLED'} try: work_mat_name = mat_data.material + '_temp' work_mat, base_mat = FnMaterial.swap_materials( meshObj, work_mat_name, mat_data.material) except MaterialNotFoundError: self.report({'ERROR'}, "Material not found") return {'CANCELLED'} copy_idx = meshObj.data.materials.find(work_mat.name) base_mmd_mat = base_mat.mmd_material work_mmd_mat = work_mat.mmd_material if mat_data.offset_type == "MULT": try: diffuse_offset = divide_vector_components( work_mmd_mat.diffuse_color, base_mmd_mat.diffuse_color ) + [special_division(work_mmd_mat.alpha, base_mmd_mat.alpha)] specular_offset = divide_vector_components( work_mmd_mat.specular_color, base_mmd_mat.specular_color) edge_offset = divide_vector_components(work_mmd_mat.edge_color, base_mmd_mat.edge_color) mat_data.diffuse_color = diffuse_offset mat_data.specular_color = specular_offset mat_data.shininess = special_division(work_mmd_mat.shininess, base_mmd_mat.shininess) mat_data.ambient_color = divide_vector_components( work_mmd_mat.ambient_color, base_mmd_mat.ambient_color) mat_data.edge_color = edge_offset mat_data.edge_weight = special_division( work_mmd_mat.edge_weight, base_mmd_mat.edge_weight) except DivisionError: mat_data.offset_type = "ADD" # If there is any 0 division we automatically switch it to type ADD except ValueError: self.report({'ERROR'}, 'An unexpected error happened') # We should stop on our tracks and re-raise the exception raise if mat_data.offset_type == "ADD": diffuse_offset = list(work_mmd_mat.diffuse_color - base_mmd_mat.diffuse_color) + [ work_mmd_mat.alpha - base_mmd_mat.alpha ] specular_offset = list(work_mmd_mat.specular_color - base_mmd_mat.specular_color) edge_offset = Vector(work_mmd_mat.edge_color) - Vector( base_mmd_mat.edge_color) mat_data.diffuse_color = diffuse_offset mat_data.specular_color = specular_offset mat_data.shininess = work_mmd_mat.shininess - base_mmd_mat.shininess mat_data.ambient_color = work_mmd_mat.ambient_color - base_mmd_mat.ambient_color mat_data.edge_color = list(edge_offset) mat_data.edge_weight = work_mmd_mat.edge_weight - base_mmd_mat.edge_weight mat = meshObj.data.materials.pop(index=copy_idx) if mat.users < 1: bpy.data.materials.remove(mat) return {'FINISHED'}