Sunday 17 July 2016

Object Planter






I did some work as an environment artist on this anime movie. However, my main contribution is in writing the tools to create the flashing light bulbs such as in the screen shot. There are thousands of these light bulbs. This movie takes place in a casino themed ship, complete with towns and castles and stuff in the ship, and the light bulbs are everywhere, flashing Vegas style. There were just three in house environment modelers on this job. The lead, me, and another person. Most of the models were done by outsource vendors but the light bulbs had to be done in house.

3 person on this task would be crazy, so I wrote a couple of tools to help manage this. The first was a script which I will call the Object Planter. The task was to stick all these lightbulbs onto all these strips and surfaces you can see in the screen shot. This part is relatively easy. Take the surfaces, divide them evenly, then find the co-ordinates of the face centers of all the faces on the strip you want to plant the lightbulbs on. This can be done by accessing maya API. 

The really tricky part is not this bit at all however. As you might note in the trailer, these light bulb flash in sequence. So in order to do this, the light bulbs themselves had to be put into a logical sequence in the list before this can happen. Here's the problem. The script will generate the light bulbs based on the faces of the target geo, and those faces gets put into a loop list based on their face index. However, during the process of modeling those face index gets messed up. objectFace;1 could be sitting next to objectFace:329 and objectFace:2 can be sitting way over there.  When Maya reads the list however, it will still read in the ordered list. So lightbulb 1 will flash, and then light bulb 2 will flash way over there, and 327 times later, the light bulb besides 1 will flash.

To solve this I had to tell Maya to start from somewhere, and then instead of going through the face list, to instead convert the face it is currently looking at into edges. Then to compare the edges, and see which other face in the list shares the same edge number. If a face also has 1 matching edge, then that should be appended into a new list as the number 2. As can be expected, this can take a while.

This section is the code for planting the objects onto the surface of another geometry:


Object Planter Tool:
----------------------------
import numpy as np
import maya.cmds as cmds
import maya.mel as mel
import maya.OpenMaya as OpenMaya
import math
import re
from string import Template, zfill
from functools import partial

FACE_CENTER_DICT={}
ORDERED_LIST = []
class SelectFaces():

    def __init__(self, geometry, start_face):
        ORDERED_LIST[:] = []
        self.start_face = start_face
        self.reorder_face_list(geometry)       

    def get_edge_list(self, face):
        cmds.select(cmds.polyListComponentConversion( face, ff=True, te=True ))
        edge_list = cmds.ls(sl=True, fl = True)
        
        return edge_list
        
    def reorder_face_list(self, geometry):
        # convienience function to convert current geometry selection to list of faces
        cmds.select(str(geometry)+'.f[*]') # command to select all faces of selected geometry
        self.face_list = cmds.ls(sl=True, fl=True) # turns selected faces into a list
        self.working_list = []
        #self.ordered_list = []
        self.edge_list = []        
        
        starting_face = self.start_face
        ORDERED_LIST.append(starting_face)
        self.edge_list = self.get_edge_list(ORDERED_LIST[-1])
        count = 0

        for dummy_face in self.face_list:
            if dummy_face != starting_face:
                self.working_list.append(dummy_face)
                
        for dummy_count in range(len(self.working_list)):
            if len(ORDERED_LIST) <= len(self.working_list):
                for dummy_var in self.working_list:
                    self.edge_list = self.get_edge_list(ORDERED_LIST[-1])
                    dummy_var_edge = self.get_edge_list(dummy_var)
                    count += 1
                    for edge in dummy_var_edge:
                        if edge in self.edge_list:
                            if dummy_var not in ORDERED_LIST:
                                ORDERED_LIST.append(dummy_var)
                            else:
                                pass
                        else:
                            pass

        
class Face_Center:
    def __init__(self):
        # This finds the face center of each face and uploads data to the global dictionary
        faceCenter = []
        cmds.select(ORDERED_LIST)
        selection = OpenMaya.MSelectionList()
        OpenMaya.MGlobal.getActiveSelectionList(selection)

        iter = OpenMaya.MItSelectionList (selection, OpenMaya.MFn.kMeshPolygonComponent)

        while not iter.isDone():
            dagPath = OpenMaya.MDagPath()
            component = OpenMaya.MObject()

            iter.getDagPath(dagPath, component)

            polyIter = OpenMaya.MItMeshPolygon(dagPath, component)

            while not polyIter.isDone():
                # enumerates the faces in selection
                i = 0
                i = polyIter.index()
                faceInfo = ("face [%s]" %i)
                
                # finds the face center of enumerated face                
                center = OpenMaya.MPoint
                center = polyIter.center(OpenMaya.MSpace.kWorld)
                point = [0.0,0.0,0.0]
                point[0] = center.x
                point[1] = center.y
                point[2] = center.z
                faceCenter = point
                
                # uploads face to global dictionary
                FACE_CENTER_DICT.update({faceInfo:faceCenter})
                
                #goes to next face
                polyIter.next()
                
            iter.next()
                   
class Find_Interger_In_String:
    
        def tryint(self, string):
            try:
                return int(string)
            except:
                return string
        def numeric_key(self, string):
            #import re
            return [self.tryint(string_bits) for string_bits in re.split("([0-9]+)", string)]        
        
class Mass_Planter:
    
    def __init__(self, radius, subdivision_list, item_list):
        self.radius = radius 
        self.subdivision_list = subdivision_list
        self.item_list = item_list
        self.dummy_list = []
        self.dummy_grp=cmds.group( em=True, name=str(self.item_list[0])+'_objCopy_grp' )    
        self.tryint = Find_Interger_In_String()
        SelectFaces(self.item_list[0], self.item_list[1])
        Face_Center()
       
        for dummy_index, dummy_item in enumerate(ORDERED_LIST):            
            for dummy_key, dummy_val in FACE_CENTER_DICT.iteritems():
                string_list = self.tryint.numeric_key(dummy_item)
                '''
                the geometry selected MUST have a number behind it AND ONLY THAT, or the string slicing will pick up the wrong
                list index and cause error
                '''
                face_num_var = '[' + str(string_list[3]) + ']'
                if face_num_var in dummy_key:
                    dummy_sphere=cmds.polySphere(n=str(self.item_list[0]) + '_pika_1',  sx=self.subdivision_list[0], sy=self.subdivision_list[1], r=self.radius)
                    cmds.setAttr( str(dummy_sphere[0])+'.translateX', dummy_val[0] )
                    cmds.setAttr( str(dummy_sphere[0])+'.translateY', dummy_val[1] )
                    cmds.setAttr( str(dummy_sphere[0])+'.translateZ', dummy_val[2] )
                    dummy_constr = cmds.normalConstraint(dummy_item, str(dummy_sphere[0]), 
                                        aimVector = (0,1,0), u = (0,1,0), worldUpType= 0, wu = (0, 1, 0))                    
                    cmds.delete(dummy_constr)
                    self.dummy_list.append(dummy_sphere)                    
        for dummy_geo in self.dummy_list:
            cmds.parent( dummy_geo, str(self.dummy_grp) )
        
        FACE_CENTER_DICT.clear()       

                        
class Mass_Planter_Legacy:
    
    def __init__(self, radius, subdivision_list, geo_sel):
        self.radius = radius 
        self.subdivision_list = subdivision_list
        self.geo_sel = geo_sel
        self.dummy_list = []    
        self.tryint = Find_Interger_In_String()

                        
        for dummy_item in geo_sel:
            ORDERED_LIST[:] = []
            cmds.select(str(dummy_item)+'.f[*]')
            sel_list = cmds.ls(sl = True, fl = True)
            for dummy_var in sel_list:
                ORDERED_LIST.append(dummy_var)
            Face_Center()
            dummy_list = []
            dummy_grp=cmds.group( em=True, name=str(dummy_item)+'_objCopy_grp' )            
            for key, val in FACE_CENTER_DICT.iteritems():
                dummy_sphere=cmds.polySphere(sx=self.subdivision_list[0], sy=self.subdivision_list[1], r=self.radius)
                cmds.setAttr( str(dummy_sphere[0])+'.translateX', val[0] )
                cmds.setAttr( str(dummy_sphere[0])+'.translateY', val[1] )
                cmds.setAttr( str(dummy_sphere[0])+'.translateZ', val[2] )
                dummy_constr = cmds.normalConstraint(dummy_item, str(dummy_sphere[0]), aimVector = (0,1,0), u = (0,1,0), worldUpType= 0, wu = (0, 1, 0))
                cmds.delete(dummy_constr)
                dummy_list.append(dummy_sphere)
            for dummy_geo in dummy_list:
                cmds.parent( dummy_geo, str(dummy_grp) )                
            FACE_CENTER_DICT.clear()
  


