Hi,
I have python script at the obj level connected to two other geo nodes.
I want my script to print out the names of the two geo nodes.
This is my script :
node = hou.pwd()
node_outputs = node.outputs()
for i in node_outputs:
print node_outputs(i).name()
I get this error :
Python error: Traceback (most recent call last):
File “”, line 5, in
TypeError: tuple indices must be integers, not ObjNode
What am I missing here?
If I write :
print node_outputs(0).name()
This works correctly though. So what's wrong with my loop?
Thanks!
PS I had to replace the brackets where the indices are to post this
Python node connection for each loop
1860 4 0- maxbel
- Member
- 36 posts
- Joined: Jan. 2014
- Offline
- jjayakumar
- Member
- 106 posts
- Joined: June 2011
- Offline
Hi there!
TypeError: tuple indices must be integers, not ObjNode
As the error says, the elements in the node_outputs are of type objNode and not integer. So when you do for i in node_outputs, datatype of i is not an integer(number), so you won't be able to access the tuple elements using that.
Hope it helps
-J
TypeError: tuple indices must be integers, not ObjNode
As the error says, the elements in the node_outputs are of type objNode and not integer. So when you do for i in node_outputs, datatype of i is not an integer(number), so you won't be able to access the tuple elements using that.
# node_outputs will look something like this node_outputs(<hou.ObjNode of type geo at /obj/geo2>, <hou.ObjNode of type geo at /obj/geo3>) # Since you already have the node object in node_outputs, # you could get its name directly like this for out_node in node_outputs: print(out_node.name()) # or if you prefer to use the same approach, # you have to loop through the full length of node_outputs for i in range(len(node_outputs)): print(node_outputs[i].name())
Hope it helps
-J
- maxbel
- Member
- 36 posts
- Joined: Jan. 2014
- Offline
Hi J and thanks for your very descriptive response
I'm obviously still very new to python so there are some concepts that I don't fully grasp.
For example, when I created the object node_outputs = node.outputs() I assumed that this was returning me a list I could loop over the indices. From what I understood from your answer is that my node_outputs only contains hou.ObjNodes that don't have indices. Hence tuples don't have indices?
Thanks again for your explanations
Max
I'm obviously still very new to python so there are some concepts that I don't fully grasp.
For example, when I created the object node_outputs = node.outputs() I assumed that this was returning me a list I could loop over the indices. From what I understood from your answer is that my node_outputs only contains hou.ObjNodes that don't have indices. Hence tuples don't have indices?
Thanks again for your explanations
Max
- jjayakumar
- Member
- 106 posts
- Joined: June 2011
- Offline
Hi Max,
tuples are data structures which can store multiple values, For example,
Both are valid tuples. You access the elements of the tuple using its index. Tuple indices starts from 0.
When you do for i in my_tuple1, here i is just a temporary variable that you're creating to access the elements of the tuple when looping and i doesn't refer to the index of the tuple. In this case i refers to ‘hello’ in the first iteration and ‘world’ in the second.
In your case, node.outputs() returns a tuple which you could loop through using it's indices but node_outputs is not returning you a tuple of indices.
It is returning a tuple of objNodes. so when you do for i in node_outputs, i refers to the first objNode in first iteration and not the index of the first objNode. To get the index, you do for i in range(len(node_outputs)). Now you're looping through a range of values and len(node_outputs) returns the total length of node_outputs (2 in my case).
So under the hood, it's doing for i in range(0, 2), and i will be 0 in the first iteration and 1 in the second iteration and you can use these numbers as indices to access the elements of the tuple.
Hope it's clear. Please let me know if it's not.
Cheers
-J
tuples are data structures which can store multiple values, For example,
my_tuple1 = ('hello', 'world') my_tuple2 = (1, 2, 3)
Both are valid tuples. You access the elements of the tuple using its index. Tuple indices starts from 0.
first_element = my_tuple1[0] second_element = my_tuple1[1] >> print(first_element) 'hello' >> print(second_element) 'world'
When you do for i in my_tuple1, here i is just a temporary variable that you're creating to access the elements of the tuple when looping and i doesn't refer to the index of the tuple. In this case i refers to ‘hello’ in the first iteration and ‘world’ in the second.
In your case, node.outputs() returns a tuple which you could loop through using it's indices but node_outputs is not returning you a tuple of indices.
It is returning a tuple of objNodes. so when you do for i in node_outputs, i refers to the first objNode in first iteration and not the index of the first objNode. To get the index, you do for i in range(len(node_outputs)). Now you're looping through a range of values and len(node_outputs) returns the total length of node_outputs (2 in my case).
So under the hood, it's doing for i in range(0, 2), and i will be 0 in the first iteration and 1 in the second iteration and you can use these numbers as indices to access the elements of the tuple.
Hope it's clear. Please let me know if it's not.
Cheers
-J
- maxbel
- Member
- 36 posts
- Joined: Jan. 2014
- Offline
Hi J,
Thanks again for your explanations. It does make a lot more sense.
I understand that in my for each loop, my ‘i’ was already getting assigned the objects of my tuple node_outputs.
Hence all I needed was something like print i.name()
Maybe I don't have the right terminology but it does make more sense
Thanks again!
Max
Thanks again for your explanations. It does make a lot more sense.
I understand that in my for each loop, my ‘i’ was already getting assigned the objects of my tuple node_outputs.
Hence all I needed was something like print i.name()
Maybe I don't have the right terminology but it does make more sense
Thanks again!
Max
-
- Quick Links