MaterialXLab API  0.0.1
APIs For MaterialXLab Libraries
Loading...
Searching...
No Matches
MxDefinitionCreator Class Reference

Class which will create a MaterialX definition from a node graph. More...

Public Member Functions

 constructor (compoundGraph)
 Constructor.
 
 getDefaultOptions ()
 Get the default options for definition creation.
 
 sanitizeXMLString (docString)
 Sanitize a string to be safe to use with XML / HTML.
 
 setOptions (new_options)
 Set the options for definition creation.
 
 execute ()
 Create a new definition.
 

Detailed Description

Class which will create a MaterialX definition from a node graph.

Definition at line 710 of file JsMaterialXGraph.js.

Member Function Documentation

◆ constructor()

MxDefinitionCreator::constructor ( compoundGraph)

Constructor.

Definition at line 715 of file JsMaterialXGraph.js.

715 {
716 this.compoundGraph = compoundGraph;
717 this.options = this.getDefaultOptions();
718
719 this.DEFINITION_NAME = 'definitionName';
720 this.UI_NAME = 'uiName';
721 this.NODEGROUP = 'nodeGroup';
722 this.VERSION = 'version';
723 this.DEFAULT_VERSION = 'defaultVersion';
724 this.DEFINITION_PREFIX = 'definitionPrefix';
725 this.NODEGRAPH_PREFIX = 'nodegraphPrefix';
726 this.DOCUMENTATION = 'documentation';
727 this.NAMESPACE = 'namespace';
728 }
getDefaultOptions()
Get the default options for definition creation.

◆ execute()

MxDefinitionCreator::execute ( )

Create a new definition.

