Griffin Bajor

New Page

 

 Update Lighting Group

Used during the development of Miles to update our environment lighting with a new set of lights. Saved us from a roughly 20 minute manual process for every single shot of the film.

 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
Update Lighting Group

Traverse through each lighting shot and replace the old sun group with the updated version

"""
import maya.cmds as mc
import os

# Initialize file path
PATH_TO_SCENES = ''


#Function to browse for shots
def browse_files(browse_title, *args):
    directory = mc.fileDialog2(cap=browse_title, fm=3)
    directory = directory[0] + '/'
    global PATH_TO_SCENES
    PATH_TO_SCENES = directory

def updateLightGroupUI():
    # Initialize UI
    title = 'Update Sun Group'

    if mc.window('updateLightWindow', exists=True):
        mc.deleteUI('updateLightWindow')

    mc.window('updateLightWindow', title=title, wh=(512, 230))


# Design GUI

    cl = mc.columnLayout(adjustableColumn=True, cal='center', cat=('both', 50), rs=10)

    test = mc.button('path', label='Directory of lighting shots', c='browse_files("Browse for lighting shot directory")')

    textInput1 = mc.textFieldGrp(label='Name of sun scene', text='SunGroupUpdate.ma')
    NEW_SUN_SCENE = mc.textFieldGrp(textInput1, query=True, text=True)

    textInput2 = mc.textFieldGrp(label='Original sungroup', text='Sun_GRP')
    OLD_SUN_NAME = mc.textFieldGrp(textInput2, query=True, text=True)

    textInput3 = mc.textFieldGrp(label='Updated sun group', text='Sun_02_GRP')
    NEW_SUN_GROUP = mc.textFieldGrp(textInput3, query=True, text=True)

    textInput4 = mc.textFieldGrp(label='Master group', text='Lighting_GRP')
    MASTER_GRP = mc.textFieldGrp(textInput4, query=True, text=True)

    textInput5 = mc.textFieldGrp(label='Lighting scene prefix', text='Lighting_')
    SHOT_PREFIX = mc.textFieldGrp(textInput5, query=True, text=True)

    rl = mc.rowLayout(numberOfColumns=2,adjustableColumn=True)
    mc.button(label='Run', command='update_all_shots(PATH_TO_SCENES, SHOT_PREFIX)', align='left')
    mc.button(label='Close', command='mc.deleteUI(%s, wnd=True)' % '"updateLightWindow"', align='right')

    mc.showWindow()


# Define variables

# Define functions

# Function to to complete sun replacement operation
def replace_sun(path, new_sun_scene, new_sun_group, old_sun_group, master_group):
    """
    Runs a series of commands to replace a sun group with an updated version from an imported file

    Parameters:
        <string>path				path to directory
        <string>new_sun_group		name of new imported sun group
        <string>old_sun_group		name of old sun group
        <string>master_group		name of parent of sun group
    """

    # Import new sun group
    mc.file(os.path.join(path + new_sun_scene), i=True)
    print 'Imported %s' % new_sun_scene

    # Match transforms
    mc.matchTransform([new_sun_group, old_sun_group], pos=True, rot=True)
    print 'Rotated new sun to match'

    # Delete old sun group
    mc.delete(old_sun_group)
    print 'Deleted old sun group'

    # Parent new group to environment light group
    mc.parent(new_sun_group, master_group)
    print 'Parented new sun group to master lighting'

    # Rename new lighting group back to original name
    mc.rename(new_sun_group, old_sun_group)
    print 'Renamed %s to %s' % (new_sun_group, old_sun_group)


def update_all_shots(path, prefix):

    """
    Runs operation function on all lighting files in a given directory

    Parameters:
        <string>path		Path to directory to process
        <string>prefix		Prefix of all lighting files to process
    """

    shot_scenes = []

    # Build list of shot files
    for x in os.listdir(path):
        if x.startswith(prefix):
            shot_scenes.append(x)
    print 'Operating on %s' % shot_scenes

    for i in shot_scenes:
        # Open each file
        print '##########################'
        print 'Working on %s' % i
        # file_path = os.path.join(path, i)
        mc.file(os.path.join(path, i), o=True, f=True)

        # Run operation and check for error
        try:
            replace_sun(PATH_TO_SCENES, NEW_SUN_SCENE, NEW_SUN_GROUP, OLD_SUN_NAME, MASTER_GRP)
        except:
            print '#### ERROR with %s - Unable to complete operation' % i

        # Save and check for error
        try:
            mc.file(s=True)

        except:
            print '#### ERROR with %s - Unable to save, may be set to read only' % i

        print '%s complete' % i

    print '##########################'
    print '##########################'
    print 'All Operations Complete'
    print '##########################'
    print '##########################'

def updateLightGroup():
    updateLightGroupUI()