HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
traverse.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  * This sample program traverses the whole hierarchy printing out
27  * information about the nodes in the file.
28  */
29 
30 
31 #include <UT/UT_Exit.h>
32 #include <UT/UT_Main.h>
33 #include <UT/UT_NTStreamUtil.h>
34 #include <UT/UT_String.h>
35 #include <CMD/CMD_Args.h>
36 #include <PI/PI_ResourceManager.h>
37 #include <MOT/MOT_Director.h>
38 #include <iostream>
39 
40 static bool verbose = false;
41 
42 using std::cout;
43 
44 static void
45 printIndent(int indent)
46 {
47  while (indent > 0)
48  {
49  cout << " ";
50  indent--;
51  }
52 }
53 
54 static void
55 printNode(OP_Node *node, OP_Node *input, int indent)
56 {
57  printIndent(indent);
58  cout << node->getName();
59  if (input)
60  cout << " is wired to " << input->getName();
61  cout << "\n";
62 
63  // Now, we print out the nodes which have this node as an input
64  for (auto &&out : OP_OutputIterator(*node))
65  printNode(out, node, indent+1);
66 }
67 
68 static void
69 printNetwork(OP_Network *net, int indent = 0)
70 {
71  int i, nkids;
72  OP_Node *node;
73  OP_Network *kidnet;
74 
75  // Here, we find all the nodes which don't have inputs. These are the
76  // "root" nodes. We follow the outputs down for each node printing out the
77  // information.
78  nkids = net->getNchildren();
79  for (i = 0; i < nkids; i++)
80  {
81  node = net->getChild(i);
82 
83  // If we have no inputs, then print us out, otherwise, the node
84  // printing will print us out...
85  if (node->nInputs() == 0)
86  printNode(node, 0, indent);
87  }
88 
89  // Now, if any of the nodes are networks themselves, let's print out them
90  // and their contents.
91 
92  for (i = 0; i < nkids; i++)
93  {
94  node = net->getChild(i);
95  if (node->isNetwork())
96  {
97  // Here, this is a safe cast.
98  kidnet = (OP_Network *)node;
99  if (kidnet->getNchildren())
100  {
101  printIndent(indent+1);
102  cout << kidnet->getName() << " contains:\n";
103  printNetwork(kidnet, indent+2);
104  }
105  }
106  }
107 }
108 
109 int
110 theMain(int argc, char *argv[])
111 {
112  MOT_Director *boss = new MOT_Director("traverse");
113  OPsetDirector(boss);
115 
116  CMD_Args args;
117  args.initialize(argc, argv);
118  args.stripOptions("v");
119  if (args.found('v'))
120  verbose = true;
121 
122  // Load the arguments
123  for (int i = 1; i < args.argc(); i++)
124  {
125  const char *filename = args(i);
126  UT_String warnings_or_errors;
127  // Merge in all the arguments
128  if (verbose)
129  cout << "Loading: " << filename << "\n";
130  bool success = boss->loadOrMergeHipFile(
131  filename,
132  true, // merge into existing scene (false means to clear existing scene)
133  nullptr,// pattern for which nodes to load
134  false, // overwrite existing nodes
135  warnings_or_errors);
136  if (!success)
137  {
138  std::cerr << "Error loading network for " << filename << ":\n";
139  std::cerr << warnings_or_errors.c_str() << "\n";
140  }
141  else if (verbose && warnings_or_errors.isstring())
142  {
143  std::cerr << "Warnings loading network for " << filename << ":\n";
144  std::cerr << warnings_or_errors.c_str() << "\n";
145  }
146  }
147 
148  if (verbose)
149  cout << "Traversing the HIP file(s)\n";
150 
151  printNetwork(boss);
152 
153  return 0;
154 }
155 UT_MAIN(theMain);// exit with proper tear down
GT_API const UT_StringHolder filename
int getNchildren() const override
OP_API OP_Director * OPsetDirector(OP_Director *boss)
bool loadOrMergeHipFile(const char *file_name, bool merge, const char *pattern, bool overwrite, UT_String &warnings_or_errors)
int theMain(int argc, char *argv[])
Definition: traverse.C:110
PI_API void PIcreateResourceManager(bool verbose=true)
OP_Node * getChild(const char *name=0, int *hint=0) const override
void stripOptions(const char *options)
int found(int opt) const
Definition: UT_Args.h:61
const char * c_str() const
Definition: UT_String.h:508
virtual unsigned nInputs() const
virtual int isNetwork() const
int argc() const
Definition: UT_Args.h:48
SYS_FORCE_INLINE const UT_String & getName() const
**If you just want to fire and args
Definition: thread.h:609
bool isstring() const
Definition: UT_String.h:691
void initialize(int argc, const char *const argv[])
UT_MAIN(theMain)