11
$\begingroup$

Blender 2.68a, Python 3.3

I would like to unlink material from a mesh, and even after googling a hour I don't know how to do it. I only saw scripts that work in Blender 2.5x only for this.

So could someone tell me (or link info), how to do this:

enter image description here

with python script?

$\endgroup$
16
$\begingroup$

Unlink a Material

Remove the first material from the mesh.

import bpy
obj = bpy.context.object
obj.data.materials.pop(0, update_data=True)

Remove all materials.

import bpy
obj = bpy.context.object
obj.data.materials.clear()

Unlink Texface

This clears images for each face (active uv layer only)

import bpy
obj = bpy.context.object
for tf in obj.data.uv_textures.active.data:
    tf.image = None

This clears images for all UV layers

import bpy
obj = bpy.context.object
for texlay in obj.data.uv_textures:
    for tf in texlay.data:
        tf.image = None

Update: TexFace has been removed in Blender2.8

$\endgroup$
  • 1
    $\begingroup$ Trying to avoid comments, like "thanks"... LOL but thanks, I would never thought it is the "pop" I was looking for. Will accept in 2 mins, and vote up when I get 15 repu. $\endgroup$ – Zéiksz Aug 13 '13 at 9:02
  • 1
    $\begingroup$ Ideally we would be able to do: del obdata.materials[0], Which would be the most obvious way to do this, but this way doesn't accept arguments for how to handle linked data. $\endgroup$ – ideasman42 Aug 17 '13 at 12:07
  • $\begingroup$ Is there something similar for textures? $\endgroup$ – McLawrence Aug 13 '18 at 15:27
  • $\begingroup$ @McLawrence best ask a new question, link to this one. $\endgroup$ – ideasman42 Aug 13 '18 at 23:36
0
$\begingroup$

You can also do the following to properly remove materials. This will both unlink the materials and also remove the unlinked materials from memory. ideasman42's original solution does not remove the left-over data blocks and can cause memory leakage issues unless you close/re-open Blender or exit the current Python environment (themultiprocessing package can help with the latter). Here's a modified version of the solution that ideasman42 proposed that properly removes materials:

import bpy
obj = bpy.context.scene.objects.active
mats = obj.data.materials

for mat in mats:
    mat.user_clear()

matLength = len(mats)
for i in range(matLength):
    obj.data.materials.pop(0, update_data=True)

leftOverMatBlocks = [block for block in bpy.data.materials if block.users == 0]
for block in leftOverMatBlocks:
    bpy.data.materials.remove(block)
bpy.context.scene.update()

mats.clear() # I'd recommend not executing this line. It might sometimes cause the object not be shown in final rendering
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.