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 PRM_ScriptParm;
26 class PRM_ScriptPage;
27 class PRM_ScriptImports;
28 class PRM_Name;
29 class DS_Stream;
30 class OP_Node;
31 
32 // Function to call for each parameter. Return 1 if successful, 0 if fail.
33 typedef int (*OP_ScriptParmFunc)(OP_Node *node, PRM_Template *temp,
34  int idx, fpreal t, void *data);
35 
37 {
38 public:
39  ~OP_ScriptOperator() override;
40 
41  static OP_ScriptOperator *createMantraScriptOp(const char *name,
42  const char *english);
43 
44  bool hasLoadedParmTemplates() const override;
45  int updateParmTemplates() override;
46 
47  // To force our parm templates to be reloaded, we just reset our parm
48  // dialog time stamp, and call updateParmTemplates.
49  void forceUpdateParmTemplates() override;
50 
51  /// Obtains the full name of the script provided by this operator.
52  /// It idendifies the script using an operator name, "opdef:"or "op:' path,
53  /// or using the script name (refering to a file on disk).
54  void getScriptName(UT_String &name,
55  bool forvex, bool for_python = false,
56  const char * section_name = NULL) const;
57 
58  /// Obtains the function (shader) name used for script.
59  /// It provieds the shader name that should be used in code.
60  void getScriptFunctionName(UT_String &name) const;
61 
62  /// Obtains the secondary function (shader) names (as keys) provided by this
63  /// operator, along with the section names (as mapped values) where
64  /// the function definition is stored.
65  void getScriptSecondaryFunctionsNames(
66  UT_StringMap<UT_StringHolder> &functions ) const;
67 
68  /// Tests if the script operator has a script section corresponding
69  /// to the given type. Returns true if operator is defined by an HDA
70  /// that a script section for the type. If the operator is not defined
71  /// by a library or the library definition has no such section, this method
72  /// returns false.
73  bool hasExplicitScriptSection(VEX_ContextType context_type);
74 
75  /// Returns true if the operator has cached code.
76  bool hasCachedCode();
77 
78 
79  void resetTemplate(OP_TemplatePair *pair);
80  // Build a command line for the script to be called with. This does all
81  // the channel evaluation and string expansion required. If there is an
82  // error, 0 is returned (otherwise 1). This function essentially calls
83  // OP_Parameters::buildVexCommand, using the right template list, and
84  // prefixing the script name.
85  int buildVexCommand(UT_String &result, OP_Node *node, fpreal now);
86 
87  // Build a index entry for our script operator:
88  void buildIndexEntry(UT_String &result, const UT_String &dsloc);
89 
90  // This will find all matching files in the correct Houdini path location
91  // and build a list of their contents.
92  // *NOTE* The caller will be responsible for cleaning up the
93  // definition array elements.
94  static int findScriptOperators(const char *path,
95  const char *filename,
96  OP_OTLDefinitionArray &defs);
97  // Get the table that lists all index files read by parseIndex.
98  static UT_StringSet &getAllIndexFiles();
99 
100  // This will traverse the parameters for the script and call traverseFunc
101  // for each. If there is an error, 0 is returned (otherwise 1).
102  int traverseCommand(OP_Node *node,
103  OP_ScriptParmFunc traverseFunc, void *data,
104  fpreal now);
105 
106  // Currently, only VEX is supported
107  enum {
108  OP_SYNTAX_VEX, // Vex syntax for building command
109  OP_SYNTAX_SHELL, // Shell syntax for building command
110  OP_SYNTAX_RMAN // Renderman syntax for building command
111  };
112 
113  // If the grammar of the dialog script needs to be changed, then the
114  // following routine can be used to handle tokens.
115  // It should return 1 if the token is handled, otherwise 0.
116  virtual int handleUnknownToken(DS_Stream &is, UT_String &token);
117 
118  UT_String &getScriptHelp() { return myHelp; }
119  const char *getOperatorShortHelpString() override;
121  int verbose,
122  UT_WorkBuffer &text) override;
123 
124  /// Fill in this info tree with details about this operator (similar to
125  /// getOperatorSpecificInfoText(..)
127  UT_InfoTree &tree,
128  const OP_NodeInfoTreeParms &parms) override;
129  // Edit the script file. We check the script to see if it has any of the
130  // common object code file extensions. If it does, then we replace it with
131  // the extension given. The common object code extensions are:
132  // .slo, .so, .vex
133  // The object code file extensions array must be null terminated. If the
134  // file is not found, the context is used in the funciton declarator.
135  void editScriptFile(const char *pathprefix,
136  const char *context = "surface",
137  const char *extension = ".vfl",
138  const char *title = "VEX Function",
139  const char **obj_extensions = 0,
140  int localdir = 1);
141 
142  // If this node type uses Python to cook, this method will return the
143  // compiled version of that code. This method caches its results, so
144  // subsequent calls to it will avoid reloading and recompiling the
145  // code. This way, the sop can recook without reading from disk.
146  // When the node type is changed, the cache is cleared.
147  PY_CompiledCode *getCachedCompiledPythonCookCode();
148  bool isCompiledPythonCookCodeCached();
149 
150  void clearOTLIndexFile() override;
151 
152  // Mark the parameter templates as dirty and send out notifications
153  // to interested parties that an update is required.
154  void dirtyParmTemplates();
155 
156  fpreal getUnitLength() const { return myUnitLength; }
157  fpreal getUnitMass() const { return myUnitMass; }
158 
159 protected:
160  OP_ScriptOperator( const char *name,
161  const char *english,
162  OP_Constructor construct,
163  PRM_Template *templates,
164  const char *child_table_name,
165  unsigned min_sources,
166  unsigned max_sources = 9999,
167  CH_LocalVariable *variables = 0,
168  unsigned flags = 0,
169  const char **inputlabels = 0,
170  int maxoutputs = 1);
171 
172  /// Obtains a string that specifies the definition source of this operator.
173  void getDefinitionSourceText( UT_String &defsource );
174 
175  bool loadParmTemplatesSubclass() override;
176 
177  // Dummy virtuals that aren't implemented at this level.
178  virtual void removeAllBindings()
179  { }
180  virtual void refreshAllBindings()
181  { }
183  const char * /* filename */,
184  UT_String & /* scriptName */,
185  PRM_ScriptImports *& /* imports */)
186  { return 0; }
187 
188  // Use this method to free parm templates.
189  static void freeParmTemplates(PRM_Template *templates);
190 
191  // Use this method to free switcher defaults.
192  static void freeSwitcherDefaults(PRM_Default *defs);
193 
200  int myStartParm, myEndParm;
201 
202  // The following information (if set) is used to insert a folder into the
203  // specified switcher from myBase. myBaseSwitcherDefaults is allocated
204  // using malloc and must not be destroyed before myParmTemplates.
209 
210  // Format for generating cmd
211  unsigned mySyntax;
212 
213  // The info text that contains a short help string.
215 
218 
219  /// State var indicating if cached code is saved with HDA definition.
221 
222  // A table containing all the index files that get read by parseIndex.
224 };
225 
226 // ============================================================================
227 /// This is a helper class that parses the extra info from the HDA definition.
228 /// It can also write te extra info to the HDA definition.
230 {
231 public:
232  /// Constructor and destructor.
234  OP_ExtraInfoBuffer(const char *extra_info);
236 
237  /// Parses the extra info string.
238  /// After this call, you can use the query methods below.
239  void readExtraInfo(const char *extra_info);
240 
241  /// @{ Writes out the extra info state of this object into a string.
242  void writeExtraInfo(UT_WorkBuffer &extra_info);
243  void writeExtraInfo(UT_String &extra_info);
244  /// @}
245 
246  /// @{ Getters and setters.
247  bool foundKeywords() const
248  { return myIsKeywordFound; }
249 
250  bool getHideDefaultParms() const
251  { return myHideDefaultParms; }
252  void setHideDefaultParms(bool hide)
253  { myHideDefaultParms = hide; }
254 
256  { return myRepresentativeNodePath; }
257  void setRepresentativeNodePath(const char *path)
258  { myRepresentativeNodePath.harden(path); }
259 
261  { return myGuideNodePath; }
262  void setGuideNodePath(const char *path)
263  { myGuideNodePath.harden(path); }
264 
266  { return myOpSubType; }
267  void setOpSubType(const char *type)
268  { myOpSubType.harden(type); }
269 
271  { return myShaderType; }
272  void setShaderType(const char *type)
273  { myShaderType.harden(type); }
274 
276  { return myRenderMask; }
277  void setRenderMask(const char *type)
278  { myRenderMask.harden(type); }
279 
281  { return myVopnetMask; }
282  void setVopnetMask(const char *type)
283  { myVopnetMask.harden(type); }
284 
285  const UT_StringHolder & getPDGType() const
286  { return myPDGType; }
287  void setPDGType(const char *type)
288  { myPDGType.harden(type); }
289  /// @}
290 
291 protected:
292  // Resets the data in this class, we do this before we parse a new string.
293  void resetSettings();
294 
295 private:
296  bool myIsKeywordFound;
297  bool myHideDefaultParms;
298  UT_StringHolder myRepresentativeNodePath;
299  UT_StringHolder myGuideNodePath;
300  UT_StringHolder myOpSubType;
301  UT_StringHolder myShaderType;
302  UT_StringHolder myRenderMask;
303  UT_StringHolder myVopnetMask;
304  UT_StringHolder myPDGType;
305 };
306 
308 {
309 public:
310  OP_ExtraInfoBuffer2() = default;
311  OP_ExtraInfoBuffer2(const char *extra_info)
312  { readExtraInfo(extra_info); }
313 
314  /// Parses the extra info string.
315  /// After this call, you can use the query methods below.
316  void readExtraInfo(const char *extra_info);
317 
318  /// @{ Writes out the extra info state of this object into a string.
319  void writeExtraInfo(UT_WorkBuffer &extra_info);
320  void writeExtraInfo(UT_String &extra_info);
321  /// @}
322 
323  /// True if known keywords were parsed
324  bool foundKeywords() const
325  { return (myOptions.getNumOptions() > 0); }
326 
327 #define OP_EXTRA_ACCESSOR_BOOL(NAME, TOKEN) \
328  const char * get##NAME##Token() const { return TOKEN; } \
329  bool get##NAME() const \
330  { return myOptions.getOptionB(get##NAME##Token()); } \
331  void set##NAME(bool value) \
332  { myOptions.setOptionB(get##NAME##Token(), value); } \
333  /**/
334 #define OP_EXTRA_ACCESSOR_STRING(NAME, TOKEN) \
335  const char * get##NAME##Token() const { return TOKEN; } \
336  const UT_StringHolder & \
337  get##NAME() const \
338  { return myOptions.getOptionS(get##NAME##Token()); } \
339  void set##NAME(const UT_StringHolder &value) \
340  { myOptions.setOptionSRaw(get##NAME##Token(),value); } \
341  /**/
342 
343  OP_EXTRA_ACCESSOR_BOOL (HideDefaultParms, "hidedefaultparms")
344  OP_EXTRA_ACCESSOR_BOOL (HasObjExtParms, "objextparms")
345  OP_EXTRA_ACCESSOR_STRING(RepresentativeNodePath, "rep")
346  OP_EXTRA_ACCESSOR_STRING(GuideNodePath, "guide")
347  OP_EXTRA_ACCESSOR_STRING(OpSubType, "subtype")
348  OP_EXTRA_ACCESSOR_STRING(ShaderType, "shadertype")
349  OP_EXTRA_ACCESSOR_STRING(RenderMask, "rendermask")
350  OP_EXTRA_ACCESSOR_STRING(VopnetMask, "vopnetmask")
351  OP_EXTRA_ACCESSOR_STRING(PDGType, "pdgtype")
352 
353 #undef OP_EXTRA_ACCESSOR_STRING
354 #undef OP_EXTRA_ACCESSOR_BOOL
355 
356 private:
357  UT_Options myOptions;
358 };
359 
360 // ============================================================================
361 /// Helper class for reading and writing FunctionName HDA section.
363 {
364 public:
365  /// @{ Reads from and writes to a file.
366  bool readFile(const FS_IndexFile &file);
367  void writeFile(FS_IndexFile &file) const;
368  /// @}
369 
370  /// @{ Reads from and writes to a string buffer.
371  void readBuffer(const char *str);
372  void writeBuffer(UT_WorkBuffer &str) const;
373  /// @}
374 
375  /// @{ The name of the main (shader) function HDA represents.
376  void setMainFunctionName(const char *name)
377  { myMainFunctionName = name; }
379  { return myMainFunctionName; }
380  /// @}
381 
382  /// @{ The dictionary of additional functions stored in HDA.
384  { mySecondaryFunctionsMap = m; }
386  { return mySecondaryFunctionsMap; }
387  /// @}
388 
389  /// Utility function to obtain the function code section names referenced
390  /// by a function info section.
391  static void getFunctionCodeSections(UT_StringArray &section_names,
392  const FS_IndexFile &file);
393 private:
394  void parseStringJSON(const char *str);
395  void writeStringJSON(UT_WorkBuffer &str) const;
396 
397 private:
398  /// Name of the function or a script represented by the HDA (eg, shader).
399  UT_StringHolder myMainFunctionName;
400 
401  /// List of other functions stored by the HDA, and a map to the
402  /// HDA section where the function implementation is stored.
403  UT_StringMap<UT_StringHolder> mySecondaryFunctionsMap;
404 };
405 
406 #endif
#define OP_EXTRA_ACCESSOR_STRING(NAME, TOKEN)
virtual void clearOTLIndexFile()
Helper class for reading and writing FunctionName HDA section.
GT_API const UT_StringHolder filename
GLuint writeBuffer
Definition: glew.h:4033
GLuint const GLchar * name
Definition: glew.h:1814
virtual int updateParmTemplates()
void setSecondaryFunctionsMap(const UT_StringMap< UT_StringHolder > &m)
The dictionary of additional functions stored in HDA.
void setRenderMask(const char *type)
Getters and setters.
PRM_Default * myBaseSwitcherDefaults
const UT_StringHolder & getOpSubType() const
Getters and setters.
GLenum GLsizei GLsizei GLsizei GLsizei GLbitfield flags
Definition: glew.h:2864
const UT_StringHolder & getRenderMask() const
Getters and setters.
PY_CompiledCode * myCachedCompiledPythonCookCode
const GLdouble * m
Definition: glew.h:9124
void setOpSubType(const char *type)
Getters and setters.
virtual void removeAllBindings()
const UT_StringHolder & getRepresentativeNodePath() const
Getters and setters.
bool foundKeywords() const
True if known keywords were parsed.
const UT_StringHolder & getGuideNodePath() const
Getters and setters.
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 *&)
const UT_StringHolder & getPDGType() const
Getters and setters.
bool foundKeywords() const
Getters and setters.
OIIO_API std::string extension(const std::string &filepath, bool include_dot=true)
void setPDGType(const char *type)
Getters and setters.
virtual bool loadParmTemplatesSubclass()
void setHideDefaultParms(bool hide)
Getters and setters.
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
void setVopnetMask(const char *type)
Getters and setters.
virtual void refreshAllBindings()
const UT_StringHolder & getVopnetMask() const
Getters and setters.
fpreal getUnitLength() const
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
const UT_StringHolder & getShaderType() const
Getters and setters.
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()
GLenum shadertype
Definition: glew.h:6383
virtual bool hasLoadedParmTemplates() const
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
virtual bool writeFile(const GA_Detail &g, const char *filename, const GA_SaveOptions *opts, UT_StringArray *errors) const
OP_Node *(* OP_Constructor)(OP_Network *, const char *, OP_Operator *)
Definition: OP_Operator.h:104
int myHasCachedCode
State var indicating if cached code is saved with HDA definition.
void setShaderType(const char *type)
Getters and setters.
GLsizei const GLchar *const * path
Definition: glew.h:6461
A map of string to various well defined value types.
Definition: UT_Options.h:84
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()
bool getHideDefaultParms() const
Getters and setters.
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)
GLuint64EXT * result
Definition: glew.h:14007
void setRepresentativeNodePath(const char *path)
Getters and setters.
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.
OP_ExtraInfoBuffer2(const char *extra_info)
void setGuideNodePath(const char *path)
Getters and setters.
GLdouble GLdouble t
Definition: glew.h:1398
#define OP_EXTRA_ACCESSOR_BOOL(NAME, TOKEN)