Create a “light from view” script with python in maya

In this tutorial we will write a simple script to create lights from the camera’s point of view. If you never used python or mel before dont worry because it’s really easy to understand and having some knowledge about this topic can be really helpfull in you carreer, or even if you are just setting your own home pipeline. Beeing able to automate tasks and add new features combining the maya native functionality with your own aproach is a great skill to have in yout toolbox.

Final result

light_from_view_script

Import maya.cmds and define your function

import maya.cmds as cmds

def lightFromView():
    #your code here

Start by opening the script editor and create a new python tab. The first thing that we have to do is import maya native commands, you will be doing this all the time, so you will memorize it easily. We also give it the alias “cmds” so it’s easy to reference, the alias “cmds” is recommended but you can give it your own name, like “mc”.

Next we will define our function, the syntax is pretty simple, again, this is how you will always define a function, in this case we dont need any arguments between the “( )”. The lines starting with an “#” are just comments of your code.

import maya.cmds as cmds

def lightFromView():
    #select the modelEditor camera
    selection = cmds.ls('persp')

We need a way to get the camera’s position, so we start by selecting the default camera “persp”. This is not the best aproach, we should get the modelPanel (viewport) in focus and query the current camera, but for now this will work just fine. So we use the “ls” command , and we will store the camera in a variable “selection”.

import maya.cmds as cmds

def lightFromView():
    #select the modelEditor camera
    selection = cmds.ls('persp')

    #Get the transform values
    translation = cmds.xform(selection, query=1, ws=1, rp=1)
    rotation = cmds.xform(selection, query=1, ws=1, ro=1)

Now we store the translation and rotation values of the camera to later apply those to the light. We are using the xform command which is used to get the the transform attributtes of objects in maya. we can use xform to position certain objects in other’s position, but we can also ask(query) an object position/rotation, using the query flag. In your maya main menu, go to the help and select “python command reference”, there you can check all the commands and flags, and even get examples. The “query=1″ means “query = True, just like in any other flag 1 means true and 0 means false.

If it’s your first time using python or scripting at all, if you feel a bit confused dont worry, this will always be very similar. You will eventually write these commands without thinking about it.

def lightFromView():
    #select the modelEditor camera
    selection = cmds.ls('persp')

    #Get the transform values
    translation = cmds.xform(selection, query=1, ws=1, rp=1)
    rotation = cmds.xform(selection, query=1, ws=1, ro=1)

    #Create aiAreaLight
    myLight = mtoa.utils.createLocator('aiAreaLight', asLight=1)

In this case I will be creating an arnold are light, which is what I use most of the time. For that you will have to call a specific MtoA command, to found about this I just looked at mtoa scripts folder. If you are using maya area lights just go to the python command reference and search the command for light creation.

def lightFromView():
    #select the modelEditor camera
    selection = cmds.ls('persp')

    #Get the transform values
    translation = cmds.xform(selection, query=1, ws=1, rp=1)
    rotation = cmds.xform(selection, query=1, ws=1, ro=1)

    #Create aiAreaLight
    #myLight # Result: (u'aiAreaLightShape1', u'aiAreaLight1') #
    myLight = mtoa.utils.createLocator('aiAreaLight', asLight=1)

    #Apply transforms to the light
    cmds.xform( myLight[1], r=1, ro=rotation, t=translation, s=(30,30,30))

Now we need to apply the transforms to our light using the xform command, passing the light’s transform which is “myLight[1]“. the second index, the first [0] is the light shape as you can see in the comment area.

Then we use the relative flag (r) and we pass the rotation and translation variables with the current camera values. Also we can define a scale here, I used 30 as an example.

def lightFromView():
    #select the modelEditor camera
    selection = cmds.ls('persp')

    #Get the transform values
    translation = cmds.xform(selection, query=1, ws=1, rp=1)
    rotation = cmds.xform(selection, query=1, ws=1, ro=1)

    #Create aiAreaLight
    #myLight # Result: (u'aiAreaLightShape1', u'aiAreaLight1') #
    myLight = mtoa.utils.createLocator('aiAreaLight', asLight=1)

    #Apply transforms to the light
    cmds.xform( myLight[1], r=1, ro=rotation, t=translation, s=(30,30,30))

    #Set attributtes to the light
    cmds.setAttr(myLight[0] + ".aiTranslator" , 'quad', type="string")
    cmds.setAttr(myLight[0] + ".intensity" , 300)
    cmds.setAttr(myLight[0] + ".aiSamples" , 2)

Finally we can set some attributtes in our light, in my case I want to create a quad light by default, increase the intensity  to 300 and the samples to 2. Choose your own values.

And we’re done with this script, now we just need to call the function by executing “lightFromView()” in the command line or script editor. You can drag this code to your shelf and use it when you need. Experiment your script by tumble around in your viewport and execute the function command, if you dont see the light just dolly the camera back and it’s there.

I will be posting in future posts some examples of python scripts that you can create to improve your workflow in maya. Have fun creating your own scripts.