Last week, somebody asked me how to update attributes in XGen. I found that there isn’t many XGen Python API examples.
Some of XGen’ source code is included in your Maya folder <maya>\plug-ins\XGen\scripts\xgenm. It is always a good idea to take a look there for reference.
The XGen Python module is called xgenm. We’ll use the functions inside this package to update the XGen descriptors.
If you read the document in our website, you can find xgenm.xgGlobal is mentioned. It can be used to check whether XGen is present and get the singleton reference of the description editor: xgenm.xgGlobal.DescriptionEditor.
There is a naming convention between XGen and Python API. The palettes in the Python refers to the collections of XGen.
The biggest challenge here is there is no detailed documentation for the user to get the attribute name in XGen. For example, for the Spline Primitives, attribute “Control Using” which determines using attribute or guides to create primitives in XGen becomes an enumerate attribute “iMethod” and accepts 0(attribute) and 1(guides).
I wrote a small piece of script to print active attributes and their values in a XGen descriptor. So we can find the attribute name we want by comparing the values.
import xgenm as xg
import xgenm.xgGlobal as xgg
#palette is collection, use palettes to get collections first.
palettes = xg.palettes()
for palette in palettes:
print "Collection:" + palette
#Use descriptions to get description of each collection
descriptions = xg.descriptions(palette)
for description in descriptions:
print " Description:" + description
objects = xg.objects(palette, description, True)
#Get active objects,e.g. SplinePrimtives
for object in objects:
print " Object:" + object
attrs = xg.allAttrs(palette, description, object)
for attr in attrs:
print " Attribute:" + attr + ", Value:" + xg.getAttr(attr, palette, description, object)
To modify an attribute, we need to provide palette(collection) name, descriptor name, object name and the attribute name in string.
For example, if we want to change the “Control Using” attribute of Spline Primitive object, we should write like this.
#iMethod = Control Using, 0:Attribute, 1:Guides
xg.setAttr("iMethod","1","collection", "description", "SplinePrimitive")
However, you cannot change object by using this function. For example, if I am using to switch from Card Primitive to Spline Primitive, we should use the setActive function.
#if you don't want to override the previewing in viewport, please use False by default
xg.setActive("collection", "description", "SplinePrimitive", False)
If you tried to run the code above, you may find that nothing happened in the description editor. The XGen core and the UI are separated; but renderman for example will use the updated values. To make things more clear. We still need to update the description editor to see the update in the UI.
#Get the description editor first.
de = xgg.DescriptionEditor
#Do a full UI refresh
It will refresh the description editor with the latest updates.
This is it :)