class Mass_Planter_UI:
    def __init__(self, *args):
        window = cmds.window( title="PikaPikaTamaChu", iconName='PiAdj', widthHeight=(300, 360) )
        cmds.columnLayout( adjustableColumn=True, rowSpacing=10)
        cmds.separator( style='single' )
        cmds.text( label='1) Select faces of geometries. \n2) Set sphere settings. \n3) Run.', align='left' )
        cmds.separator( style='single' )
        self.obj_radius_value = cmds.floatSliderGrp( field=True, label='Radius',
                 minValue=0.0, maxValue=1000.0, fieldMinValue=1, fieldMaxValue=1000, value=1 )
        cmds.button( label='SetRadius', command=partial(self.print_sphere_radius, 1) )
        self.obj_axis_value = cmds.intSliderGrp( field=True, label='Subdivisions Axis',
                 minValue=1, maxValue=1000, fieldMinValue=1, fieldMaxValue=1000, value=1 )
        self.obj_height_value = cmds.intSliderGrp( field=True, label='Subdivisions Height',
                 minValue=1, maxValue=1000, fieldMinValue=1, fieldMaxValue=1000, value=1 )
        cmds.button( label='Set Subdivision', command=partial(self.print_sphere_subdivision, 1) )
        cmds.separator( style='single' )  
        cmds.button( label='Run', command=partial(self.run_command, 1) )
        cmds.separator( style='single' )    
        cmds.button( label='Legacy Run', command=partial(self.legacy_run_command, 1) )        
        cmds.setParent( '..' )
        cmds.showWindow( window )
        
    def set_root_obj(self, *args):
        root_obj = cmds.textFieldGrp(self.root_obj_name, query=True, text=True)
        start_point = cmds.textFieldGrp(self.start_face, query=True, text=True)
        item_list = [root_obj, start_point]
        return item_list
    
    def sphere_radius(self, *args):
        radius = cmds.floatSliderGrp(self.obj_radius_value, q=True, v=True)
        return radius
        
    def sphere_subdivision(self, *args):
        subdivision_setting = [cmds.intSliderGrp(self.obj_axis_value, q=True, v=True), 
                cmds.intSliderGrp(self.obj_height_value, q=True, v=True)]
        return subdivision_setting
    
    def print_root_obj(self, *args):
        print_list = self.set_root_obj()
        print 'selected asset is ' + str(print_list[0]) + '\n' + 'start point is ' + str(print_list[1]) 
    
    def print_sphere_radius(self, *args):
        print 'sphere radius set at '+ str(self.sphere_radius())
        
    def print_sphere_subdivision(self, *args):
        subdivision_setting = self.sphere_subdivision()
        print 'subD Axis set at ' + str(subdivision_setting[0]) + ' | subD Height set at ' + str(subdivision_setting[1])
    
    
    def run_command(self, *args):
        face_list=cmds.ls(sl=True, fl=True)
        item_list = list()        
        for face in face_list:
            slice_point = face.find('.')
            geo_name = face[:slice_point]
            runtime_list = [geo_name, face]            
            Mass_Planter(self.sphere_radius(), self.sphere_subdivision(), runtime_list)
        
    def legacy_run_command(self, *args):
        obj_sel = cmds.ls(sl = True)
        Mass_Planter_Legacy(self.sphere_radius(), self.sphere_subdivision(), obj_sel)

        
                  
def main():

        Mass_Planter_UI()

if __name__=='__main__':
main()


----------------------------



The next script tool is just something that allows the artist to quickly sort through the generated lightbulbs and order them in whatever multiples they need to.

Select in Multiple Tool:
----------------------------
import maya.cmds as cmds
import maya.mel as mel
import maya.OpenMaya as OpenMaya
import math, re
import os, string, shutil
from os import path, listdir
from string import Template, zfill
from functools import partial
from operator import itemgetter
from itertools import groupby

NEXT_LIST = []
ATTR_DICT = {'tX':'translateX', 'tY':'translateY', 'tZ':'translateZ', 'rX':'rotateX', 'rY':'rotateY', 'rZ':'rotateZ', 'sX':'scaleX', 'sY':'scaleY', 'sZ':'scaleZ'}

class Init_Run:
    
    def __init__(self, geo_group):
        self.set_name = str(geo_group)
        
    def next_run(self, new_modulo):
        global NEXT_LIST
        self.new_modulo = int(new_modulo)
        self.filtered_list = []
        self.shape_list = cmds.listRelatives(self.set_name, c=True)
        for dummy_index, dummy_item in enumerate(self.shape_list):         
            if (dummy_index+1) % self.new_modulo == 0:
                self.filtered_list.append(dummy_item)
        
        cmds.select(self.filtered_list)
        #cmds.sets(n=self.set_name + '_by_' + str(self.new_modulo))

class Vray_Obj_Id:
    def __init__(self, geo_list, id_num):
        self.geo_list = geo_list
        self.id_num = id_num #cmds.intSliderGrp(self.obj_id_value, q=True, v=True)
        self.node_list = cmds.listRelatives(self.geo_list, c=True)
        print self.node_list
        
        if cmds.objExists(str(self.node_list[0]) + ".vrayObjectID"):
            pass           
        else:
            cmds.vray("addAttributesFromGroup", self.node_list[0], "vray_objectID", 1)
        
        cmds.setAttr((str(self.node_list[0])+".vrayObjectID"), self.id_num)
        

class Reorder_Selection:
    def tryint(self, string):
        try:
            return int(string)
        except:
            return string
        
    def numeric_key(self, string):
        import re
        return [self.tryint(string_bits) for string_bits in re.split("([0-9]+)", string)]
        
    def natsort(self, input_list):
        input_list.sort(key=self.numeric_key) 

##Essential classes for setting transform attributes (to be used here for replacing duplicates with instances)
class GetAttribute:
    
    def __init__(self, obj, objAttr):
        self.obj = obj
        self.objAttr = objAttr
        
    def run_getAttr_cmd(self):
        attrValue = cmds.getAttr(str(str(self.obj)+"."+str(self.objAttr)))
        return attrValue

class SetAttribute:
    def __init__(self, rootObj, objAttr, attrValue):
        self.rootObj = rootObj
        self.objAttr = objAttr
        self.attrValue = attrValue
        
    def setAttrCmd(self):
        cmds.setAttr(str(str(self.rootObj)+"."+str(self.objAttr)), self.attrValue)
##Essential classes for setting transform attributes (to be used here for replacing duplicates with instances)

class Instance_Replace:
    def __init__(self, geo_list, geo_type_name):
        self.geo_list = geo_list
        self.geo_type_name = geo_type_name
        instance_root = cmds.instance(self.geo_list[0], n = self.geo_type_name+str(self.geo_list[0]))

        for dummy_geo in self.geo_list:
            if dummy_geo == self.geo_list[0]:
                cmds.delete(dummy_geo)
            else:
                new_instance = cmds.instance(instance_root, n = self.geo_type_name + str(dummy_geo))
                translation_matrix = cmds.xform(dummy_geo, ws=True, q=True, a=True, t=True)
                rotation_matrix = cmds.xform(dummy_geo, ws=True, q=True, a=True, ro=True)
                scale_matrix = cmds.xform(dummy_geo, ws=True, q=True, a=True, s=True)
                self.query = cmds.listRelatives(new_instance, allParents=True)
                cmds.parent( self.query[0] + '|' + new_instance[0], world=True )
                cmds.xform( new_instance, a=True, t=(translation_matrix[0], translation_matrix[1], translation_matrix[2]) )
                cmds.xform( new_instance, a=True, ro=(rotation_matrix[0], rotation_matrix[1], rotation_matrix[2]) )
                cmds.xform( new_instance, a=True, s=(scale_matrix[0], scale_matrix[1], scale_matrix[2]) )                
                
                '''
                for dummy_key, dummy_val in ATTR_DICT.iteritems():
                    transform = GetAttribute(dummy_geo, dummy_val)
                    transform_value = transform.run_getAttr_cmd() 
                    set_transform = SetAttribute(new_instance[0], dummy_val, transform_value)
                    set_transform.setAttrCmd()
                '''
                cmds.delete(dummy_geo)


