Delete duplicate file textures


#1

Hi,

I was wondering if anyone has a script to delete duplicate file textures. I have a scene with 100’s of the same textures in the hypershade. It would be nice to have just 1 for each texture and have the shader re-path to the 1 file texture, instead of multiples of the same texture and shaders.

The delete duplicate shaders just deletes shaders and doesn’t effect textures.

Has anyone got a script for this, I tried looking for a script but could only find

http://forums.cgsociety.org/archive/index.php?t-1059817.html

but this didn’t work correctly.

Thanks!


#2

in real life find and replace file duplicates is not simple thing. you have to check a lot of parameters and input and output connections. but in your probably case the difference is only texture file, and connection to change is outColor.

so the script might be:


import maya.cmds as cmds
import os

## check if paths are equal
def same_path(file1, file2):
    return (os.path.abspath(file1.lower()) == os.path.abspath(file2.lower()))

## check if two files use same textures
def uses_same_file(file1, file2):
    f1 = cmds.getAttr(file1 + ".fileTextureName")
    f2 = cmds.getAttr(file2 + ".fileTextureName")
    return same_path(f1, f2)

## reconnect out color of file1 to destination connection of out color file2
def reconnect_file_outcolor(file1, file2):
    dest = cmds.listConnections(file2 + ".outColor", s=1, p=1)
    if dest:
        curr = cmds.listConnections(file1 + ".outColor", s=1, p=1)
        for d in dest:
            if d not in curr: cmds.connectAttr(file1 + ".outColor", d, force=1)
        return len(dest)

## search all duplicates and return two lists >> targets(to keep) and sources(for delete)
def replace_same_files():
    files = set(cmds.ls(type = "file"))
    targets = []
    sources = []
    while files:
        target = files.pop()
        for source in files:
            if uses_same_file(target, source):
                reconnect_file_outcolor(target, source)
                sources.append(source)
        targets.append(target)
        files = files.difference(sources)
    return targets, sources

replace_same_files()

(not really tested well. let me know if anything works wrong)


#3

Thanks Denis,
I couldn’t get it to work unfortunately. It errored.
I ended up manually doing it.
I think I need to learn some mel!!

Thanks for the help though.


#4

it isn’t Mel, it’s Python. If you run it on a Mel window it won’t run.
And if you want to learn coding in Maya, you should definitely go Python.


#5

If you’re cleaning up a scene imported from something else, watch out–sometimes the duplicated texture nodes are due to different placement node settings, like different UV offsets, which are lightweight in some software but require ugly texture duplication in Maya. If you just combine the texture nodes, you’ll lose this and your textures will break. (You can usually fix this in other ways, like adjusting UVs or using a separate UV set, but it takes more work than just combining texture nodes.)

Also beware that there are bugs in Maya that can trigger if you plug the same texture node into multiple outputs. I don’t recall exactly off-hand, but I think it can happen if you have the same texture node feeding into multiple bump or displacement nodes (maybe specific to mental ray). It’s really annoying–this is really basic stuff and having to duplicate texture nodes is incredibly annoying–but it happens, so watch out for it too.


#6

what you tell scares. could you give an example, please?


#7

That’s all the detail I remember off the top of my head. I recall issues with meshes coming out black in mental ray renders when a texture node was input into multiple nodes, either with bump or displacement (don’t remember which). Just make sure you test render and check all of your textures after merging texture nodes to make sure it doesn’t have any unexpected side-effects.


#8

i got it… merge texture and pray :slight_smile:


#9

Don’t pray, test.