Unity Editor Script – Group Selected

posted in: Uncategorized | 0

This post offers an editor convenience class for Maya-like grouping of objects in the Unity scene hierarchy. It configures this script to run with the hotkey (Ctrl+G). Drop this into the editor. Your artists will love it. It currently does not take into account any parent groups that might be above your selected transforms. It will always try to group the selected to the an empty node at the world root.

 

using UnityEngine;
using UnityEditor;
using System.Collections;

public class GroupSelected : EditorWindow
{
    /// <summary>
    ///  Creates an empty node at the center of all selected nodes and parents all selected underneath it. 
    ///  Basically a nice re-creation of Maya grouping!
    /// </summary>

    [MenuItem("GameObject/Group Selected %g", priority = 80)]
    static void Init()
    {
        Transform[] selected = Selection.GetTransforms(SelectionMode.ExcludePrefab | SelectionMode.TopLevel);

        GameObject emptyNode = new GameObject();
        Vector3 averagePosition = Vector3.zero;
        foreach(Transform node in selected)
        {
            averagePosition += node.position;
        }
        if (selected.Length > 0)
        {
            averagePosition /= selected.Length;
        }
        emptyNode.transform.position = averagePosition;
        emptyNode.name = "group";
        foreach (Transform node in selected)
        {
            node.parent = emptyNode.transform;
        }
   }
}

 

 

Select By Material – Python Snippet

posted in: Uncategorized | 0

Here’s another one-line snippet that selects all faces and objects in a scene based on shading group set. Material assignment in Maya is driven by “sets”. According to the Maya documentation, “a set is a logical grouping of an arbitrary collection of objects, attributes, or components of object.”  We’ll be querying a set to find everything assigned by a specific material and then selecting it.

cmds.select(cmds.sets("initialShadingGroup", q=True))

This line selects faces and objects assigned with lambert1, a helpful check I perform in scenes before exporting. (We never want artists to export default Maya materials.) It shows the artist where in the scene the lambert1 lives by highlighting it in the selection.

If you don’t know the name of the set associated with the material off hand (you probably won’t), you can easily translate from material name to set:

materialName = "lambert1"
shadingGroup = cmds.listConnections(materialName, type="shadingEngine")
componentsWithMaterial = cmds.sets(shadingGroup, q=True)
cmds.select(componentsWithMaterial)

 

List All Texture Files In Scene – Python Method

posted in: Uncategorized | 0

Here’s another Python snippet you can use in Maya to, in one line, gather a list of all the files referenced in your scene. This is of great use whenever you need to copy, export, or do other actions on textures associated with a particular Maya scene.

allFiles = [cmds.getAttr("%s.fileTextureName"%file) for file in cmds.ls(type="file")]

This Python list comprehension iterates over every file found using the ls command and then gets the attribute that points to the file node’s texture path. Easy!

 

As an alternative, if you’re concerned about only transferring specific file types, or handling certain file types differently, you can use the following extended version of the above line.

fileFilter = ".jpg"
typedFiles = [cmds.getAttr("%s.fileTextureName"%file) for file in cmds.ls(type="file") if fileFilter in cmds.getAttr("%s.fileTextureName"%file)]

Save To Clipboard Python Method

posted in: Uncategorized | 0

Here’s a super useful snippet you can use to copy any string to the user’s OS clipboard. We use this for any manual data-entry into databases or spreadsheets.

def addToClipboard(text):
    command = 'echo ' + text.strip() + '| clip'
    mel.eval("system("%s") "%command)

You’ll notice I wrap the system call with MEL. This is just a personal touch and definitely not needed. I’ve noticed the usual python call to os.system displays an empty command line window for a split second while executing the command. This is a bit distracting to me.. and  the MEL version seems to avoid that. :]