Skip to content

Instantly share code, notes, and snippets.

@lucasvinbr
Created November 16, 2018 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasvinbr/a18a4837adec9a6767a9ab522980bc1b to your computer and use it in GitHub Desktop.
Save lucasvinbr/a18a4837adec9a6767a9ab522980bc1b to your computer and use it in GitHub Desktop.
Blender: Remap Materials Based on Main Texture Image
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Remap Materials Based on Main Texture Image",
"category": "Material",
"author": "lucasvinbr (lucasvinbr@gmail.com)",
"version": "0.1",
"location": "Material Properties > Remap Materials",
"description": "Replaces references to other materials that have the same base texture image as the selected one",
}
import bpy
from bpy.types import (Operator,
Panel
)
class RemapSimilarMatsOp(Operator):
bl_idname = "material.remap_similar_mats_op"
bl_label = "Remap Similar Materials to Active"
def execute(self, context):
curScene = context.scene
curMat = context.material
mats = bpy.data.materials
replacedMats = 0
if curMat is None:
self.report({"ERROR_INVALID_INPUT"},"No material selected")
elif curMat.texture_slots[0] is None:
self.report({"ERROR_INVALID_CONTEXT"},"Selected material's reference texture slot has no data")
elif curMat.texture_slots[0].texture.image is None:
self.report({"ERROR_INVALID_CONTEXT"},"Selected material's reference texture has no Image linked to it")
else:
refTex = curMat.texture_slots[0].texture.image
for material in mats:
if material == curMat or material.texture_slots[0] is None:
continue
if material.texture_slots[0].texture.image == refTex:
material.user_remap(curMat)
replacedMats += 1
self.report({"INFO"},"Remapped {0} materials".format(replacedMats))
return {'FINISHED'}
class RemapSimilarMatsPanel(Panel):
"""Creates a Panel in the Material properties window"""
bl_label = "Remap Similar Materials By Base Image"
bl_idname = "MATERIAL_PT_remapsimilar"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "material"
@classmethod
def poll(self, context):
return context.active_object.active_material!=None
def draw(self, context):
layout = self.layout
row = layout.row()
row.operator(RemapSimilarMatsOp.bl_idname, RemapSimilarMatsOp.bl_label)
#--------------
#REGISTER\UNREGISTER
#--------------
classes = (
RemapSimilarMatsOp,
RemapSimilarMatsPanel
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment