I tried implementing the automatic deletion of matching nodes. I switched to python since handling lists is just a lot easier in that language and it's just the language I'm more confortable with. I tested the scripts in Maya 2018 so it shouldn't be a problem.
I use a Temporary namespace so the nodes don't get renamed in any way and I can check for matching names, after the matching nodes are deleted the Temp namespace is as well.
ma:
import maya.cmds as mc
path= "E:/WORK/ma.ma"
check= 0
nameSpace= "TempNamespace"
while check ==0:
if mc.namespace(ex= nameSpace)== True:
nameSpace= nameSpace + "1"
else:
check = 1
all = mc.ls()
nn = mc.file(path,i = True ,typ = "mayaAscii", mergeNamespacesOnClash= True, rdn=True, ra= True, rnn= True, ns= nameSpace, options = "mo=1" ,loadReferenceDepth = "none")
for n in nn:
if mc.objExists(n) ==True:
if mc.objectType(n)== "mesh":
mc.select(n, r=True)
mc.polyMergeUV(d=0.01, ch=0)
if "initialShadingGroup" in n:
mc.delete(n)
nn.remove(n)
else:
nn.remove(n)
nd = mc.ls(ud=True)
for n in all:
np = "|{0}:{1}".format(nameSpace,n)
nwp = "{0}:{1}".format(nameSpace,n)
if np in nn or nwp in nn:
if n not in nd:
try:
mc.delete(n)
except:
pass
for n in nn:
if mc.objExists(n) ==False:
nn.remove(n)
mc.namespace(rm=nameSpace,mnr= True)
obj:
import maya.cmds as mc
path= "E:/WORK/obj.obj"
check= 0
nameSpace= "TempNamespace"
while check ==0:
if mc.namespace(ex= nameSpace)== True:
nameSpace= nameSpace + "1"
else:
check = 1
all = mc.ls()
nn = mc.file(path,i = True ,typ = "OBJ", mergeNamespacesOnClash= True, rdn=True, ra= True, rnn= True, ns= nameSpace, options = "mo=1" ,loadReferenceDepth = "none")
for n in nn:
if mc.objExists(n) ==True:
if mc.objectType(n)== "mesh":
mc.select(n, r=True)
mc.polySoftEdge(a=180, ch=0)
if "initialShadingGroup" in n:
mc.delete(n)
nn.remove(n)
else:
nn.remove(n)
for n in nn:
if mc.objExists(n) ==False:
nn.remove(n)
nd = mc.ls(ud=True)
for n in all:
np = "|{0}:{1}".format(nameSpace,n)
nwp = "{0}:{1}".format(nameSpace,n)
if np in nn or nwp in nn:
if n not in nd:
mc.delete(n)
mc.namespace(rm=nameSpace,mnr= True)
I should mention that I had to single out the initialShadingGroup, where the new one is deleted as the one in the scene is not deletable, when deleting the shading group, the assosiated nodes get deleted as well which is why I needed another loop to remove them from the list.
I hope it helps!