OpenVDB  8.1.1
ParmFactory.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
9 
10 #ifndef HOUDINI_UTILS_PARM_FACTORY_HAS_BEEN_INCLUDED
11 #define HOUDINI_UTILS_PARM_FACTORY_HAS_BEEN_INCLUDED
12 
13 #include <GA/GA_Attribute.h>
14 #include <OP/OP_AutoLockInputs.h>
15 #include <OP/OP_Operator.h>
16 #include <PRM/PRM_Include.h>
17 #include <PRM/PRM_SpareData.h>
18 #include <SOP/SOP_Node.h>
19 #include <SOP/SOP_NodeVerb.h>
20 #if defined(PRODDEV_BUILD) || defined(DWREAL_IS_DOUBLE)
21  // OPENVDB_HOUDINI_API, which has no meaning in a DWA build environment but
22  // must at least exist, is normally defined by including openvdb/Platform.h.
23  // For DWA builds (i.e., if either PRODDEV_BUILD or DWREAL_IS_DOUBLE exists),
24  // that introduces an unwanted and unnecessary library dependency.
25  #ifndef OPENVDB_HOUDINI_API
26  #define OPENVDB_HOUDINI_API
27  #endif
28 #else
29  #include <openvdb/version.h>
30 #endif
31 #include <exception>
32 #include <functional>
33 #include <map>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 
39 #ifdef SESI_OPENVDB
40  #ifdef OPENVDB_HOUDINI_API
41  #undef OPENVDB_HOUDINI_API
42  #define OPENVDB_HOUDINI_API
43  #endif
44 #endif
45 
46 
47 class GU_Detail;
48 class OP_OperatorTable;
49 class PRM_Parm;
50 
51 namespace houdini_utils {
52 
53 class ParmFactory;
54 
55 using SpareDataMap = std::map<std::string, std::string>;
56 
61 const SpareDataMap& getOperatorSpareData(const OP_Operator&);
62 
69 void addOperatorSpareData(OP_Operator&, const SpareDataMap&);
70 
71 
74 {
75 public:
76  using PrmTemplateVec = std::vector<PRM_Template>;
77 
78  ParmList() {}
79 
81  bool empty() const { return mParmVec.empty(); }
85  size_t size() const { return mParmVec.size(); }
86 
88  void clear() { mParmVec.clear(); mSwitchers.clear(); }
89 
92  ParmList& add(const PRM_Template&);
93  ParmList& add(const ParmFactory&);
95 
98  ParmList& beginSwitcher(const std::string& token, const std::string& label = "");
101  ParmList& beginExclusiveSwitcher(const std::string& token, const std::string& label = "");
105  ParmList& endSwitcher();
106 
111  ParmList& addFolder(const std::string& label);
112 
114  PRM_Template* get() const;
115 
116 private:
117  struct SwitcherInfo { size_t parmIdx; std::vector<PRM_Default> folders; bool exclusive; };
118  using SwitcherStack = std::vector<SwitcherInfo>;
119 
120  void incFolderParmCount();
121  SwitcherInfo* getCurrentSwitcher();
122 
123  PrmTemplateVec mParmVec;
124  SwitcherStack mSwitchers;
125 }; // class ParmList
126 
127 
129 
130 
148 {
149 public:
150  ParmFactory(PRM_Type, const std::string& token, const std::string& label);
151  ParmFactory(PRM_MultiType, const std::string& token, const std::string& label);
152 
153  // Settings
154  ParmFactory& setCallbackFunc(const PRM_Callback&);
155 
157  ParmFactory& setChoiceList(const PRM_ChoiceList*);
162  ParmFactory& setChoiceListItems(PRM_ChoiceListType typ, const std::vector<std::string>& items);
168  ParmFactory& setChoiceListItems(PRM_ChoiceListType typ, const char* const* items);
169 
190  ParmFactory& setGroupChoiceList(size_t inputIndex,
191  PRM_ChoiceListType typ = PRM_CHOICELIST_TOGGLE);
192 
198  using AttrFilterFunc =
199  std::function<bool (const GA_Attribute&, const PRM_Parm&, const SOP_Node&)>;
200 
226  ParmFactory& setAttrChoiceList(size_t inputIndex, GA_AttributeOwner attrOwner,
227  PRM_ChoiceListType typ = PRM_CHOICELIST_TOGGLE,
228  AttrFilterFunc attrFilter = AttrFilterFunc{});
229 
230 
231 #if defined(GCC3)
232  #define IS_DEPRECATED __attribute__ ((deprecated))
233 #elif defined(_MSC_VER)
234  #define IS_DEPRECATED __declspec(deprecated)
235 #else
236  #define IS_DEPRECATED
237 #endif
238 
248  IS_DEPRECATED ParmFactory& setChoiceList(PRM_ChoiceListType typ,
249  const std::vector<std::string>& items, bool paired = false);
260  IS_DEPRECATED ParmFactory& setChoiceList(PRM_ChoiceListType typ,
261  const char* const* items, bool paired = false);
262 
263 #undef IS_DEPRECATED
264 
265  ParmFactory& setConditional(const PRM_ConditionalBase*);
266 
271  ParmFactory& setDefault(fpreal, const char* = nullptr, CH_StringMeaning = CH_STRING_LITERAL);
273  ParmFactory& setDefault(const std::string&, CH_StringMeaning = CH_STRING_LITERAL);
277  ParmFactory& setDefault(const std::vector<fpreal>&);
280  ParmFactory& setDefault(const std::vector<PRM_Default>&);
282  ParmFactory& setDefault(const PRM_Default*);
283 
286  ParmFactory& setHelpText(const char*);
289  ParmFactory& setTooltip(const char*);
296  ParmFactory& setDocumentation(const char*);
297 
298  ParmFactory& setParmGroup(int);
299 
301  ParmFactory& setRange(
302  PRM_RangeFlag minFlag, fpreal minVal,
303  PRM_RangeFlag maxFlag, fpreal maxVal);
306  ParmFactory& setRange(const std::vector<PRM_Range>&);
308  ParmFactory& setRange(const PRM_Range*);
309 
311  ParmFactory& setSpareData(const SpareDataMap&);
313  ParmFactory& setSpareData(const PRM_SpareData*);
314 
318  ParmFactory& setMultiparms(const ParmList&);
319 
321  ParmFactory& setTypeExtended(PRM_TypeExtended);
322 
325  ParmFactory& setVectorSize(int);
326 
331  ParmFactory& setInvisible();
332 
334  PRM_Template get() const;
335 
336 private:
337  struct Impl;
338  std::shared_ptr<Impl> mImpl;
339 
340  // For internal use only, and soon to be removed:
341  ParmFactory& doSetChoiceList(PRM_ChoiceListType, const std::vector<std::string>&, bool);
342  ParmFactory& doSetChoiceList(PRM_ChoiceListType, const char* const* items, bool);
343 }; // class ParmFactory
344 
345 
347 
348 
349 class OpPolicy;
350 using OpPolicyPtr = std::shared_ptr<OpPolicy>;
351 
352 
377 {
378 public:
379  enum OpFlavor { SOP, POP, ROP, VOP, HDA };
380 
383  static std::string flavorToString(OpFlavor);
384 
395  template<typename OpPolicyType>
396  OpFactory(const OpPolicyType& /*unused*/, const std::string& english,
397  OP_Constructor ctor, ParmList& parms, OP_OperatorTable& table, OpFlavor flavor = SOP)
398  {
399  this->init(OpPolicyPtr(new OpPolicyType), english, ctor, parms, table, flavor);
400  }
401 
403  OpFactory(const std::string& english, OP_Constructor ctor,
404  ParmList& parms, OP_OperatorTable& table, OpFlavor flavor = SOP);
405 
407 #if OPENVDB_ABI_VERSION_NUMBER >= 7
408  virtual ~OpFactory();
409 #else
410  ~OpFactory();
411 #endif
412 
413  OpFactory(const OpFactory&) = delete;
414  OpFactory& operator=(const OpFactory&) = delete;
415 
418  OpFlavor flavor() const;
421  std::string flavorString() const;
424  const std::string& name() const;
427  const std::string& english() const;
430  const std::string& iconName() const;
435  const std::string& helpURL() const;
439  const std::string& documentation() const;
442  const OP_OperatorTable& table() const;
443 
448  OpFactory& addAlias(const std::string& english);
453  OpFactory& addAliasVerbatim(const std::string& name);
458  OpFactory& setDocumentation(const std::string&);
460  OpFactory& addInput(const std::string& name);
462  OpFactory& addOptionalInput(const std::string& name);
466  OpFactory& setMaxInputs(unsigned = 9999);
468  OpFactory& setObsoleteParms(const ParmList&);
470  OpFactory& setLocalVariables(CH_LocalVariable*);
471  OpFactory& setFlags(unsigned);
472  OpFactory& setInternalName(const std::string& name);
473  OpFactory& setOperatorTable(const std::string& name);
474 
479  using CacheAllocFunc = std::function<SOP_NodeCache* (void)>;
480 
489  OpFactory& setVerb(SOP_NodeVerb::CookMode cookMode, const CacheAllocFunc& allocator);
490 
493  OpFactory& setInvisible();
494 
500  OpFactory& addSpareData(const SpareDataMap&);
501 
502 protected:
505  OP_OperatorTable& table();
506 
507 private:
508  void init(OpPolicyPtr, const std::string& english, OP_Constructor,
509  ParmList&, OP_OperatorTable&, OpFlavor);
510 
511  struct Impl;
512  std::shared_ptr<Impl> mImpl;
513 }; // class OpFactory
514 
515 
517 
518 
522 {
523 public:
524  OpPolicy() {}
525  virtual ~OpPolicy() {}
526 
528  std::string getName(const OpFactory& factory) { return getName(factory, factory.english()); }
529 
537  virtual std::string getName(const OpFactory& factory, const std::string& english);
538 
541  virtual std::string getIconName(const OpFactory&) { return ""; }
542 
544  virtual std::string getHelpURL(const OpFactory&) { return ""; }
545 
549  virtual std::string getLabelName(const OpFactory&);
550 
553  virtual std::string getFirstName(const OpFactory&) { return ""; }
554 
557  virtual std::string getTabSubMenuPath(const OpFactory&) { return ""; }
558 };
559 
560 
562 
563 
566 {
567 public:
568  ScopedInputLock(SOP_Node& node, OP_Context& context)
569  {
570  mLock.setNode(&node);
571  if (mLock.lock(context) >= UT_ERROR_ABORT) {
572  throw std::runtime_error("failed to lock inputs");
573  }
574  }
576 
577  void markInputUnlocked(exint input) { mLock.markInputUnlocked(input); }
578 
579 private:
580  OP_AutoLockInputs mLock;
581 };
582 
583 
585 
586 
587 // Extended group name drop-down menu incorporating "@<attr>=<value" syntax
588 
589 OPENVDB_HOUDINI_API extern const PRM_ChoiceList PrimGroupMenuInput1;
590 OPENVDB_HOUDINI_API extern const PRM_ChoiceList PrimGroupMenuInput2;
591 OPENVDB_HOUDINI_API extern const PRM_ChoiceList PrimGroupMenuInput3;
592 OPENVDB_HOUDINI_API extern const PRM_ChoiceList PrimGroupMenuInput4;
593 
597 OPENVDB_HOUDINI_API extern const PRM_ChoiceList PrimGroupMenu;
598 
599 
600 } // namespace houdini_utils
601 
602 #endif // HOUDINI_UTILS_PARM_FACTORY_HAS_BEEN_INCLUDED
OPENVDB_HOUDINI_API const PRM_ChoiceList PrimGroupMenuInput3
ParmList()
Definition: ParmFactory.h:78
OPENVDB_HOUDINI_API const PRM_ChoiceList PrimGroupMenuInput1
virtual std::string getTabSubMenuPath(const OpFactory &)
Return the tab sub-menu path of the op.
Definition: ParmFactory.h:557
OpFlavor
Definition: ParmFactory.h:379
virtual std::string getFirstName(const OpFactory &)
Return the inital default name of the operator.
Definition: ParmFactory.h:553
virtual std::string getIconName(const OpFactory &)
Return an icon name for the operator defined by the given factory.
Definition: ParmFactory.h:541
Definition: ParmFactory.h:51
std::map< std::string, std::string > SpareDataMap
Definition: ParmFactory.h:55
Helper class to simplify construction of PRM_Templates and dynamic user interfaces.
Definition: ParmFactory.h:147
#define OPENVDB_HOUDINI_API
Definition: Platform.h:248
std::function< bool(const GA_Attribute &, const PRM_Parm &, const SOP_Node &)> AttrFilterFunc
Functor to filter a list of attributes from a SOP&#39;s input.
Definition: ParmFactory.h:199
const std::string & english() const
Return the new operator&#39;s UI name.
std::vector< PRM_Template > PrmTemplateVec
Definition: ParmFactory.h:76
Parameter template list that is always terminated.
Definition: ParmFactory.h:73
std::function< SOP_NodeCache *(void)> CacheAllocFunc
Functor that returns newly-allocated node caches for instances of this operator.
Definition: ParmFactory.h:479
OpPolicy()
Definition: ParmFactory.h:524
bool empty() const
Return true if this list contains no parameters.
Definition: ParmFactory.h:81
OpFactory(const OpPolicyType &, const std::string &english, OP_Constructor ctor, ParmList &parms, OP_OperatorTable &table, OpFlavor flavor=SOP)
Construct a factory that on destruction registers a new operator type.
Definition: ParmFactory.h:396
Helper class to manage input locking.
Definition: ParmFactory.h:565
std::string getName(const OpFactory &factory)
Return a type name for the operator defined by the given factory.
Definition: ParmFactory.h:528
#define IS_DEPRECATED
Definition: ParmFactory.h:236
virtual std::string getHelpURL(const OpFactory &)
Return a help URL for the operator defined by the given factory.
Definition: ParmFactory.h:544
size_t size() const
Return the number of parameters in this list.
Definition: ParmFactory.h:85
OPENVDB_HOUDINI_API const PRM_ChoiceList PrimGroupMenuInput2
void clear()
Remove all parameters from this list.
Definition: ParmFactory.h:88
void markInputUnlocked(exint input)
Definition: ParmFactory.h:577
const SpareDataMap & getOperatorSpareData(const OP_Operator &)
Return the spare data associated with the given operator.
std::shared_ptr< OpPolicy > OpPolicyPtr
Definition: ParmFactory.h:350
An OpPolicy customizes the behavior of an OpFactory. This base class specifies the required interface...
Definition: ParmFactory.h:521
void addOperatorSpareData(OP_Operator &, const SpareDataMap &)
Specify (key, value) pairs of spare data for the given operator.
~ScopedInputLock()
Definition: ParmFactory.h:575
Helper class to simplify operator registration.
Definition: ParmFactory.h:376
Definition: ParmFactory.h:379
virtual ~OpPolicy()
Definition: ParmFactory.h:525
OPENVDB_HOUDINI_API const PRM_ChoiceList PrimGroupMenuInput4
ScopedInputLock(SOP_Node &node, OP_Context &context)
Definition: ParmFactory.h:568
OPENVDB_HOUDINI_API const PRM_ChoiceList PrimGroupMenu