|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Open or reference 'sub_graphs' graph in Maya or Standalone and pass an nxt |
| 4 | +# graph, mayapy.exe path and parameters in the '_maya_sub_graph' node, and |
| 5 | +# this will run that graph in maya standalone |
| 6 | +# |
| 7 | +# Shell Example: |
| 8 | +# open a shell. Navigate to your bin/mayapy.exe file in your installation of |
| 9 | +# maya. Then run: mayapy.exe path/to/this/run_maya_graph.py -g path/to/nxt_graph/you_want_to_run.nxt |
| 10 | +# |
| 11 | + |
| 12 | +# command line arguments |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import os |
| 16 | + |
| 17 | + |
| 18 | +# define the function for the dictionary argument |
| 19 | +def dict_or_string(value={}): |
| 20 | + '''This will ensure the data being passed to the argparse for parameters |
| 21 | + is always a dictionary. We expect a json string, a dictionary, or a path to |
| 22 | + a json file |
| 23 | +
|
| 24 | + :param value: The parameters you want to pass to the graph, defaults to {} |
| 25 | + :type value: dict | str, optional |
| 26 | + :raises TypeError: Error if it's not a str that is json or dict |
| 27 | + :return: Return the dictionary of the data being passed as str or dict |
| 28 | + :rtype: dict |
| 29 | + ''' |
| 30 | + # check see if it's a dictionary. |
| 31 | + if isinstance(value, dict): |
| 32 | + return value |
| 33 | + try: |
| 34 | + # Try parsing as a dictionary |
| 35 | + parsed_dict = json.loads(value) |
| 36 | + if isinstance(parsed_dict, dict): |
| 37 | + return parsed_dict |
| 38 | + except ValueError: |
| 39 | + pass |
| 40 | + # Check if value string is a valid parameters file |
| 41 | + if os.path.isfile(value): |
| 42 | + try: |
| 43 | + # open the filepath and load the json file |
| 44 | + with open(value, 'r') as fp: |
| 45 | + return json.load(fp) |
| 46 | + except json.JSONDecodeError: |
| 47 | + pass |
| 48 | + raise TypeError( |
| 49 | + 'Passed value must be of type dict, string of a dict, or filepath to parameters file!' |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +# Initialize parser |
| 54 | +parser = argparse.ArgumentParser(description='This is a cli for running the standalone maya') |
| 55 | + |
| 56 | +# Adding optional argument |
| 57 | +parser.add_argument( |
| 58 | + '-g', '--graph_path', help='The path to the nxt graph you want to run.', required=True |
| 59 | +) |
| 60 | +parser.add_argument( |
| 61 | + '-p', |
| 62 | + '--parameters', |
| 63 | + help='''The parameters you want to pass to the graph. Parameters are a string representing a |
| 64 | + dictionary.e.g. {'path/to/node.attr':'value'}''', |
| 65 | + type=dict_or_string, |
| 66 | +), |
| 67 | +parser.add_argument( |
| 68 | + '-s', '--start_node', help='The path to the nxt node init the graph you want to run.' |
| 69 | +) |
| 70 | + |
| 71 | +# Read arguments from command line |
| 72 | +args = parser.parse_args() |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + # import maya standalone |
| 77 | + from maya import standalone |
| 78 | + |
| 79 | + # initialize maya standalone |
| 80 | + standalone.initialize() |
| 81 | + |
| 82 | + # in maya import the execute graph for nxt |
| 83 | + from nxt import execute_graph |
| 84 | + |
| 85 | + # make sure you can evaluate the start node |
| 86 | + start_node = None |
| 87 | + if args.start_node: |
| 88 | + if isinstance(args.start_node, str): |
| 89 | + start_node = args.start_node |
| 90 | + |
| 91 | + # execute the graph |
| 92 | + execute_graph(args.graph_path, parameters=args.parameters, start=start_node) |
| 93 | + # uninitialize maya standalone |
| 94 | + standalone.uninitialize() |
0 commit comments