HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  virtual ~OP_ScriptOperator();
38 
39  static OP_ScriptOperator *createMantraScriptOp(const char *name,
40  const char *english);
41 
42  virtual bool hasLoadedParmTemplates() const;
43  virtual int updateParmTemplates();
44 
45  // To force our parm templates to be reloaded, we just reset our parm
46  // dialog time stamp, and call updateParmTemplates.
47  virtual void forceUpdateParmTemplates();
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  virtual const char *getOperatorShortHelpString();
118  virtual void getOperatorSpecificInfoText(int verbose,
119  UT_WorkBuffer &text);
120 
121  /// Fill in this info tree with details about this operator (similar to
122  /// getOperatorSpecificInfoText(..)
123  virtual void fillInfoTreeOperatorSpecific(UT_InfoTree &tree,
124  const OP_NodeInfoTreeParms &parms);
125  // Edit the script file. We check the script to see if it has any of the
126  // common object code file extensions. If it does, then we replace it with
127  // the extension given. The common object code extensions are:
128  // .slo, .so, .vex
129  // The object code file extensions array must be null terminated. If the
130  // file is not found, the context is used in the funciton declarator.
131  void editScriptFile(const char *pathprefix,
132  const char *context = "surface",
133  const char *extension = ".vfl",
134  const char *title = "VEX Function",
135  const char **obj_extensions = 0,
136  int localdir = 1);
137 
138  // If this node type uses Python to cook, this method will return the
139  // compiled version of that code. This method caches its results, so
140  // subsequent calls to it will avoid reloading and recompiling the
141  // code. This way, the sop can recook without reading from disk.
142  // When the node type is changed, the cache is cleared.
143  PY_CompiledCode *getCachedCompiledPythonCookCode();
144  bool isCompiledPythonCookCodeCached();
145 
146  virtual void clearOTLIndexFile();
147 
148  // Mark the parameter templates as dirty and send out notifications
149  // to interested parties that an update is required.
150  void dirtyParmTemplates();
151 
152  fpreal getUnitLength() const { return myUnitLength; }
153  fpreal getUnitMass() const { return myUnitMass; }
154 
155 protected:
156  OP_ScriptOperator( const char *name,
157  const char *english,
158  OP_Constructor construct,
159  PRM_Template *templates,
160  const char *child_table_name,
161  unsigned min_sources,
162  unsigned max_sources = 9999,
163  CH_LocalVariable *variables = 0,
164  unsigned flags = 0,
165  const char **inputlabels = 0,
166  int maxoutputs = 1);
167 
168  /// Obtains a string that specifies the definition source of this operator.
169  void getDefinitionSourceText( UT_String &defsource );
170 
171  virtual bool loadParmTemplatesSubclass();
172 
173  // Dummy virtuals that aren't implemented at this level.
174  virtual void removeAllBindings()
175  { }
176  virtual void refreshAllBindings()
177  { }
179  const char * /* filename */,
180  UT_String & /* scriptName */,
181  PRM_ScriptImports *& /* imports */)
182  { return 0; }
183 
184  // Use this method to free parm templates.
185  static void freeParmTemplates(PRM_Template *templates);
186 
187  // Use this method to free switcher defaults.
188  static void freeSwitcherDefaults(PRM_Default *defs);
189 
196  int myStartParm, myEndParm;
197 
198  // The following information (if set) is used to insert a folder into the
199  // specified switcher from myBase. myBaseSwitcherDefaults is allocated
200  // using malloc and must not be destroyed before myParmTemplates.
205 
206  // Format for generating cmd
207  unsigned mySyntax;
208 
209  // The info text that contains a short help string.
211 
214 
215  /// State var indicating if cached code is saved with HDA definition.
217 
218  // A table containing all the index files that get read by parseIndex.
220 };
221 
222 // ============================================================================
223 /// This is a helper class that parses the extra info from the HDA definition.
224 /// It can also write te extra info to the HDA definition.
226 {
227 public:
228  /// Constructor and destructor.
230  OP_ExtraInfoBuffer(const char *extra_info);
232 
233  /// Parses the extra info string.
234  /// After this call, you can use the query methods below.
235  void readExtraInfo(const char *extra_info);
236 
237  /// @{ Writes out the extra info state of this object into a string.
238  void writeExtraInfo(UT_WorkBuffer &extra_info);
239  void writeExtraInfo(UT_String &extra_info);
240  /// @}
241 
242  /// @{ Getters and setters.
243  bool foundKeywords() const
244  { return myIsKeywordFound; }
245 
246  bool getHideDefaultParms() const
247  { return myHideDefaultParms; }
248  void setHideDefaultParms(bool hide)
249  { myHideDefaultParms = hide; }
250 
252  { return myRepresentativeNodePath; }
253  void setRepresentativeNodePath(const char *path)
254  { myRepresentativeNodePath.harden(path); }
255 
257  { return myGuideNodePath; }
258  void setGuideNodePath(const char *path)
259  { myGuideNodePath.harden(path); }
260 
262  { return myOpSubType; }
263  void setOpSubType(const char *type)
264  { myOpSubType.harden(type); }
265 
267  { return myShaderType; }
268  void setShaderType(const char *type)
269  { myShaderType.harden(type); }
270 
272  { return myRenderMask; }
273  void setRenderMask(const char *type)
274  { myRenderMask.harden(type); }
275 
277  { return myVopnetMask; }
278  void setVopnetMask(const char *type)
279  { myVopnetMask.harden(type); }
280  /// @}
281 
282 protected:
283  // Resets the data in this class, we do this before we parse a new string.
284  void resetSettings();
285 
286 private:
287  bool myIsKeywordFound;
288  bool myHideDefaultParms;
289  UT_StringHolder myRepresentativeNodePath;
290  UT_StringHolder myGuideNodePath;
291  UT_StringHolder myOpSubType;
292  UT_StringHolder myShaderType;
293  UT_StringHolder myRenderMask;
294  UT_StringHolder myVopnetMask;
295 };
296 
297 // ============================================================================
298 /// Helper class for reading and writing FunctionName HDA section.
300 {
301 public:
302  /// @{ Reads from and writes to a file.
303  bool readFile(const FS_IndexFile &file);
304  void writeFile(FS_IndexFile &file) const;
305  /// @}
306 
307  /// @{ Reads from and writes to a string buffer.
308  void readBuffer(const char *str);
309  void writeBuffer(UT_WorkBuffer &str) const;
310  /// @}
311 
312  /// @{ The name of the main (shader) function HDA represents.
313  void setMainFunctionName(const char *name)
314  { myMainFunctionName = name; }
316  { return myMainFunctionName; }
317  /// @}
318 
319  /// @{ The dictionary of additional functions stored in HDA.
321  { mySecondaryFunctionsMap = m; }
323  { return mySecondaryFunctionsMap; }
324  /// @}
325 
326  /// Utility function to obtain the function code section names referenced
327  /// by a function info section.
328  static void getFunctionCodeSections(UT_StringArray &section_names,
329  const FS_IndexFile &file);
330 private:
331  void parseStringJSON(const char *str);
332  void writeStringJSON(UT_WorkBuffer &str) const;
333 
334 private:
335  /// Name of the function or a script represented by the HDA (eg, shader).
336  UT_StringHolder myMainFunctionName;
337 
338  /// List of other functions stored by the HDA, and a map to the
339  /// HDA section where the function implementation is stored.
340  UT_StringMap<UT_StringHolder> mySecondaryFunctionsMap;
341 };
342 
343 #endif
virtual void clearOTLIndexFile()
Helper class for reading and writing FunctionName HDA section.
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.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
const UT_StringHolder & getRenderMask() const
Getters and setters.
PY_CompiledCode * myCachedCompiledPythonCookCode
GLbitfield flags
Definition: glcorearb.h:1595
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 *&)
bool foundKeywords() const
Getters and setters.
virtual bool loadParmTemplatesSubclass()
void setHideDefaultParms(bool hide)
Getters and setters.
GLuint writeBuffer
Definition: glcorearb.h:2673
void setVopnetMask(const char *type)
Getters and setters.
virtual void refreshAllBindings()
const UT_StringHolder & getVopnetMask() const
Getters and setters.
fpreal getUnitLength() const
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()
GLboolean * data
Definition: glcorearb.h:130
virtual bool hasLoadedParmTemplates() const
GLuint const GLchar * name
Definition: glcorearb.h:785
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:88
int myHasCachedCode
State var indicating if cached code is saved with HDA definition.
void setShaderType(const char *type)
Getters and setters.
double fpreal
Definition: SYS_Types.h:263
Parameters for OP_Node::fillInfoTree()/OP_Node::fillInfoTreeNodeSpecific()
typedef int
Definition: png.h:1175
#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.
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
fpreal getUnitMass() const
virtual void getOperatorSpecificInfoText(int verbose, UT_WorkBuffer &text)
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.