Thats alot clearer now, thanks!
If you have all the objects in a set, you can just get them with the sets command in query mode and then run them through a loop that breaks their links.
This is a Python version of how you would do this:
import maya.cmds as mc
def breakLinkOnSetMembers(set = "", light = ""):
objects = mc.sets(set, q= True)
for obj in objects:
mc.lightlink(b = True, object = obj, light = light)
breakLinkOnSetMembers(set = "Name_Of_Your_Set", light = "Name_Of_The_Light")
Now to the issue of having to do the same in every scene/file.
If you have a lot of identical/very similar scenes that need the same lightlinking, the best way to approach it is probably to set up the lightlinking in one scene and then use an Export/Import Script to copy the lightlinking over.
It's actually pretty simple to check which objects are not affected by which lights, because you have a "lightLinker" object in your scene, this object stores the connections in "ignored" attributes, which have a pair of leaf attriubtes called "objectIgnored" and "lightIgnored", those attributes have a message connection to the light/object they referre to. Each of those attribute pairs stands for a broken lightlink. They can be found using the listConnections() command and once you have them stored in a list or array you can just export them to a text file and import that textfile in other scenes in a way that it restores the connections there as well.
Here is a python example of how you could do this, I chose a JSON filetype for the export because it is a really lightweight but still human friendly format. It's not a script that I use often, so it's not very optimized and doesn't catch all possible problems with custom Errors. But it should work if the light setup doesn't have multiple lightlinker objects and if the names in the scenes are identical.
import json
import maya.cmds as mc
class exportLightlinking():
def __init__(self):
"""
init function that loads up UI
"""
#UI proportions and naming
self.path = ''
self.window = "exportLightLinkToJSON"
self.title = "Export/Import Lightlink to JSON"
self.size = (30, 90)
self.fields = []
# close old window is open
if mc.window(self.window, exists = True):
mc.deleteUI(self.window, window=True)
#main layout
self.window = mc.window(self.window, title=self.title)
mainLayout = mc.rowColumnLayout(nc = 1)
#layout to browse for filepath
loadLayout = mc.rowLayout( numberOfColumns=3, columnAttach=(1, 'right', 10), columnWidth=[(1,143),(2,245),(3,20)], parent = mainLayout )
mc.text(label = "JSON File")
mc.textField("pathTextField",tx = self.path,width = 240 )
mc.button(label="Browse", command=self.browse)
#Buttons to trigger export and import
mc.separator(height=40, parent = mainLayout)
self.createExportBTN = mc.button(label='Export Lightlinking',command= self.executeExport, parent = mainLayout)
self.createImportBTN = mc.button(label='Import Lightlinking',command= self.executeImport, parent = mainLayout)
mc.showWindow()
def browse(self, *args):
"""
opens file dialoge to browse for save file
"""
path = mc.fileDialog2()[0]
self.storeFileName(path = path)
def storeFileName(self,path = None):
"""
store fileName to textfield
"""
mc.textField("pathTextField", edit = True, tx = path)
def getLinkedObjects(self):
"""
function that reads out connections on ignored attributes on lightlinker
"""
#get lightlinker
linker = mc.ls(type = "lightLinker")[0]
conn = mc.listConnections(linker, c = True, sh = True)
#checking if connection is on ignored Attribute and storing ignored attributes to list
ignoredList = []
for c in range(0,len(conn)):
try:
if "lightIgnored" in conn[c+2] and "objectIgnored" in conn[c]:
ignoredList.append([conn[c+1],conn[c+3]])
except IndexError:
break
return ignoredList
def writeLinkedObjects(self, data = None):
"""
function that writes the JSON file
"""
path = mc.textField("pathTextField", q = True, text = 1)
json.dump(data, open(path, 'w'))
def readLinkedObjects(self):
"""
function that gets data from specified JSON file
"""
path = mc.textField("pathTextField", q = True, text = 1)
with open(path) as f:
objToLink = json.load(f)
return objToLink
def executeExport(self, *args):
"""
function manage export
"""
objs = self.getLinkedObjects()
self.writeLinkedObjects(data = objs)
def executeImport(self, *args):
"""
function to manage import
"""
#get lightLinker object
linker = mc.ls(type = "lightLinker")[0]
objToLink = self.readLinkedObjects()
#connect the list of objects
index = 0
for o in objToLink:
execute = False
#check for free ignore plugs on lightLinker
while execute == False:
if mc.listConnections("{0}.ignore[{1}].lightIgnored".format(linker, index))== None and mc.listConnections("{0}.ignore[{1}].objectIgnored".format(linker, index))== None:
execute = True
else:
index = index +1
#create connections
if mc.objExists(o[0]) == True and mc.objExists(o[1]) == True:
mc.connectAttr("{0}.message".format(o[1]), "{0}.ignore[{1}].lightIgnored".format(linker,index))
mc.connectAttr("{0}.message".format(o[0]), "{0}.ignore[{1}].objectIgnored".format(linker,index))
index = index + 1
exportLightlinking()
I hope this helps!