HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OP_ScriptOperator.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: OP_ScriptOperator.h ( OP Library, C++)
7  *
8  * COMMENTS: Reads an OP definition from a script to generate the parm
9  * template, etc.
10  */
11 
12 #ifndef __OP_ScriptOperator__
13 #define __OP_ScriptOperator__
14 
15 #include "OP_API.h"
16 #include "OP_Operator.h"
17 #include <UT/UT_Lock.h>
18 #include <UT/UT_Options.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_StringMap.h>
21 #include <SYS/SYS_Types.h>
22 #include <sys/types.h>
23 
24 class UT_InfoTree;
25 class PY_CompiledCode;
26 class PRM_ScriptParm;
27 class PRM_ScriptPage;
28 class PRM_ScriptImports;
29 class PRM_Name;
30 class DS_Stream;
31 class OP_Node;
32 
33 // Function to call for each parameter. Return 1 if successful, 0 if fail.
34 typedef int (*OP_ScriptParmFunc)(OP_Node *node, PRM_Template *temp,
35  int idx, fpreal t, void *data);
36 
38 {
39 public:
40  ~OP_ScriptOperator() override;
41 
42  static OP_ScriptOperator *createMantraScriptOp(const char *name,
43  const char *english);
44 
45  bool hasLoadedParmTemplates() const override;
46  int updateParmTemplates() override;
47 
48  // To force our parm templates to be reloaded, we just reset our parm
49  // dialog time stamp, and call updateParmTemplates.
50  void forceUpdateParmTemplates() override;
51 
52  /// Obtains the full name of the script provided by this operator.
53  /// It idendifies the script using an operator name, "opdef:"or "op:' path,
54  /// or using the script name (refering to a file on disk).
55  void getScriptName(UT_String &name,
56  bool forvex, bool for_python = false,
57  const char * section_name = NULL) const;
58 
59  /// Obtains the function (shader) name used for script.
60  /// It provieds the shader name that should be used in code.
61  void getScriptFunctionName(UT_String &name) const;
62 
63  /// An operator may have several signatures for a shader, and use
64  /// separate shader function name for each of them.
65  /// This method returns such shader function names.
66  virtual UT_StringArray getScriptSignatureFunctions() const;
67 
68  /// Obtains the secondary function (shader) names (as keys) provided by this
69  /// operator, along with the section names (as mapped values) where
70  /// the function definition is stored.
71  void getScriptSecondaryFunctionsNames(
72  UT_StringMap<UT_StringHolder> &functions ) const;
73 
74  /// Tests if the script operator has a script section corresponding
75  /// to the given type. Returns true if operator is defined by an HDA
76  /// that a script section for the type. If the operator is not defined
77  /// by a library or the library definition has no such section, this method
78  /// returns false.
79  bool hasExplicitScriptSection(VEX_ContextType context_type);
80 
81  /// Returns true if the operator has cached code.
82  bool hasCachedCode();
83 
84 
85  void resetTemplate(OP_TemplatePair *pair);
86  // Build a command line for the script to be called with. This does all
87  // the channel evaluation and string expansion required. If there is an
88  // error, 0 is returned (otherwise 1). This function essentially calls
89  // OP_Parameters::buildVexCommand, using the right template list, and
90  // prefixing the script name.
91  int buildVexCommand(UT_String &result, OP_Node *node, fpreal now);
92 
93  // Build a index entry for our script operator:
94  void buildIndexEntry(UT_String &result, const UT_String &dsloc);
95 
96  // This will find all matching files in the correct Houdini path location
97  // and build a list of their contents.
98  // *NOTE* The caller will be responsible for cleaning up the
99  // definition array elements.
100  static int findScriptOperators(const char *path,
101  const char *filename,
102  OP_OTLDefinitionArray &defs);
103  // Get the table that lists all index files read by parseIndex.
104  static UT_StringSet &getAllIndexFiles();
105 
106  // This will traverse the parameters for the script and call traverseFunc
107  // for each. If there is an error, 0 is returned (otherwise 1).
108  int traverseCommand(OP_Node *node,
109  OP_ScriptParmFunc traverseFunc, void *data,
110  fpreal now);
111 
112  // Currently, only VEX is supported
113  enum {
114  OP_SYNTAX_VEX, // Vex syntax for building command
115  OP_SYNTAX_SHELL, // Shell syntax for building command
116  OP_SYNTAX_RMAN // Renderman syntax for building command
117  };
118 
119  // If the grammar of the dialog script needs to be changed, then the
120  // following routine can be used to handle tokens.
121  // It should return 1 if the token is handled, otherwise 0.
122  virtual int handleUnknownToken(DS_Stream &is, UT_String &token);
123 
124  UT_String &getScriptHelp() { return myHelp; }
125  const char *getOperatorShortHelpString() override;
127  int verbose,
128  UT_WorkBuffer &text) override;
129 
130  /// Fill in this info tree with details about this operator (similar to
131  /// getOperatorSpecificInfoText(..)
133  UT_InfoTree &tree,
134  const OP_NodeInfoTreeParms &parms) override;
135  // Edit the script file. We check the script to see if it has any of the
136  // common object code file extensions. If it does, then we replace it with
137  // the extension given. The common object code extensions are:
138  // .slo, .so, .vex
139  // The object code file extensions array must be null terminated. If the
140  // file is not found, the context is used in the funciton declarator.
141  void editScriptFile(const char *pathprefix,
142  const char *context = "surface",
143  const char *extension = ".vfl",
144  const char *title = "VEX Function",
145  const char **obj_extensions = 0,
146  int localdir = 1);
147 
148  // If this node type uses Python to cook, this method will return the
149  // compiled version of that code. This method caches its results, so
150  // subsequent calls to it will avoid reloading and recompiling the
151  // code. This way, the sop can recook without reading from disk.
152  // When the node type is changed, the cache is cleared.
153  PY_CompiledCode *getCachedCompiledPythonCookCode();
154  bool isCompiledPythonCookCodeCached();
155 
156  void clearOTLIndexFile() override;
157 
158  // Mark the parameter templates as dirty and send out notifications
159  // to interested parties that an update is required.
160  void dirtyParmTemplates();
161 
162  fpreal getUnitLength() const { return myUnitLength; }
163  fpreal getUnitMass() const { return myUnitMass; }
164 
165 protected:
166  OP_ScriptOperator( const char *name,
167  const char *english,
168  OP_Constructor construct,
169  PRM_Template *templates,
170  const char *child_table_name,
171  unsigned min_sources,
172  unsigned max_sources = 9999,
173  CH_LocalVariable *variables = 0,
174  unsigned flags = 0,
175  const char **inputlabels = 0,
176  int maxoutputs = 1);
177 
178  /// Obtains a string that specifies the definition source of this operator.
179  void getDefinitionSourceText( UT_String &defsource );
180 
181  bool loadParmTemplatesSubclass() override;
182 
183  // Dummy virtuals that aren't implemented at this level.
184  virtual void removeAllBindings()
185  { }
186  virtual void refreshAllBindings()
187  { }
189  const char * /* filename */,
190  UT_String & /* scriptName */,
191  PRM_ScriptImports *& /* imports */)
192  { return 0; }
193 
194  // Use this method to free parm templates.
195  static void freeParmTemplates(PRM_Template *templates);
196 
197  // Use this method to free switcher defaults.
198  static void freeSwitcherDefaults(PRM_Default *defs);
199 
206  int myStartParm, myEndParm;
207 
208  // The following information (if set) is used to insert a folder into the
209  // specified switcher from myBase. myBaseSwitcherDefaults is allocated
210  // using malloc and must not be destroyed before myParmTemplates.
215 
216  // Format for generating cmd
217  unsigned mySyntax;
218 
219  // The info text that contains a short help string.
221 
224 
225  /// State var indicating if cached code is saved with HDA definition.
227 
228  // A table containing all the index files that get read by parseIndex.
230 };
231 
232 // ============================================================================
233 /// Helper class for reading and writing FunctionName HDA section.
235 {
236 public:
237  /// @{ Reads from and writes to a file.
238  bool readFile(const FS_IndexFile &file);
239  void writeFile(FS_IndexFile &file) const;
240  /// @}
241 
242  /// @{ Reads from and writes to a string buffer.
243  void readBuffer(const char *str);
244  void writeBuffer(UT_WorkBuffer &str) const;
245  /// @}
246 
247  /// @{ The name of the main (shader) function HDA represents.
248  void setMainFunctionName(const char *name)
249  { myMainFunctionName = name; }
251  { return myMainFunctionName; }
252  /// @}
253 
254  /// @{ The dictionary of additional functions stored in HDA.
256  { mySecondaryFunctionsMap = m; }
258  { return mySecondaryFunctionsMap; }
259  /// @}
260 
261  /// Utility function to obtain the function code section names referenced
262  /// by a function info section.
263  static void getFunctionCodeSections(UT_StringArray &section_names,
264  const FS_IndexFile &file);
265 private:
266  void parseStringJSON(const char *str);
267  void writeStringJSON(UT_WorkBuffer &str) const;
268 
269 private:
270  /// Name of the function or a script represented by the HDA (eg, shader).
271  UT_StringHolder myMainFunctionName;
272 
273  /// List of other functions stored by the HDA, and a map to the
274  /// HDA section where the function implementation is stored.
275  UT_StringMap<UT_StringHolder> mySecondaryFunctionsMap;
276 };
277 
278 #endif
virtual void clearOTLIndexFile()
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLbitfield flags
Definition: glcorearb.h:1596
std::function< OP_Ptr(OP_Network *, const char *, OP_Operator *)> OP_Constructor
Definition: OP_Operator.h:107
Helper class for reading and writing FunctionName HDA section.
GT_API const UT_StringHolder filename
virtual int updateParmTemplates()
void setSecondaryFunctionsMap(const UT_StringMap< UT_StringHolder > &m)
The dictionary of additional functions stored in HDA.
PRM_Default * myBaseSwitcherDefaults
GLboolean * data
Definition: glcorearb.h:131
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
PY_CompiledCode * myCachedCompiledPythonCookCode
**But if you need a result
Definition: thread.h:613
virtual void removeAllBindings()
const UT_StringHolder & getMainFunctionName() const
The name of the main (shader) function HDA represents.
static UT_StringSet theAllIndexFiles
PRM_ScriptPage * myPage
virtual PRM_Template * loadParmTemplates(UT_IStream &, const char *, UT_String &, PRM_ScriptImports *&)
virtual bool loadParmTemplatesSubclass()
GLuint writeBuffer
Definition: glcorearb.h:2674
virtual void refreshAllBindings()
fpreal getUnitLength() const
virtual void fillInfoTreeOperatorSpecific(UT_InfoTree &tree, const OP_NodeInfoTreeParms &parms)
const UT_StringMap< UT_StringHolder > & getSecondaryFunctionsMap() const
The dictionary of additional functions stored in HDA.
UT_String & getScriptHelp()
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual bool hasLoadedParmTemplates() const
GLdouble t
Definition: glad.h:2397
virtual bool writeFile(const GA_Detail &g, const char *filename, const GA_SaveOptions *opts, UT_StringArray *errors) const
int myHasCachedCode
State var indicating if cached code is saved with HDA definition.
Parameters for OP_Node::fillInfoTree()/OP_Node::fillInfoTreeNodeSpecific()
fpreal64 fpreal
Definition: SYS_Types.h:277
#define OP_API
Definition: OP_API.h:10
UT_Lock myCachedCompiledPythonCookLock
virtual const char * getOperatorShortHelpString()
virtual bool readFile(GA_Detail &g, const char *filename, const GA_LoadOptions *opts, UT_StringArray *errors) const
Class which defines an I/O interface to save/load geometry.
fpreal getUnitMass() const
virtual void getOperatorSpecificInfoText(int verbose, UT_WorkBuffer &text)
virtual void forceUpdateParmTemplates()
int(* OP_ScriptParmFunc)(OP_Node *node, PRM_Template *temp, int idx, fpreal t, void *data)
VEX_ContextType
Definition: VEX_VexTypes.h:60
void setMainFunctionName(const char *name)
The name of the main (shader) function HDA represents.
OIIO_UTIL_API std::string extension(string_view filepath, bool include_dot=true) noexcept
Definition: format.h:895