PyMel: Creating and Accessing the Notes Attribute in an Object

Today I find myself wondering how I can access the 'notes' section of a Maya object using script.

I searched the internet and found out that to access the notes in an object are stored in an attribute called 'notes'. No surprises.

What surprised me, was when I went through the attributes of a newly created Maya object, I could not find that attribute!

So I do the following experiment.

Consider myObject which is a newly created object in the scene (an empty group).

from pymel.core import *
myObject = group(empty=True)

At this point, it does not contain the attribute 'notes'. The following list comprehension looks through each of myObject's attributes, and only includes attributes with 'notes' in its name in the resulting list:
[x for x in myObject.listAttr() if 'notes' in x.lower()]

This returned an empty list. It means that the attribute does not exist.
# Result: [] #

However, if I enter something into the notes section via the Maya Attribute Editor, and I run the same statement:
[x for x in myObject.listAttr() if 'notes' in x.lower()]

the script editor will now display the following result:
# Result: [Attribute(u'null1.notes')] #

This means that the attribute now exists.

After further searching, I came to a useful blog post that mentions that we can create the 'notes' attribute ourselves through script:

The blog post used MEL examples, so I created the 'notes' attribute via PyMel.

if not [x for x in myObject.listAttr() if 'notes' in x.lower()]:
    myObject.addAttr('notes', dataType='string')
    myObject.notes.set('hello')

This block of code will create the notes attribute and set the value to 'hello'. In the Attribute Editor under the 'notes' section, I will be able to see the field update to contain 'hello'.

Here comes something unexpected.

At this point, if I use the attribute editor to delete the 'hello', leaving the notes field empty again, and then I run:
[x for x in myObject.listAttr() if 'notes' in x.lower()]
# Result: [] #

Maya returns an empty list again. The attribute got removed.

Now, I enter something in the notes field in the Attribute Editor again, so that it is not empty. Then I run the following to test if the 'notes' attribute exists. This time I use the attributeQuery() command:
attributeQuery('notes', node=myObject, exists=True)
# Result: True #

Maya created the 'notes' attribute again.

Now I try to give it an empty string by code (not using the Attribute Editor), to see if Maya will still remove the attribute automatically.
myObject.notes.set('')

Now we try to query the existence of the 'notes' attribute:
attributeQuery('notes', node=myObject, exists=True)
# Result: True #

The attribute exists. Maya did not remove the attribute when we set the attribute to an empty string via script.

Thus we can conclude these things about the 'notes' attribute in Maya nodes.

  • in Maya, objects are created without the 'notes' attribute by default
  • to use the 'notes' attribute for the first time (via script), we need to create the 'notes' attribute.
  • the 'notes' attribute is automatically created when we enter something the 'notes' field using the Attribute Editor
  • the 'notes' attribute is automatically remove when we clear the 'notes' field using the Attribute Editor
  • when we assign an empty string to the 'notes' attribute via script, Maya does not remove the 'notes' attribute

Comments

Popular posts from this blog

Maya UI Layout Example

Maya Expressions Part 02 - Conditional Statements

Jooble.org - A Jobs Platform for VFX Artists