class Shader_Setup:
    def __init__(self, id_num):
        #self.group_name = group_name
        #self.geo_list = cmds.listRelatives(self.group_name, c=True)
        self.id_num = str(id_num)
        #slice_poin t= new_geo_list[0].find('_pika')
        #shader_root = new_geo_list[0][:slice_point]
        shader_name = 'ObjID_' + self.id_num             
        shader=cmds.shadingNode("surfaceShader",asShader=True, name=shader_name+'_lightShader' )
        shading_group = cmds.sets(renderable=True,noSurfaceShader=True,empty=True, name=str(shader)+'SG')
        cmds.connectAttr('%s.outColor' %shader ,'%s.surfaceShader' %shading_group)
        cmds.setAttr(str(shader)+"."+"outColor", 0.0, 0.0, 0.0, type="double3" )
                

class Select_By_Multiple_UI:
    def __init__(self, *args):
        self.selection_group_list = list()
        window = cmds.window( title="Select_By_Multiple", iconName='SlMul', mnb = True, mxb = False, sizeable = False )
        cmds.columnLayout( adjustableColumn=True, rowSpacing=10, w = 280)
        cmds.frameLayout( label='Select Groups', borderStyle='in', cll=True )
        cmds.columnLayout(adjustableColumn=True, rowSpacing=10, w = 280)
        cmds.button( label='Set Master Group', command=partial(self.master_list, 1) )
        cmds.separator( style='single' ) 
        cmds.setParent( '..' )
        cmds.setParent( '..' )
        
        cmds.frameLayout( label='Select Multiples', borderStyle='in', cll=True )
        cmds.columnLayout(adjustableColumn=True, rowSpacing=10, w = 280)
        self.new_modulo_val_set = cmds.textFieldGrp( label = 'Next Multiples Of?' )
        cmds.button( label='Select Objects', command=partial(self.run_command_second, 1) )  
        #cmds.button( label='Set Up Groups', command=partial(self.group_set_up, 1) )     
        cmds.separator( style='single' )
        cmds.setParent( '..' )
        cmds.setParent( '..' )       

        cmds.frameLayout( label='Assign Shaders and Obj ID', borderStyle='in', cll=True )
        cmds.columnLayout(adjustableColumn=True, rowSpacing=10, w = 280)        
        cmds.separator( style='single' )
        
        self.obj_id_value = cmds.intSliderGrp( field=True, label='SetObjectID',
                 minValue=0, maxValue=30, fieldMinValue=0, fieldMaxValue=1000, value=0 )          
        cmds.button( label='Assign Shaders and Obj ID', command=partial(self.add_shdr_obj_id, 1) )   
        cmds.separator( style='single' )
        cmds.setParent( '..' )
        cmds.setParent( '..' )   

        cmds.frameLayout( label='Sort Selection', borderStyle='in', cll=True )
        cmds.columnLayout(adjustableColumn=True, rowSpacing=10, w = 280)                  
        self.sort_list_grp = cmds.textFieldGrp( label='Sort List Group' )
        cmds.button( label='Sort Selection', command=partial(self.run_sort_selection, 1) )
        cmds.separator( style='single' )

        cmds.button( label='Unparent', command=partial(self.unparent, 1) ) 
        cmds.setParent( '..' )
        cmds.setParent( '..' )    
        cmds.setParent( '..' )
        cmds.showWindow( window )
        
    def master_list(self, *args):
        self.selection_group_list = cmds.ls(sl=True, fl=True)
        
    def new_modulo(self, *args):
        new_modulo_val = cmds.textFieldGrp(self.new_modulo_val_set, query=True, text=True)
        return new_modulo_val
            
    def print_modulo(self, *args):
        print 'selecting by ' + str(self.modulo())
    
    def print_new_modulo(self, *args):
        print 'selecting by ' + str(self.new_modulo()) 
        
    def group_set_up(self, *args):
        #Puts selected geo into groups
        geo_list = cmds.ls(sl=True, fl=True)
        geo_type_name = 'instance_'
        new_geo_list = []
        new_group_list = []
        id_num = self.new_modulo()
        slice_point=geo_list[0].find('_pika')
        new_name = geo_list[0][:slice_point]
        modulo_index = cmds.textFieldGrp(self.new_modulo_val_set, query=True, text=True)
        new_group = cmds.group( em=True, name=new_name + '_Sel_x' + modulo_index )
        
        for dummy_geo in geo_list:
            cmds.parent( dummy_geo, new_group )
        
        ## Use the below for converting to instance. 
        '''
        Instance_Replace(geo_list, geo_type_name)
        for dummy_geo in geo_list:
            new_geo_list.append(geo_type_name + dummy_geo)

        for dummy_group in self.selection_group_list:
            group_name = cmds.group( em=True, name=geo_type_name + dummy_group + '_Sel_' + str(id_num))
            new_group_list.append(group_name)
        for dummy_geo in new_geo_list:
            slice_point=dummy_geo.find('_pika')
            new_name = dummy_geo[:slice_point]
            for dummy_group in new_group_list:
                if new_name in dummy_group:
                    cmds.parent( dummy_geo, dummy_group )
        cmds.select(new_geo_list)       
        '''
    def add_shdr_obj_id(self, *args):
        group_list = cmds.ls(sl=True, fl=True)
        id_num = cmds.intSliderGrp(self.obj_id_value, q=True, v=True)
        Shader_Setup(id_num)
        for dummy_group in group_list:
            Vray_Obj_Id(dummy_group, id_num)
            dummy_geo_list = cmds.listRelatives(dummy_group, c=True)
            for dummy_geo in dummy_geo_list:
                cmds.sets(dummy_geo, e=True, forceElement='ObjID_' + str(id_num) +'_lightShaderSG')
        cmds.select(group_list)
        
        
    def run_sort_selection(self, *args):
        sort_cmd = Reorder_Selection()        
        dummy_list = cmds.ls(sl=True, fl=True)
        sort_cmd.natsort(dummy_list)
        group_name = cmds.textFieldGrp(self.sort_list_grp, query=True, text=True)
        sort_group = cmds.group( em=True, name=group_name )
        for dummy_geo in dummy_list:
            cmds.parent( dummy_geo, str(sort_group) )
            
    def unparent(self, *args):
        sel_list = cmds.ls(sl=True, fl=True)
        for dummy_item in sel_list:
            query = cmds.listRelatives(dummy_item, allParents=True)
            for dummy_query in query:
                cmds.parent( dummy_query + '|' + dummy_item, world=True )
    
    def run_command_second(self, *args):
        self.print_new_modulo()
        self.geo_selection_list = list()
        for dummy_group in self.selection_group_list:
            run_cmd = Init_Run(dummy_group)
            run_cmd.next_run(self.new_modulo())
            dummy_tmp_list = cmds.ls(sl=True, fl=True)
            for dummy_tmp in dummy_tmp_list:
                self.geo_selection_list.append(dummy_tmp)
        cmds.select(self.geo_selection_list)
                  
def main():

        Select_By_Multiple_UI()

if __name__=='__main__':
main()

----------------------------
    


Needless to say, this re-order of faces generally works only for strips of polygons. Full planar surfaces is very difficult, as the shapes and circumstances they can come in is probably infinite. 

Still this had been a really satisfying challenge for me.

No comments:

Post a Comment