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 let category = nodeGraph.getName();
798 if (this.options['definitionName']) {
799 category = this.options['definitionName'];
800 }
801
802 let identifier = category;
803 if (this.options[this.VERSION])
804 identifier = identifier + '_' + this.options['version'];
805
806 let parameter_signature = '';
807 let outputs = nodeGraph.getOutputs();
808 for (let output of outputs) {
809 let outputType = output.getType();
810 parameter_signature = parameter_signature + '_' + outputType;
811 }
812 identifier = identifier + parameter_signature;
813 identifier = mx.createValidName(identifier)
814
815 let nodeDefName = this.options[this.DEFINITION_PREFIX] + identifier;
816 let nodegraphName = this.options[this.NODEGRAPH_PREFIX] + identifier;
817 let uiName = this.options[this.UI_NAME];
818 let defaultVersion = this.options[this.DEFAULT_VERSION];
819 let nodeGroup = this.options[this.NODEGROUP];
820 let version = this.options[this.VERSION]
821 let namespace = this.options[this.NAMESPACE]
822
823 let definitionDoc = mx.createDocument();
824
825 // Note that the pre 1.39 equivalent was removed.
826 let definition = definitionDoc.addNodeDefFromGraph(nodeGraph, nodeDefName, category, nodegraphName)
827 if (version.length > 0)
828 {
829 definition.setVersionString(version);
830 }
831 if (defaultVersion)
832 definition.setDefaultVersion(defaultVersion);
833 if (nodeGroup.length > 0)
834 {
835 definition.setNodeGroup(nodeGroup);
836 }
837 let functionalGraph = definitionDoc.getNodeGraph(nodegraphName);
838
839 if (uiName.length)
840 {
841 uiName = this.sanitizeXMLString(uiName)
842 definition.setAttribute('uiname', uiName);
843 }
844
845 if (namespace.length > 0)
846 {
847 namespace = mx.createValidName(namespace)
848 definition.setNamespace(namespace);
849 functionalGraph.setNamespace(namespace);
850 // WARNING: Need to rename the nodedef reference
851 functionalGraph.setNodeDefString(namespace + ":" + functionalGraph.getNodeDefString())
852 }
853
854 let docString = this.options[this.DOCUMENTATION]
855 if (docString)
856 {
857 docString = this.sanitizeXMLString(docString)
858 definition.setDocString(docString)
859 functionalGraph.setDocString(docString)
860 console.log('get doc string>>>>>>>>>>>>>>>>>>>>:', definition.getDocString())
861 }
862
863 /*
864 // Cleanup the result
865 let filterAttributes = ['nodegraph', 'nodename', 'channels', 'interfacename', 'xpos', 'ypos']
866
867 // Transfer input interface from the graph to the nodedef
868 for (let input of functionalGraph.getInputs()) {
869 let nodeDefInput = definition.addInput(input.getName(), input.getType())
870 if (nodeDefInput) {
871 nodeDefInput.copyContentFrom(input)
872 for (let filterAttribute of filterAttributes) {
873 nodeDefInput.removeAttribute(filterAttribute);
874 }
875 nodeDefInput.setSourceUri('')
876 input.setInterfaceName(nodeDefInput.getName())
877 }
878 }
879 for (let input of functionalGraph.getInputs()) {
880 functionalGraph.removeInput(input.getName())
881 }
882
883 for (let output of nodeGraph.getOutputs()) {
884 let nodeDefOutput = definition.getOutput(output.getName())
885 if (nodeDefOutput)
886 definition.removeOutput(output.getName())
887 definition.addOutput(output.getName(), output.getType())
888 if (nodeDefOutput)
889 nodeDefOutput.copyContentFrom(output)
890 for (let filterAttribute in filterAttributes)
891 nodeDefOutput.removeAttribute(filterAttribute)
892 nodeDefOutput.setSourceUri('')
893 }
894 for (let graphChild of functionalGraph.getChildren()) {
895 graphChild.removeAttribute('xpos');
896 graphChild.removeAttribute('ypos');
897 }
898 */
899 return definitionDoc;
900 }
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: