I am trying create a node and set a value using this function:
def createTextureNode(textureNode):
keyword = textureNode
for fname in os.listdir(assetFilePath):
if keyword in fname:
texFilename = fname + ".tex"
newTexNode = NodegraphAPI.CreateNode("PrmanShadingNode", rootNode)
newTexNode.getParameter('name').setValue(assetCode + '_' + textureNode, 0)
newTexNode.getParameter('nodeType').setValue("PxrTexture", 0)
newTexNode = NodegraphAPI.GetNode(assetCode + '_' + textureNode)
if textureNode == "Albedo":
newTexNode.getParameter("parameters.linearize.enable").setValue(True,0)
newTexNode.getParameter("parameters.linearize.value").setValue(True,0)
Its pretty simple, if a particular file exists it will create a node for it. However, I am unable to set the "linearize" value (or any other value under the "parameters" group)
If I manually create a node and manually set it to be a PxrTexture and use this code:
node = NodegraphAPI.GetNode("PrmanShadingNode")
node.getParameter("parameters.linearize.enable").setValue(1,0)
node.getParameter("parameters.linearize.value").setValue(1,0)
Then it works, the check box turns on, but if I set the nodeType via script then it fails. But if I run it a second time it works! The error I keep getting is:
AttributeError: 'NoneType' object has no attribute 'setValue'
I know this means that its returning "None" but the tool tip in Katana says that its "parameters.linearize" and the first two parameters were able to be set... but these are under a Group, I think?
Since it works with the manual create, it must be something I am missing in my script, but I can not figure it out. I have also tried using finalizeValue after setting my nodeType but his has no affect
I am using this as my reference:
Getting and Setting Parameters
GOT IT!
When setting a Dynamic Parameter Pragmatically, you have to run checkDynamicParameters() after you set the value that makes the new parameters appear
This updates the UI Loop and makes the new parameters available. When doing this via the UI, it happens on the fly, and thats why it works when doing it manually
ANSWER REFERENCE
Related
I am trying to create a program containing various drop down menus and writable fields. To simplify adding new menus and writable fields i wanted to loop over a list of tuples containing the information required to create dynamic variable names and the menus.
The problem i am running into is when running the below code where 'name' is the string "Percentage". It should be noted that the code works if i remove 'validate' and 'validatecommand', though obviously the validation isn't done.
def number_validate(char):
return char.isdigit()
self.percentage_validification = self.root.register(number_validate)
exec("self." + name + "_box" + " = " + "tkinter.Entry(self.root, validate = 'key', self.validatecommand = (self.validation, '%S'))")
The above code gives the following error:
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
The following code gives the correct output but does not allow dynamic variable naming and creation, which indicates that it is not a problem with the command itself:
self.Percentage_box = tkinter.Entry(self.root, validate = "key", validatecommand = (self.percentage_validification, '%S'))
I assume my problem relates to improper handling of strings in the exec function, but i simply can't figure out how is should be done. Does anyone have an answer.
First of all, i am somewhat new to python coding, so this might seem like a stupid question.
Problem: I am trying to create a script that allows me to import a number into (variable?operator?) names, in order to run my python script from a bash script.
Ideally i wanted to do the following (i know the syntax is wrong, but it is from my first try and captures what i would want it to do):
replica_number = 2 #2 is only for testing. It will later be exchanged with an imported number from a bash script over many different data sheets.
t_r+replica number = md.load(data_path+'potein_+replica_number+_centered.xtc', top = data_path+'potein_+replica_number+_first.gro')[1:]
What i want this to do is to automatically create the variables named t_r2 and import the files called protein_2_centered.xtc and protein_2_first.gro. However when i do this i get: SyntaxError: can't assign to operator
Does anyone know how to get around this problem, or do i just have to make a separate script for every replica?
What you need is either a list or a dictionary.
you can keep all your results in a list (without keeping the replica_number):
t_r_list = []
t_r_list.append(md.load(...)[1:]) # Run this line for each t_r you want to load
or if you want to keep the replica_number, you can use a dict:
t_r_dict = {}
t_r_dict[replica_number] = md.load(...)[1:]
You might want to read a tutorial on these data structures and how to use them, it will greatly help you on your journey with python later on and is the basis of the basis when it comes to working with data in python.
when the name of the attributes or variables is dynamic, we can use, for example, the new way Python uses for fomat Strings (f'') and then the setattr method:
The settattr method is this, is part of the builtins library:
def setattr(x, y, v): # real signature unknown; restored from __doc__
""" Sets the named attribute on the given object to the specified value."""
Here what you can do with variables
replica_number = 2
variable_name = f't_r{replica_number }'
and then check and set the aatribute:
if not hasattr(YOUR_OBJECT, variable_name ):
raise ValueError
setattr(YOUR_OBJECT, variable_name , THE_VALUE)
Use a dictionary for such kind of operations:
replica_number = md.load(...)[1:]
your_dict = {t_r : replica_number}
And access it through
your_dict[t_r]
I'm running Odoo 12 and I'd like to set up an automated action after a new Opportunity is created.
This automated action would be some custom Python code which calls a system parameter, gets its value and stores it in a field in the new opportunity's record.
Afterwards, that system parameter should be increased by 1 so another new opportunity doesn't have the same number.
How do you get/set a system parameter from python code?
I've created a system parameter with key "customParameter"and value 10122.
The field where this parameter should be put is called "x_studio_deal_quotation_id", within a new opportunity.
I've got a little experience in Python, so I've no idea how to call upon this parameter. Can someone help me out? Or is there an easier way to achieve the same?
A big thanks in advance.
I managed to find what I'm looking for on my own, this is the code I used:
record[("x_studio_deal_quotation_id")] = record.env['ir.config_parameter'].get_param('customParameter')
var = record.env['ir.config_parameter'].get_param('customParameter')
var = int(var)
var += 1
record.env['ir.config_parameter'].set_param('customParameter', var)
In this function I called my system parameter by using record.env['ir.config_parameter'].get_param('customParameter')
I had to convert it to int because I got some error that the type wasn't right.
Finally, I add 1 to my variable value and write the same system parameter with the new value by using:record.env['ir.config_parameter'].set_param('customParameter', var)
I'm a bit new in scripting and french so exuse me if my explanations are not perfectly clear.
I'm trying to make a script in Maya to manage easily the keys value for animation.
So I created a window reproducing Maya's ChannelBox with another organisation.
And now I'm trying to get the attribute values of a selected object inside the different textField (transX, Y, Z, rotX..).
Here is what I have for now :
transX_value = cmds.textField( w=100 , h=22 , tx= cmds.getAttr("%s.translateX" %selected) )
But when I select my cube for tests and launch my script this error appears:
TypeError: Object [u'pCube1'].translateX is invalid
So I tried something like this to see if the problem is coming from my formulation:
transX_value = cmds.textField( w=100 , h=22 , tx= cmds.getAttr("pCube1.translateX") )
It worked and printed the good value inside the textField.
How can I call the attribute of any selected object? I just discovered the %s command, so I'm sure that I'm not using it right.
That's a very common mistake. Your 'selected' variable holds a list, not a string. You should get the first list value instead, so just change your code like that:
transX_value = cmds.textField(w=100, h=22, tx=cmds.getAttr("%s.translateX" % selected[0]))
I am trying to figure out what the required string argument for the nuke.ChannelMask_Knob() function corresponds to. On some other knob constructors the first argument seems to be the name/label, but that does not seem to be the case for the ChannelMask_Knob...
I have looked at the Nuke Python API, but I am unsure how to follow it back to the appropriate function definition to answer my question. My line of thinking is that this has to do with the init function which is overridden by the ChannelMask_Knob class, but the parameter list according to the API is just "..." which I believe means it has to do with a builtin function. Since I can't see the body of the init function, I have no idea what that argument is used for, thus my problem...
Here is an example of my issue:
test_knob = nuke.ChannelMask_Knob("required_argument")
node.addKnob(test_knob)
This works just fine, but I would like to know what the "required_argument" is used for since it is apparently not the name or label for the knob.
You must intentionally leave a blank space for parameter in ChannelMask_Knob('') method. It doesn't work. But for assigning a name and a label you need to use .setName('name') and .setLabel('label') methods respectively. It works fine.
Here is a code:
import nuke
noop = nuke.nodes.NoOp()
### maskKnob = nuke.ChannelMask_Knob('name', 'label', False)
maskKnob = nuke.ChannelMask_Knob('')
maskKnob.setName('name')
maskKnob.setLabel('label')
noop.addKnob(maskKnob)
For selection of channel in this dropdown menu it's better to use the following method:
nuke.selectedNode().knob('name').setValue('alpha')
nuke.selectedNode().knob('name').setValue('disparity')