Working with string options

I’ve written an example script for working with string options in Maya.  The four main functions are:

  • stringOptionExists(name)
  • setStringOption(name, value)
  • getStringOption(name)
  • removeStringOption(name)

The usage is pretty simple.  stringOptionExists checks if a string option already exists (returning True or False), setStringOption will set a string option value, getStringOption will return a string option value, and removeStringOption will delete the string option.  setStringOption and getStringOption will match string option data types with python data types.

“string” -> Python’s str type

  • text, e.g. “hello world”

“boolean” -> Python’s bool type

  • True or False

“integer” -> Python’s int type

  • any whole number, e.g. -5, 0, 1, 100, etc…

“scalar”‘ -> Python’s float type

  • any decimal number, e.g. -5.4, 0.0, 1.0, 104.23, etc…

“color” -> Python’s tuple type of length 4 (although you can set it using a list of length 4)

  • four scalars corresponding to RGBA, e.g. (1.0, 1.0, 1.0, 1.0)

“vector” -> Python’s tuple type of length 3 (although you can set it using a list of length 3)

  • three scalars corresponding to XYZ, e.g. (1.0, 1.0, 1.0)

undeclared -> Python’s None type

Here is an example usage of working with the script.  First I check if the “unified sampling” string option exists, printing out it’s value if it does.  Next I set all of the standard unified sampling string options, creating them if they do not already exist.  Then I print the string option values that I just created right before I remove those string options:

from string_options import stringOptionExists, setStringOption, getStringOption, removeStringOption

# Check to see if the unified sampling string option exists.
if stringOptionExists("unified sampling"):
    value = getStringOption("unified sampling")
    print "The unified sampling string option already exists. It's value is", value
else:
    print "The unified sampling string option does not exist. We will create it when we set it."

# Set unified sampling string options.
setStringOption("unified sampling", True)
setStringOption("samples quality", (0.5, 0.5, 0.5, 0.5))
setStringOption("samples min", 1.0)
setStringOption("samples max", 100.0)
setStringOption("samples error cutoff", (0.0, 0.0, 0.0, 0.0))

# Print unified sampling values
print getStringOption("unified sampling")
print getStringOption("samples quality")
print getStringOption("samples min")
print getStringOption("samples max")
print getStringOption("samples error cutoff")

# Remove unified sampling string options
removeStringOption("unified sampling")
removeStringOption("samples quality")
removeStringOption("samples min")
removeStringOption("samples max")
removeStringOption("samples error cutoff")

You can download the script here: string_options.py


			

About bnrayner

I am a VFX developer specializing in 3d rendering. With a background in Physics and Digital Art, I enjoying using math to explain how light interacts with materials and to creating pretty pictures along the way.

Posted on July 14, 2012, in Developer, maya, scripts. Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: