Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

lightlink for plenty of object

Amit_Karny_עמית_קרני
Advocate
Advocate

lightlink for plenty of object

Amit_Karny_עמית_קרני
Advocate
Advocate

Hi there!

I do have pleanty of geometry object in my scene. i'm talking about 200 +/-. I'm looking for simple way to break link insted of :

 

lightlink -break -light pointLight1 -object  pCube1  -object  pCube2  -object  pCube3 and so on....  

 the issue is much more complicatied when some of them grouped into a group the some of that group member supposed to be affected by  pointLight1.. Plz Help. thanks in advance.

0 Likes
Reply
Accepted solutions (1)
336 Views
5 Replies
Replies (5)

Amit_Karny_עמית_קרני
Advocate
Advocate

UP

0 Likes

Kahylan
Advisor
Advisor

Hi!

 

Your question is pretty vague... Like, how do you want to specify which objects get linked and which don't?

 

For example, this is a python script that breaks all the selected objects from a light specified in the variable "light".

import maya.cmds as mc

list = mc.ls(sl = True)
light = "aiAreaLight2"

for o in list:
    mc.lightlink(b = True, object = o, light = light)

But that still requires you to manually select all the objects and specify the light, so I don't see how this would be beneficial compared to just using the lightlinking menu.

 

So how do you want to specify your objects and which lights they need to be broken from? Do you already have a scene with lightlinking set up and need to get that lightlinking to another scene with the same objects?

Is there a naming convention that could be used or attribute tags or sets?

0 Likes

Amit_Karny_עמית_קרני
Advocate
Advocate

Hi there!
Thanks for your reply...

 

Umm..actually I do have the deafault light set in my outliner. and made some selection sets in my reference file whice is roamed with the rest of my object in my scene/file. i wish i could use the set's name as a string after the object flag for example. and yes, the procedure is the same - meaning i do have to link and break the same object-light affecting in each and every scene/file.

 

Hope I made myself much more accurate and clear now. thanks alot.

0 Likes

Kahylan
Advisor
Advisor
Accepted solution

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!

0 Likes

Amit_Karny_עמית_קרני
Advocate
Advocate

Thank you very much!!

0 Likes