Friday, February 28, 2014

Maya Tools - Locator size by scale - Write a function

So, we saw there was a simple 4 lines script to adjust locator size by scale.
How to make a so called tool?
I have no idea whether there is a scientific approach of that. I know about writing a specification. As a matter of fact I found a good article about that: Painless Functional Specification
I simply skipped the first chapter because I knew that is important so I didn't need a tale about that :)

But before I even knew about specification I usually thought as a user because I stared my "career" as an artist not a TD.
In this case this tool  should be only one click solution: Set Locator Scale By Size. To make it clear it is not for locator type object but cameras and lights. There shouldn't have been any other things just one button to click so we can move on to technical specification.

First of all we should write a function:

 def LocatorSizeByScale(Selection):  
     for item in Selection:  
         scaleVal = cmds.getAttr(item + ".s")[0][0]   
         cmds.setAttr(item + ".s", 1, 1, 1)   
         cmds.setAttr(cmds.listRelatives(item, shapes = 1)[0] + ".locatorScale", scaleVal)  

The argument should be a Selection. In maya ls command results a list no matter whether it is one or more object so the Selection has to be a list (array). With for cycle it works more than just one object. So we can run the function like this:

 LocatorSizeByScale(cmds.ls(sl = 1))

But as I wrote before there could be a lot of error situation. I'm a visual thinker. I don't make every time flowcharts but for the sake of this demonstration I'm going through the whole process of visual design. I'm a self-taught programmer and this is my practice not a scientific approach.

First I made a sketch of a flowchart.



It took around 5 minutes and another 5 minutes to verify and add notes. After that I could start coding. I had a clear picture how it would work. Okay but I don't expect reading that from anybody. I made a better picture for this article.





This flowchart shows everything what we need. If we can draw a flowchart like this we have a clear picture how the function is going to work. So the coding is just an execution of the plan.

The result code:

 def LocatorSizeByScale(Selection):  
     '''  
     DESCRIPTION  
     Scaling camera for eg. can cause problems (Z-Depth pass can be wrong for eg.). So scale value should be transfered to the locatorScale attribute.  
     If the object (the selected node's first shape node) has locatorScale attr it will be adjusted by the transform node scale.  
     INPUT ARGUMENTS  
     list - Selection - Selected nodes  
     RETURN  
     None  
     DEPENDENCIES  
     maya.cmds  
     maya.mel  
     '''  
     if Selection:  
         for item in Selection:  
             if cmds.nodeType(item) == "transform":  
                 # Only checks scaleX attribute. That means non-uniform scale not handled.  
                 scaleXVal = cmds.getAttr(item + ".scale")[0][0]  
                 if scaleXVal != 1:  
                     # Only the first shape node considered  
                     shapeNode = cmds.listRelatives(item, shapes = 1)[0]  
                     if shapeNode:  
                         if mel.eval("attributeExists \"locatorScale\" " + shapeNode):  
                             locatorScaleVal = cmds.getAttr(shapeNode + ".locatorScale")  
                             cmds.setAttr(item + ".scale", 1, 1, 1)   
                             cmds.setAttr(shapeNode + ".locatorScale", locatorScaleVal * scaleXVal)  
                         else:  
                             raise StandardError("There is no locatorScale attribute to adjust.")  
                     else:  
                         raise StandardError("There is no shape node.")  
                 else:  
                     print "Default scale value. Skipped."  
             else:  
                 raise StandardError("Only works for transform selection.")  
     else:  
         raise StandardError("There is no selection.")  
 
Further development
Better error handling in connection with selection and selection types
Non-uniform scaled object
Option to warn non-uniform scaled object
Option to warn already set locatorScale value

We will continue...

Recent News - Kenau

I didn't have time to write during the last couple of mouths. It was quite tricky to manage my time. After all our project in the studio where I work was finished.
Kenau is historical film take place at the time of Dutch War of Independence. So at Digital Apes we handled around 300 VFX shots. There were different type effects we did. Mainly set extensions, cg cityscapes, matte paints, clean ups. There were shots we had to add arrows (usually storm of arrows), blood on swords and on amours, fire, smoke, etc. And there were more complex shots where we add a lot of elements like crowd, explosions, demolitions. And last but not least there was the opening full cg shot which was around 1000 frame long and we had to create a cg Great Black-backed Gull and a complete cg aerial view of the city of Haarlem at the time 16th century.
Personally I was glad to work on a movie what makes sense for me. We didn't have to make giant robots or dragons or demolish whole cities. As I wrote before I like movies where the VFX don't lead the story but support it. Kenau is like that. I can't wait to see it in theater.
I was involved just a couple of VFX heavy shots directly. But I supported the whole 3D crew with technical and pipeline stuff as well.
First of all I made the animatic for the opening shot. After we had to clarify our asset pipeline. Basically it meant we had to establish a naming convention for textures, shaders and Arnold StandIns. I wrote dozens of python functions to make it simple but it turned out the issue is more complex so I didn't have time to make a tool what every 3D guy could use. The next pipeline issue was the camera. The idea was that we should use Alembic camera so all the nuke and maya users would use the same asset for their shots.
My biggest challenge was to create the rig and the animation of the seagull. It took around two month. I can't say it could not be better but I like the result very much. I'm biased :) If I have time I will write a whole post about the seagull rig. To summarize I can write only common places: to have good animation, and anatomy references was crucial. Of course we could download a lot of pictures from the net but it was almost impossible to get a good quality footage about a Great Black-backed Gull flying as the camera followed it. The other problematic thing was the skeleton. I found a lot pictures about a bird skeleton but not seagull. We talked about in the studio we should catch a real seagull and did the research for ourselves (if you know what I mean).
Other major challenges were the demolition, explosion scenes. There were 4 large scale explosion in the movie I had to create. I create maya fluid effects and particle and nCloth simulations. I have lot of things to write about this topic so I'm going to continue it (soon)...