Get list of values from nuke.EvalString_Knob? - python

I'm trying to work with a custom group node that has a bunch of EvalString_Knobs and I need to get a list of items in them but when I try using node['knobName'].values()
I get an attribute error as values isn't an attirbute of EvalString_Knob.
Anyone have a way to get the values of EvalString_Knob?
Thanks.

Let's create two EvalString_Knob inside a new tab:
import nuke
write = nuke.createNode('Write', inpanel=False)
tab = nuke.Tab_Knob("Parameters")
write.addKnob(tab)
write.addKnob(nuke.EvalString_Knob('prefix','Prefix','render'))
write.addKnob(nuke.EvalString_Knob('suffix','Suffix','7'))
In UI it looks like that:
For listing all the dictionary's knobs of a Write node use the following command:
nuke.toNode("Write1").knobs().keys()
To get a value of any known knob use this command:
nuke.toNode("Write1").knob('prefix').getValue()
To list all the properties with their corresponding values use this approach:
nuke.selectedNode().writeKnobs()
P.S.
TabKnobs have no names to reach them. Only empty strings.

You can use
node['knobName'].getValue() to get a string returned from an EvalString_Knob,
node['knobName'].values() to get a list returned from an Enumeration_Knob,
and you can use node['knobName'].Class() to find out what is the type of the knob.

Related

Problems with BeautifulSoup find_all

I need to retrieve a few ids from a site html, it's not a hard work to do if i create some variables to store them there, however i would like to use a list to make it easier to find and work with.
The terminal returns "TypeError: list indices must be integers or slices, not str" when using the following line:
ids = site.find_all('p', class_="frase fr")['id']
I mean, using soup.find_all works fine for me, though if i use the square brackets in the end to specify where it should gather the info it don't work. Here lies the problem, how can i fix it?
The find_all method returns a list of elements, so if you want to get only the IDs for each element you will have to iterate over each one and extract the desired information.
Use this instead:
ids = [p.get('id') for p in site.find_all('p', class_="frase fr")]
This will give you a list of every ID in the tags you find, including None ones.
You can also filter the None's out using:
ids = [p.get('id') for p in site.find_all('p', class_="frase fr") if p.get('id')]

passing fields of an array of collections through functions in python

is there a way of passing a field of an array of collections into a function so that it can still be used to access a element in the collection in python?. i am attempting to search through an array of collections to locate a particular item by comparing it with an identifier. this identifier and field being compared will change as the function is called in different stages of the program. is there a way of passing up the field to the function, to access the required element for comparison?
this is the code that i have tried thus far:
code ...
In your code, M_work is a list. Lists are accessed using an index and this syntax: myList[index]. So that would translate to M_work[place] in your case. Then you say that M_work stores objects which have fields, and you want to access one of these fields by name. To do that, use getattr like this: getattr(M_work[place], field). You can compare the return value to identifier.
Other mistakes in the code you show:
place is misspelled pace at one point.
True is misspelled true at one point.
The body of your loop always returns at the first iteration: there is a return in both the if found == True and else branches. I don't think this is what you want.
You could improve your code by:
noticing that if found == True is equivalent to if found.
finding how you don't actually need the found variable.
looking at Python's for...in loop.

Check if element is list or another object

I've got an object which contains an element named "companies".
This element can either be a list of objects or just a single object (not contained within a list).
I would like to run through all companies, but this example fails if the element "companies" is just a single item (not contained within a list):
for company in companies:
I've tried to test before the for-loop, such as:
if type(companies['company']) is list:
# do your thing
but that fails as well.
Can anyone help?
Firstly, that's a really horrible way to structure data, and you should complain to whoever creates it. If an item can be a list, it should always be a list, even if that list just contains one element.
However, the code you have shown should work - although a better way to do it is if isinstance(companies['company'], list). If that's still not working, you will need to show the data, and the exact code that's using it.
You can make a list from a non-list for non-conditional use of "for ... in ...".
companies = list(companies)
for company in companies:
# use "company" in some way

Check if string doesn't exist in List

I have a List of Strings in Python and I want to check if the string "EVN" does not exist in the range segmentList[x][0:3], so I can create it in a specific place in the list.
I'm trying to check if the string in this range (segmentType = segmentList[x][0:3]) in the list and if it doesn't I want to call a method create_EVN() and insert the "EVN" string into the second position of the list moving up the other elements rather that deleting them.
I'm fairly new to python and I'm trying to find the most efficient way possible to do this. I have tried looping through the list with no avail.
Is this what you are looking for?
if 'EVN' not in segmentList[x][0:3]:
create_EVN()
How about
if 'EVN' not in segmentList[x][0:3] :
create_EVN()

Retrieving a specific set element in Python

Essentially this is what I'm trying to do:
I have a set that I add objects to. These objects have their own equality method, and a set should never have an element equal to another element in the set. However, when attempting to insert an element, if it is equal to another element, I'd like to record a merged version of the two elements. That is, the objects have an "aux" field that is not considered in its equality method. When I'm done adding things, I would like an element's "aux" field to contain a combination of all of the "aux" fields of equal elements I've tried to add.
My thinking was, okay, before adding an element to the set, check to see if it's already in the set. If so, pull it out of the set, combine the two elements, then put it back in. However, the remove method in Python sets doesn't return anything and the pop method returns an arbitrary element.
Can I do what I'm trying to do with sets in Python, or am I barking up the wrong tree (what is the right tree?)
Sounds like you want a defaultdict
from collections import defaultdict
D = defaultdict(list)
D[somekey].append(auxfield)
Edit:
To use your merge function, you can combine the code people have given in the comments
D = {}
for something in yourthings:
if something.key in D:
D[something.key] = something.auxfield
else:
D[something.key] = merge(D[something.key], something.auxfield)

Categories

Resources