Returns
{MaterialX document with the definition, or null if no graph supplied

Definition at line 789 of file JsMaterialXGraph.js.

790 {
791 if (!this.compoundGraph) {
792 return null;
793 }
794 //console.log('Options:', this.options)
795 let nodeGraph = this.compoundGraph;
796
797
798 let category = nodeGraph.getName();
799 if (this.options['definitionName']) {
800 category = this.options['definitionName'];
801 }
802
803 let identifier = "";
804
805 let embedNamespace = true;
806 // Due to lack of robust support for namespace metadata, we will add the namespace
807 // as a prefix to the identifiers
808 let namespace = mx.createValidName(this.options[this.NAMESPACE])
809 // Replace : with _ to avoid issues with namespaces
810 namespace = namespace.replace(':', '_')
811 if (embedNamespace && namespace.length > 0)
812 {
813 category = namespace + '_' + category;
814 }
815 // If category starts with a number or '_' add a "c_" prefix
816 if (category.match(/^[\d_]/)) {
817 console.warn('Category starts with a number. Adding prefix "c_" to category name.')
818 category = 'c' + category;
819 }
820 // Replace all '__' with '_'
821 category = category.replace(/__/g, '_')
822
823 identifier = category;
824 if (this.options[this.VERSION])
825 identifier = identifier + '_' + this.options['version'];
826
827 let parameter_signature = '';
828 let outputs = nodeGraph.getOutputs();
829 for (let output of outputs) {
830 let outputType = output.getType();
831 parameter_signature = parameter_signature + '_' + outputType;
832 }
833 identifier = identifier + parameter_signature;
834 identifier = mx.createValidName(identifier)
835 // Replace all '__' with '_'
836 identifier = identifier.replace(/__/g, '_')
837
838 let nodeDefName = this.options[this.DEFINITION_PREFIX] + identifier;
839 let nodegraphName = this.options[this.NODEGRAPH_PREFIX] + identifier;
840 let uiName = this.options[this.UI_NAME];
841 let defaultVersion = this.options[this.DEFAULT_VERSION];
842 let nodeGroup = this.options[this.NODEGROUP];
843 let version = this.options[this.VERSION]
844
845 let definitionDoc = mx.createDocument();
846
847 // Note that the pre 1.39 equivalent was removed.
848 let definition = definitionDoc.addNodeDefFromGraph(nodeGraph, nodeDefName, category, nodegraphName)
849 if (version.length > 0)
850 {
851 definition.setVersionString(version);
852 }
853 if (defaultVersion)
854 definition.setDefaultVersion(defaultVersion);
855 if (nodeGroup.length > 0)
856 {
857 definition.setNodeGroup(nodeGroup);
858 }
859 let functionalGraph = definitionDoc.getNodeGraph(nodegraphName);
860
861 if (uiName.length)
862 {
863 uiName = this.sanitizeXMLString(uiName)
864 definition.setAttribute('uiname', uiName);
865 }
866
867 if (!embedNamespace && namespace.length > 0)
868 {
869 namespace = mx.createValidName(namespace)
870 definition.setNamespace(namespace);
871 functionalGraph.setNamespace(namespace);
872 // WARNING: Need to rename the nodedef reference
873 functionalGraph.setNodeDefString(namespace + ":" + functionalGraph.getNodeDefString())
874 }
875
876 let docString = this.options[this.DOCUMENTATION]
877 if (docString)
878 {
879 docString = this.sanitizeXMLString(docString)
880 definition.setDocString(docString)
881 functionalGraph.setDocString(docString)
882 }
883
884 /*
885 // Cleanup the result
886 let filterAttributes = ['nodegraph', 'nodename', 'channels', 'interfacename', 'xpos', 'ypos']
887
888 // Transfer input interface from the graph to the nodedef
889 for (let input of functionalGraph.getInputs()) {
890 let nodeDefInput = definition.addInput(input.getName(), input.getType())
891 if (nodeDefInput) {
892 nodeDefInput.copyContentFrom(input)
893 for (let filterAttribute of filterAttributes) {
894 nodeDefInput.removeAttribute(filterAttribute);
895 }
896 nodeDefInput.setSourceUri('')
897 input.setInterfaceName(nodeDefInput.getName())
898 }
899 }
900 for (let input of functionalGraph.getInputs()) {
901 functionalGraph.removeInput(input.getName())
902 }
903
904 for (let output of nodeGraph.getOutputs()) {
905 let nodeDefOutput = definition.getOutput(output.getName())
906 if (nodeDefOutput)
907 definition.removeOutput(output.getName())
908 definition.addOutput(output.getName(), output.getType())
909 if (nodeDefOutput)
910 nodeDefOutput.copyContentFrom(output)
911 for (let filterAttribute in filterAttributes)
912 nodeDefOutput.removeAttribute(filterAttribute)
913 nodeDefOutput.setSourceUri('')
914 }
915 for (let graphChild of functionalGraph.getChildren()) {
916 graphChild.removeAttribute('xpos');
917 graphChild.removeAttribute('ypos');
918 }
919 */
920 return definitionDoc;
921 }
sanitizeXMLString(docString)
Sanitize a string to be safe to use with XML / HTML.

◆ getDefaultOptions()

MxDefinitionCreator::getDefaultOptions ( )

Get the default options for definition creation.

Returns
Default options

Definition at line 734 of file JsMaterialXGraph.js.

734 {
735 let options = {};
736 options[this.DEFINITION_NAME] = '';
737 options[this.UI_NAME] = '';
738 options[this.VERSION] = '1.0';
739 options[this.DEFAULT_VERSION] = true;
740 options[this.NODEGROUP] = 'procedural';
741 options[this.DEFINITION_PREFIX] = 'ND_';
742 options[this.NODEGRAPH_PREFIX] = 'NG_';
743 options[this.DOCUMENTATION] = '';
744 options[this.NAMESPACE] = '';
745 return options;
746 }

◆ sanitizeXMLString()

MxDefinitionCreator::sanitizeXMLString ( docString)

Sanitize a string to be safe to use with XML / HTML.

Parameters
docStringOriginal string
Returns
Sanitized string

Definition at line 753 of file JsMaterialXGraph.js.

754 {
755 console.log('------------ sanitize: ', docString)
756 // Sanitize the string so it's valid for XML and HTML
757 // by replacing special characters with their HTML entities
758 //docString = docString.replace(/&/g, '&')
759 docString = docString.replace(/</g, '&lt;')
760 docString = docString.replace(/>/g, '&gt;')
761
762 // Remove any newlines
763 docString = docString.replace(/(\r\n|\n|\r)/gm, " ");
764
765 console.log('------------ sanitize 2: ', docString)
766 return docString;
767 }

◆ setOptions()

MxDefinitionCreator::setOptions ( new_options)

Set the options for definition creation.

Parameters
new_optionsOptions to set
Returns
{void}

Definition at line 774 of file JsMaterialXGraph.js.

775 {
776 //console.log('Set options:', new_options)
777 if (new_options) {
778 this.options = {};
779 for (let key in new_options) {
780 this.options[key] = new_options[key];
781 }
782 }
783 }

The documentation for this class was generated from the following file: