HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
traverse.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
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 heirarchy printing out
27  * information about the nodes in the file.
28  */
29 
30 
31 #include <UT/UT_NTStreamUtil.h>
32 #include <UT/UT_IStream.h>
33 #include <CMD/CMD_Args.h>
34 #include <PI/PI_ResourceManager.h>
35 #include <MOT/MOT_Director.h>
36 #include <iostream>
37 
38 static int verbose = 0;
39 
40 using std::cout;
41 
42 static void
43 printIndent(int indent)
44 {
45  while (indent > 0)
46  {
47  cout << " ";
48  indent--;
49  }
50 }
51 
52 static void
53 printNode(OP_Node *node, OP_Node *input, int indent)
54 {
55  printIndent(indent);
56  cout << node->getName();
57  if (input)
58  cout << " is wired to " << input->getName();
59  cout << "\n";
60 
61  // Now, we print out the nodes which have this node as an input
62  for (auto &&out : OP_OutputIterator(*node))
63  printNode(out, node, indent+1);
64 }
65 
66 static void
67 printNetwork(OP_Network *net, int indent = 0)
68 {
69  int i, nkids;
70  OP_Node *node;
71  OP_Network *kidnet;
72 
73  // Here, we find all the nodes which don't have inputs. These are the
74  // "root" nodes. We follow the outputs down for each node printing out the
75  // information.
76  nkids = net->getNchildren();
77  for (i = 0; i < nkids; i++)
78  {
79  node = net->getChild(i);
80 
81  // If we have no inputs, then print us out, otherwise, the node
82  // printing will print us out...
83  if (node->nInputs() == 0)
84  printNode(node, 0, indent);
85  }
86 
87  // Now, if any of the nodes are networks themselves, let's print out them
88  // and their contents.
89 
90  for (i = 0; i < nkids; i++)
91  {
92  node = net->getChild(i);
93  if (node->isNetwork())
94  {
95  // Here, this is a safe cast.
96  kidnet = (OP_Network *)node;
97  if (kidnet->getNchildren())
98  {
99  printIndent(indent+1);
100  cout << kidnet->getName() << " contains:\n";
101  printNetwork(kidnet, indent+2);
102  }
103  }
104  }
105 }
106 
107 int
108 main(int argc, char *argv[])
109 {
110  CMD_Args args;
111  MOT_Director *boss;
112  int i;
113 
114  boss = new MOT_Director("traverse");
115  OPsetDirector(boss);
117 
118  args.initialize(argc, argv);
119  args.stripOptions("v");
120 
121  if (args.found('v'))
122  verbose = 1;
123 
124  // Load the arguments
125  for (i = 1; i < args.argc(); i++)
126  {
127  UT_IFStream is;
128  if (is.open(args(i), UT_ISTREAM_ASCII))
129  {
130  // Merge in all the arguments
131  if (verbose)
132  cout << "Loading: " << args(i) << "\n";
133  if (!boss->loadNetwork(is, 1))
134  std::cerr << "Error loading network for " << args(i) << "\n";
135  }
136  }
137 
138  if (verbose)
139  cout << "Traversing the HIP file(s)\n";
140 
141  printNetwork(boss);
142 
143  return 0;
144 }
const UT_String & getName() const
OP_API OP_Director * OPsetDirector(OP_Director *boss)
bool open(const char *filename, UT_ISTREAM_READTYPE binary=UT_ISTREAM_BINARY, const UT_Options *options=NULL, bool bufferable=true)
PI_API void PIcreateResourceManager(bool verbose=true)
void stripOptions(const char *options)
int found(int opt) const
Definition: UT_Args.h:56
png_uint_32 i
Definition: png.h:2877
virtual unsigned nInputs() const
virtual int isNetwork() const
bool loadNetwork(UT_IStream &is, int merge=0, const char *mergePattern=0, int overwrite=0)
virtual int getNchildren() const
int argc() const
Definition: UT_Args.h:43
void initialize(int argc, const char *const argv[])
virtual OP_Node * getChild(const char *name=0, int *hint=0) const
int main(int argc, char *argv[])
Definition: traverse.C:108