def dump(root=None, file_path=None):
"""Dump the hierarchy data starting at root to disk.
:param root: Root node of the hierarchy.
:param file_path: Export json path.
:return: The hierarchy data that was exported.
"""
if root is None:
root = cmds.ls(sl=True)
if root:
root = root[0]
else:
return
if file_path is None:
file_path = cmds.fileDialog2(fileFilter='Skeleton Files (*.json)', dialogStyle=2, caption='Export Skeleton',
fileMode=0, returnFilter=False)
if file_path:
file_path = file_path[0]
else:
return
data = get_data(root)
fh = open(file_path, 'w')
json.dump(data, fh, indent=4)
fh.close()
logger.info('Exported skeleton to %s', file_path)
return data, file_path
python类fileDialog2()的实例源码
def browse(self, line_edit, relative_to_combobox):
root = cmds.workspace(q=True, rd=True)
file_path = cmds.fileDialog2(fileFilter=self.filter, dialogStyle=2, caption=self.name,
fileMode=1, returnFilter=False, startingDirectory=root)
if file_path:
# Modify the file path to be a path based on the relative_to value
file_path = file_path[0].replace('\\', '/')
relative_to = relative_to_combobox.currentText().replace('\\', '/')
if relative_to == FilePathField.project_root:
project_root = cmds.workspace(q=True, rd=True).replace('\\', '/')
file_path = file_path.replace(project_root, '')
elif relative_to == FilePathField.full_path:
# Do nothing, just take the full path
pass
else:
# Account for if the relative_to is an environment variable.
file_path = os.path.expandvars(file_path)
# Account for if the relative_to is an actual file path.
file_path = re.sub('^{0}'.format(relative_to), '', file_path)
line_edit.setText(file_path)
def ImportFileSelectDialog():
importFrom = None
if cmds.about(version=True)[:4] == "2012": # Support for newer versions
importFrom = cmds.fileDialog2(fileMode=1, fileFilter="SEAnim Files (*.seanim)", caption="Import SEAnim")
else:
importFrom = cmds.fileDialog2(fileMode=1, dialogStyle=2, fileFilter="SEAnim Files (*.seanim)", caption="Import SEAnim")
if importFrom == None or len(importFrom) == 0 or importFrom[0].strip() == "":
return None
path = importFrom[0].strip()
pathSplit = os.path.splitext(path) # Fix bug with Maya 2013
if pathSplit[1] == ".*":
path = pathSplit
return path
# Attempt to resolve the animType for a bone based on a given list of modifier bones, returns None if no override is needed
def load(file_path=None):
"""Load a skeleton hierarchy from the given json data file and generates the hierarchy in Maya.
:param file_path: Json file on disk.
:return: The hierarchy data loaded from disk.
"""
if file_path is None:
file_path = cmds.fileDialog2(fileFilter='Skeleton Files (*.json)', dialogStyle=2, caption='Export Skeleton',
fileMode=1, returnFilter=False)
if file_path:
file_path = file_path[0]
else:
return
data = load_data(file_path)
create_node(data)
return data
def export_skin(file_path=None, shapes=None):
"""Exports the skinClusters of the given shapes to disk in a pickled list of skinCluster data.
:param file_path: Path to export the data.
:param shapes: Optional list of dag nodes to export skins from. All descendent nodes will be searched for
skinClusters also.
"""
if shapes is None:
shapes = cmds.ls(sl=True) or []
# If no shapes were selected, export all skins
skins = get_skin_clusters(shapes) if shapes else cmds.ls(type='skinCluster')
if not skins:
raise RuntimeError('No skins to export.')
if file_path is None:
file_path = cmds.fileDialog2(dialogStyle=2, fileMode=0, fileFilter='Skin Files (*{0})'.format(EXTENSION))
if file_path:
file_path = file_path[0]
if not file_path:
return
if not file_path.endswith(EXTENSION):
file_path += EXTENSION
all_data = []
for skin in skins:
skin = SkinCluster(skin)
data = skin.gather_data()
all_data.append(data)
logging.info('Exporting skinCluster %s on %s (%d influences, %d vertices)',
skin.node, skin.shape, len(data['weights'].keys()), len(data['blendWeights']))
fh = open(file_path, 'wb')
json.dump(all_data, fh)
fh.close()
def browse(path=None):
"""Open a pop-up browser for the user"""
# Acquire path from user input if none defined
if path is None:
scene_path = cmds.file(query=True, sceneName=True)
# use scene file name as default name
default_filename = os.path.splitext(os.path.basename(scene_path))[0]
if not default_filename:
# Scene wasn't saved yet so found no valid name for playblast.
default_filename = "playblast"
# Default to images rule
default_root = os.path.normpath(get_project_rule("images"))
default_path = os.path.join(default_root, default_filename)
path = cmds.fileDialog2(fileMode=0,
dialogStyle=2,
startingDirectory=default_path)
if not path:
return
if isinstance(path, (tuple, list)):
path = path[0]
if path.endswith(".*"):
path = path[:-2]
# Bug-Fix/Workaround:
# Fix for playblasts that result in nesting of the
# extension (eg. '.mov.mov.mov') which happens if the format
# is defined in the filename used for saving.
extension = os.path.splitext(path)[-1]
if extension:
path = path[:-len(extension)]
return path
def get_path(pType, widgetPath, *args):
"""get the path from dialog, and puts it in approp textFieldGrp (based on pType == 'import')"""
path = cmds.fileDialog2(dialogStyle=1, fileMode=3)
cmds.textFieldButtonGrp(widgets[widgetPath], e=True, tx=fix_path(path[0]))
if pType == "import":
clear_tsl()
populate_tsl()
def addToField(num, *args):
"""calls a file dialog to get a path and adds it to the selected field"""
#set the field to add to
textField = widgets["path%s"%num]
#call up browser to look for paths
path = cmds.fileDialog2(fm=3)[0]
#add text
if path:
cmds.textFieldButtonGrp(textField, e=True, tx=path)