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
import xgenm.XgExternalAPI as xge
if xgg.Maya:
#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",xge.prepForAttribute("1"),"collection", "description", "SplinePrimitive")
You'll need to call xgenm.XgExternalAPI.prepFroAttribute to escape "\\n", '\n', "\\t" and '\t' to "\\\\n", "\\n", "\\\\t" and "\\t" for file path and expressions.
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
de.refresh("Full")
It will refresh the description editor with the latest updates.
This is it :)
Thanks, for that information!
At my studio we want to get deeper into xGen python API, to create custom UI's for xGen. Unfortunately the Maya documentation is pretty thin on that. Do you know or have any further resourches, documentations on the xGen Python API?
Best,
Oli
Posted by: Oliver Kling | January 08, 2016 at 03:03 AM
Hi Oliver,
Unfortunately, there aren't many documents more than the ones on our website. But the XGen UI is open source, you can read and try to modify it by yourself. It's in the xgen/scripts/xgenm/ui folder.
If you had problem related to xgen Python API, you can post them on our forum or send us questions through ADN support portal if you are an ADN member.
Yours,
Li
Posted by: Chengxi Li | January 13, 2016 at 12:56 AM