Quick script for nAngus – this will import the material library from a referenced model and change the object assignments from the referenced to the local. Saves importing the refModel completely.

'''
localiseMatLib

Usage: select a referenced matLib. A (local) duplicate will be created and 
objects assigned to these new materials

'''

app = Application

print '##########################'
shaderColl = {}
sel = app.Selection
for x in sel:
	if x.Type == 'library_source':
		# is locked by refmodel?
		if x.lockType == 3:
			matLibName = x.FullName
			print 'Matlib:', matLibName
			for mem in x.items:
				print mem.name
				usedBy = mem.UsedBy
				
				itemList = []
				for obj in usedBy:
					itemList.append(obj.fullName)

				shaderColl[str(mem.name)] = itemList
			print 'shaderColl', shaderColl
			
			# copy the material library
			newMatLib = app.Duplicate(x)
			print newMatLib
			
			# assign objects to the new matlib
			for mem in x.items:
				objectList = shaderColl[str(mem.name)]
				newMatName = mem.FullName.replace(matLibName, str(newMatLib))
				print 'newMatName', newMatName
				for obj in objectList:
					print newMatName, obj
					Application.SIAssignMaterial( obj, newMatName)
		else:
			print 'Pick a matlib that is referenced'