Add Attribute

Download addAttr.py

python, pyfuncs, maya

A simple function to return the created attribute as string, it wraps all the attributes you'd like to send to the cmds.addAttr function, so you can do something like ;

1
newAttr = add_attribute(target, ln='ikfkSwitch', sn='ikfk',at='float', dv=1.0)

newAttr variable will return 'target.ikfk'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def add_attribute(*args, **kwargs):
    """
    This function simply creates an attributes and returns the adress of the attr
    as polyCube.new_attribute, understand shorthand arguments such as sn, ln etc
    :param   args: all the arguments you wants to send to the addAttr
    :param kwargs: all the  keyword arguments you wants to send to the addAttr
    :return      : string with format ; 'object.new_attribute'
    """
    cmds.addAttr(*args, **kwargs)
    # get the object's name
    if 'name' in kwargs:
        obj_name = kwargs['name']
    elif 'n' in kwargs:
        obj_name = kwargs['n']
    else:
        obj_name = args[0][0] if isinstance(args[0], list) else args[0]
    # get the attribute's name
    if 'sn' in kwargs:
        attr_name = kwargs['sn']
    elif 'ln' in kwargs:
        attr_name = kwargs['ln']
    else:
        attr_name = args[1][0] if isinstance(args[1], list) else args[1]
    return '%s.%s' % (obj_name, attr_name)

If you have any questions... =)