Created
November 17, 2022 11:05
-
-
Save aobond2/4985ad23e19dec2c621acba23f8fa61b to your computer and use it in GitHub Desktop.
Blender material change test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
def replace_material(obj, obj_data, mat_src, mat_dst): | |
""" | |
replace a material in blender. | |
""" | |
print("ob is", obj) | |
for i in range(len(obj_data.materials)): | |
# test if the old material is in the slot and replace it | |
if obj_data.materials[i] == mat_src: | |
print("match found", obj.data.materials[i]) | |
obj_data.materials[i] = mat_dst | |
def main(): | |
# get a list of objects selected | |
selected = bpy.context.selected_objects | |
active = bpy.context.active_object | |
currentmat = active.active_material | |
# mat_src = bpy.data.materials['Plaster - Teal'] | |
mat_src = currentmat | |
mat_dst = bpy.data.materials['Glass'] | |
for obj in selected: | |
obj_data = obj.data | |
if obj_data and hasattr(obj_data, "materials"): | |
replace_material(obj, obj_data, mat_src, mat_dst) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment