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