Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- from bpy.props import BoolProperty
- import sys
- import logging
- argv = sys.argv[sys.argv.index('--') + 1:]
- path_to_save = argv[0]
- def set_scene_properties(scene):
- bpy.types.Scene.Respect = BoolProperty(
- name='Respect Fake User', description='Respect Fake User for all removal actions'
- )
- scene['Respect'] = True
- def erase_unused_materials():
- material_list = []
- scene = bpy.context.scene
- set_scene_properties(scene)
- scenes = bpy.data.scenes
- for i in range(len(scenes)):
- for obj in scenes[i].objects:
- if obj.type == 'MESH' or obj.type == 'SURFACE' \
- or obj.type == 'META' or obj.type == 'CURVE' or obj.type == 'FONT':
- if obj.data.materials == '' or len(obj.material_slots.items()):
- for slot in obj.material_slots:
- material_list.append(slot.material)
- material_list = list(set(material_list)) # duplicates removal
- for material in bpy.data.materials:
- if material not in material_list:
- option = False if scene['Respect'] else True
- if option and material.use_fake_user:
- material.use_fake_user = False
- if not material.use_fake_user:
- logging.warning(material.name)
- material.user_clear()
- bpy.data.materials.remove(material, do_unlink=False)
- # bpy.data.materials.remove(material, do_unlink=True) # to be tested (do_unlink=True)
- def save_as_new(path):
- bpy.ops.wm.save_as_mainfile(filepath=path)
- erase_unused_materials()
- save_as_new(path_to_save)
Advertisement
Add Comment
Please, Sign In to add comment