Skip to content

Commit

Permalink
Add default prim drop down
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelliu-adsk committed Jan 24, 2024
1 parent 032a36e commit 7994bc8
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/mayaUsd/commands/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Each base command class is documented in the following sections.
| `-apiSchema` | `-api` | string (multi) | none | Imports the given API schemas' attributes as Maya custom attributes. This only recognizes API schemas that have been applied to prims on the stage. The attributes will properly round-trip if you re-export back to USD. |
| `-chaser` | `-chr` | string(multi) | none | Specify the import chasers to execute as part of the import. See "Import Chasers" below. |
| `-chaserArgs` | `-cha` | string[3] multi| none | Pass argument names and values to import chasers. Each argument to `-chaserArgs` should be a triple of the form: (`<chaser name>`, `<argument name>`, `<argument value>`). See "Import Chasers" below. |
| `-defaultPrim` | `-dp` | string | none | Set the default prim on export. |
| `-excludePrimvar` | `-epv` | string (multi) | none | Excludes the named primvar(s) from being imported as color sets or UV sets. The primvar name should be the full name without the `primvars:` namespace prefix. |
| `-excludePrimvarNamespace` | `-epv` | string (multi) | none | Excludes the named primvar namespace(s) from being imported as color sets or UV sets. The namespance should be the name without the `primvars:` namespace prefix. Ex: `primvars:excludeNamespace:excludeMe:excludeMetoo` can be excluded by specifying `excludeNamespace` or `excludeNamespace:excludeMe`|
| `-excludeExportTypes` | `-eet` | string (multi) | none | Excludes the selected types of prims from being exported. The options are: Meshes, Cameras and Lights.|
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/fileio/jobs/jobArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ struct UsdMayaJobExportArgs
const TfToken::Set includeAPINames;
const TfToken::Set jobContextNames;
const TfToken::Set excludeExportTypes;
const std::string defaultPrim;
const std::string defaultPrim;

using ChaserArgs = std::map<std::string, std::string>;
const std::vector<std::string> chaserNames;
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/fileio/primWriterArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class UsdMayaPrimWriterArgs
const MDagPath& dagPath,
const bool exportRefsAsInstanceable,
const TfToken::Set& excludeExportTypes,
const std::string& defaultPrim);
const std::string& defaultPrim);

/// \brief returns the MObject that should be exported.
MAYAUSD_CORE_PUBLIC
Expand Down
1 change: 1 addition & 0 deletions plugin/adsk/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
list(APPEND scripts_src
mayaUsdTranslatorImport.mel
mayaUsdTranslatorExport.mel
mayaUsd_exportHelpers.py
mayaUSDRegisterStrings.mel
mayaUSDRegisterStrings.py
AEmayaUsdProxyShapeBaseTemplate.mel
Expand Down
2 changes: 2 additions & 0 deletions plugin/adsk/scripts/mayaUSDRegisterStrings.mel
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ global proc mayaUSDRegisterStrings()
register("kExportRelativeTexturesLbl", "Texture File Paths:");
register("kExportVisibilityAnn", "Exports Maya visibility attributes as USD metadata.");
register("kExportVisibilityLbl", "Visibility");
register("kExportDefaultPrimLbl", "Default Prim");
register("kExportDefaultPrimAnn", "Default Prim");

// All strings for import dialog:
register("kImportAnimationDataLbl", "Animation Data");
Expand Down
91 changes: 84 additions & 7 deletions plugin/adsk/scripts/mayaUsdTranslatorExport.mel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//

global string $gMayaUsdTranslatorExport_SectionNames[];
global int $exportAll;

proc string stringRemoveSuffix(string $object, string $suffix)
{
Expand Down Expand Up @@ -190,6 +191,14 @@ proc string mayaUsdTranslatorExport_AppendJobContexts(string $currentOptions, st
return $currentOptions;
}

proc string mayaUsdTranslatorExport_AppendDefaultPrim(string $currentOptions, string $arg) {
if (`optionMenuGrp -exists exportDefaultPrim` == 0)
return $currentOptions;

string $defaultPrim = `optionMenuGrp -q -value exportDefaultPrim`;
return $currentOptions + ";" + $arg + "=" + $defaultPrim;
}

proc string mayaUsdTranslatorExport_AppendFromPopup(string $currentOptions, string $arg, string $widget) {
if (`optionMenuGrp -exists $widget` == 0)
return $currentOptions;
Expand Down Expand Up @@ -242,6 +251,48 @@ proc string mayaUsdTranslatorExport_AppendFromTextField(string $currentOptions,
return $currentOptions + ";" + $arg + "=" + $value;
}

proc updateDefaultPrimList() {
if ((`checkBoxGrp -exists exportMeshesCheckBox` == 0)||(`checkBoxGrp -exists exportLightsCheckBox` == 0)||(`checkBoxGrp -exists exportCamerasCheckBox` == 0)){
return;
}
int $excludeMesh = (`checkBoxGrp -q -v1 exportMeshesCheckBox` == 0);
int $excludeLight = (`checkBoxGrp -q -v1 exportLightsCheckBox` == 0);
int $excludeCamera = (`checkBoxGrp -q -v1 exportCamerasCheckBox` == 0);

string $allItems[];
global int $exportAll;
if($exportAll){
$allItems = `python("import mayaUsd_exportHelpers; mayaUsd_exportHelpers.updateDefaultPrimCandidates('" + $excludeMesh + "','" + $excludeLight + "', '" + $excludeCamera +"')")`;

} else{
$allItems = `python("import mayaUsd_exportHelpers; mayaUsd_exportHelpers.updateDefaultPrimCandidatesFromSelection('" + $excludeMesh + "','" + $excludeLight + "', '" + $excludeCamera +"')")`;
}
optionMenuGrp -edit -deleteAllItems exportDefaultPrim;
string $menuName = `optionMenuGrp -q -fullPathName exportDefaultPrim`;
menuItem -parent ($menuName + "|OptionMenu") -l "None";
for($item in $allItems){
menuItem -parent ($menuName + "|OptionMenu") -l $item;
}

if(size($allItems) > 0){
optionMenuGrp -e -select 2 exportDefaultPrim;
}

}

global proc onExportParentScopeChanged() {
string $rootPrim = `textFieldGrp -q -text parentScopeField`;
optionMenuGrp -edit -deleteAllItems exportDefaultPrim;
string $menuName = `optionMenuGrp -q -fullPathName exportDefaultPrim`;
menuItem -parent ($menuName + "|OptionMenu") -l "None";
menuItem -parent ($menuName + "|OptionMenu") -l $rootPrim;
if(size($rootPrim) > 0){
optionMenuGrp -e -select 2 exportDefaultPrim;
} else {
updateDefaultPrimList();
}
}

global proc mayaUsdTranslatorExport_AnimationCB() {
if (`checkBoxGrp -exists animationCheckBox` == 0)
return;
Expand All @@ -252,7 +303,8 @@ global proc mayaUsdTranslatorExport_AnimationCB() {
global proc mayaUsdTranslatorExport_MeshCB() {
if (`checkBoxGrp -exists exportMeshesCheckBox` == 0)
return;


updateDefaultPrimList();
columnLayout -e -enable (`checkBoxGrp -q -v1 exportMeshesCheckBox` == 1) meshExportOptsCol;
columnLayout -e -enable (`checkBoxGrp -q -v1 exportMeshesCheckBox` == 1) materialOptsCol;
}
Expand Down Expand Up @@ -431,6 +483,8 @@ global proc mayaUsdTranslatorExport_SetFromOptions(string $currentOptions, int $
int $exportMeshes = 1;
int $exportCameras = 1;
int $exportLights = 1;

string $excludeTypes;
for ($index = 0; $index < size($optionList); $index++) {
tokenize($optionList[$index], "=", $optionBreakDown);
if ($optionBreakDown[0] == "exportUVs") {
Expand All @@ -452,7 +506,7 @@ global proc mayaUsdTranslatorExport_SetFromOptions(string $currentOptions, int $
} else if($optionBreakDown[0] == "excludeExportTypes"){
$contextForcesExcludeTypes = 1;
if(mayaUsdTranslatorExport_HasFilteredPrimitive($optionBreakDown[1], "Meshes")){
$exportMeshes = 0;
$exportMeshes = 0;
}
if(mayaUsdTranslatorExport_HasFilteredPrimitive($optionBreakDown[1], "Cameras")){
$exportCameras = 0;
Expand Down Expand Up @@ -490,7 +544,7 @@ global proc mayaUsdTranslatorExport_SetFromOptions(string $currentOptions, int $
} else if ($optionBreakDown[0] == "frameSample") {
mayaUsdTranslatorExport_SetTextField($optionBreakDown[1], $enable, "frameSampleField");
} else if ($optionBreakDown[0] == "parentScope") {
mayaUsdTranslatorExport_SetTextField($optionBreakDown[1], $enable, "parentScopeField");
//mayaUsdTranslatorExport_SetTextField($optionBreakDown[1], $enable, "parentScopeField");
} else if ($optionBreakDown[0] == "convertMaterialsTo") {
mayaUsdTranslatorExport_SetConvertMaterialsToCheckboxes($optionBreakDown[1], $enable, $processJobContext);
} else if ($optionBreakDown[0] == "shadingMode" && $optionBreakDown[1] != "useRegistry") {
Expand All @@ -516,9 +570,9 @@ global proc mayaUsdTranslatorExport_SetFromOptions(string $currentOptions, int $
}
if($contextForcesExcludeTypes == 1){
checkBoxGrp -e -v1 $exportMeshes -en $enable exportMeshesCheckBox;
mayaUsdTranslatorExport_MeshCB();
checkBoxGrp -e -v1 $exportCameras -en $enable exportCamerasCheckBox;
checkBoxGrp -e -v1 $exportLights -en $enable exportLightsCheckBox;
mayaUsdTranslatorExport_MeshCB();
}

if ($jobContext != "" && $processJobContext == 1) {
Expand Down Expand Up @@ -625,6 +679,26 @@ global proc int mayaUsdTranslatorExport (string $parent,
// 0 otherwise.
//
{
// check for Export All or Export Selection
// check for export option box window first, if it doesn't exist means user opened the main export window
global string $gOptionBox;
global int $exportAll;
if (`window -exists $gOptionBox`) {
string $title = `window -query -title $gOptionBox`;
if ($title == `uiRes("m_fileOptions.kExportAllOptions")`) {
$exportAll = 1;
} else if ($title == `uiRes("m_fileOptions.kExportSelectionOptions")`) {
$exportAll = 0;
}
} else{
global string $gOperationMode;
if ($gOperationMode == "ExportAll"){
$exportAll = 1;
} else{
$exportAll = 0;
}
}

int $bResult;
string $currentOptions;

Expand Down Expand Up @@ -679,9 +753,11 @@ global proc int mayaUsdTranslatorExport (string $parent,
//separator -style "none";

textFieldGrp -l `getMayaUsdString("kExportParentScopeLbl")` -placeholderText `getMayaUsdString("kExportParentScopePht")`
-annotation `getMayaUsdString("kExportParentScopeAnn")` parentScopeField;
-annotation `getMayaUsdString("kExportParentScopeAnn")` -textChangedCommand "onExportParentScopeChanged" parentScopeField;
}

optionMenuGrp -l `getMayaUsdString("kExportDefaultPrimLbl")` -annotation `getMayaUsdString("kExportDefaultPrimAnn")` exportDefaultPrim;

separator -style "none";
setParent ..;
}
Expand Down Expand Up @@ -795,8 +871,8 @@ global proc int mayaUsdTranslatorExport (string $parent,
frameLayout -label `getMayaUsdString("kExportFrameAdvancedLbl")` -collapsable true -collapse $collapse advancedFrameLayout;
separator -style "none";

checkBoxGrp -label "" -label1 `getMayaUsdString("kExportCamerasLbl")` -annotation `getMayaUsdString("kExportCamerasAnn")` exportCamerasCheckBox;
checkBoxGrp -label "" -label1 `getMayaUsdString("kExportLightsLbl")` -annotation `getMayaUsdString("kExportLightsAnn")` exportLightsCheckBox;
checkBoxGrp -label "" -label1 `getMayaUsdString("kExportCamerasLbl")` -annotation `getMayaUsdString("kExportCamerasAnn")` -cc ("updateDefaultPrimList") exportCamerasCheckBox;
checkBoxGrp -label "" -label1 `getMayaUsdString("kExportLightsLbl")` -annotation `getMayaUsdString("kExportLightsAnn")` -cc ("updateDefaultPrimList") exportLightsCheckBox;

checkBoxGrp -label "" -label1 `getMayaUsdString("kExportVisibilityLbl")` -annotation `getMayaUsdString("kExportVisibilityAnn")` exportVisibilityCheckBox;

Expand Down Expand Up @@ -854,6 +930,7 @@ global proc int mayaUsdTranslatorExport (string $parent,
if (stringArrayContains("output-parentscope", $sectionNames)) {
$currentOptions = mayaUsdTranslatorExport_AppendFromTextField($currentOptions, "parentScope", "parentScopeField");
}
$currentOptions = mayaUsdTranslatorExport_AppendDefaultPrim($currentOptions, "defaultPrim");
}

if (stringArrayContains("materials", $sectionNames)) {
Expand Down
66 changes: 66 additions & 0 deletions plugin/adsk/scripts/mayaUsd_exportHelpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import maya.cmds as cmds
import re
from mayaUSDRegisterStrings import getMayaUsdString


__naturalOrderRE = re.compile(r'([0-9]+)')
def natural_key(item):
return [int(s) if s.isdigit() else s.lower() for s in __naturalOrderRE.split(item)]

def updateDefaultPrimCandidates(excludeMesh, excludeLight, excludeCamera):
allItems = cmds.ls(assemblies=True)
excludeList = []
if excludeMesh == "1":
meshes = cmds.ls(type=('mesh'), l=True)
for m in meshes:
excludeList.append(cmds.listRelatives(m, parent=True)[0])

if excludeLight == "1":
lights = cmds.ls(type=('light'), l=True)
for l in lights:
excludeList.append(cmds.listRelatives(l, parent=True)[0])

if excludeCamera == "1":
cameras = cmds.ls(type=('camera'), l=True)
for c in cameras:
excludeList.append(cmds.listRelatives(c, parent=True)[0])
else:
cameras = cmds.ls(type=('camera'), l=True)
startup_cameras = []
for c in cameras:
if cmds.camera(cmds.listRelatives(c, parent=True)[0], startupCamera=True, q=True):
startup_cameras.append(cmds.listRelatives(c, parent=True)[0])

allItems = list(set(allItems) - set(startup_cameras))

allItems = list(set(allItems) - set(excludeList))
allItems.sort(key=natural_key)
return allItems

def updateDefaultPrimCandidatesFromSelection(excludeMesh, excludeLight, excludeCamera):
allSelectedItems = cmds.ls(selection=True)
allItems = set()
for i in allSelectedItems:
path = cmds.listRelatives(i, fullPath=True)[0]
root = path.split("|")[1]
allItems.add(root)

excludeSet = set()
if excludeMesh == "1":
meshes = cmds.ls(type=('mesh'), l=True)
for m in meshes:
excludeSet.add(cmds.listRelatives(m, parent=True)[0])

if excludeLight == "1":
lights = cmds.ls(type=('light'), l=True)
for l in lights:
excludeSet.add(cmds.listRelatives(l, parent=True)[0])

if excludeCamera == "1":
cameras = cmds.ls(type=('camera'), l=True)
for c in cameras:
excludeSet.add(cmds.listRelatives(c, parent=True)[0])

allItems = list(allItems - excludeSet)
allItems.sort(key=natural_key)
return allItems
1 change: 1 addition & 0 deletions test/lib/usd/translators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(TEST_SCRIPT_FILES
testUsdExportCamera.py
testUsdExportColorSets.py
testUsdExportConnected.py
testUsdExportDefaultPrim.py
testUsdExportDisplayColor.py
testUsdExportEulerFilter.py
testUsdExportFileFormat.py
Expand Down
73 changes: 73 additions & 0 deletions test/lib/usd/translators/testUsdExportDefaultPrim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env mayapy
#
# Copyright 2024 Autodesk
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import unittest

from maya import cmds
from maya import standalone

from pxr import Usd
import fixturesUtils

class testUsdExportMesh(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.inputPath = fixturesUtils.setUpClass(__file__)
cls.temp_dir = os.path.abspath('.')

cls.testFile = os.path.join(cls.inputPath, "UsdExportTypeTest", "UsdExportTypeTest.ma")
cmds.file(cls.testFile, force=True, open=True)

@classmethod
def tearDownClass(cls):
standalone.uninitialize()

def _ValidateDefaultPrim(self):
cmds.file(self.testFile, force=True, open=True)
usdFile = os.path.abspath('UsdExportDefaultPrim.usda')

cmds.usdExport(mergeTransformAndShape=True, file=usdFile,
shadingMode='none', defaultPrim='pSphere1')

stage = Usd.Stage.Open(usdFile)
self.assertEqual(stage.GetDefaultPrim().GetName(), 'pSphere1')

cmds.file(self.testFile, force=True, open=True)
cmds.usdExport(mergeTransformAndShape=True, file=usdFile,
shadingMode='none', defaultPrim='pointLight1')

stage = Usd.Stage.Open(usdFile)
self.assertEqual(stage.GetDefaultPrim().GetName(), 'pointLight1')

def _ValidateWithParentScope(self):
cmds.file(self.testFile, force=True, open=True)
usdFile = os.path.abspath('UsdExportDefaultPrim.usda')
cmds.usdExport(mergeTransformAndShape=True, file=usdFile,
shadingMode='none', parentScope='testScope', defaultPrim = 'testScope')

stage = Usd.Stage.Open(usdFile)
self.assertEqual(stage.GetDefaultPrim().GetName(), 'testScope')

def testExportDefaultPrim(self):
self._ValidateDefaultPrim()
self._ValidateWithParentScope()


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 7994bc8

Please sign in to comment.