Maya python CHANGE UVTILE with place2dTexture - python

In python I want to select one object and change repeatUV .
The code work for all objects and not for one specific object
import maya.cmds as cmds
import pymel.core as pmc
def UVTILECHANGE():
selected = cmds.ls(sl=True)
textField_obj_name = cmds.textField( 'textField_action_name',query=True,text=True )
textField_obj = cmds.textField( 'textField_action',query=True,text=True )
#print textField_obj
textField_obj2 = cmds.textField( 'textField_action2',query=True,text=True )
if textField_obj_name==selected[0]:
array = cmds.ls(type="place2dTexture")
#SELECT ALL PLACE2DTECTURE
#I WANT JUST FOR selected[0]
#print len(array)
i=0
while (i <= len(array)):
print
cmds.setAttr(str(array[i])+ ".repeatU", float(textField_obj))
cmds.setAttr(str(array[i])+ ".repeatV", float(textField_obj2))
i=i+1
def MakeWindow():
if (pmc.window('flattenVertwindow', exists=True)):
pmc.deleteUI('flattenVertwindow')
pmc.window('flattenVertwindow', title="TEST",widthHeight=(200,100))
pmc.columnLayout(adjustableColumn=True)
#pmc.text(label='select axis')
cmds.text("Choisir nom de l'objet")
cmds.separator(h=10)
textField00 = cmds.textField( 'textField_action_name' )
cmds.separator(h=10)
cmds.text("Taille des Tiles")
cmds.separator(h=10)
textField01 = cmds.textField( 'textField_action' )
textField02 = cmds.textField( 'textField_action2' )
red= pmc.button(label='Change UV TILE ', command= 'UVTILECHANGE()')
pmc.showWindow('flattenVertwindow')
MakeWindow()

Im not sure what doesn't work, Ive improved your code with comments.
I hope it will help, it works here :
import maya.cmds as cmds
import pymel.core as pmc
# if you dont know classes, you can create a dict to store all main controls
UI_DIC = {}
# for function used in ui, you need *args because maya will try to parse a default True
# argument as last argument
def picker(*args):
sel = cmds.ls(sl=True)[0]
cmds.textField(UI_DIC['textField00'], edit=True, text=sel )
# use lower case for functions, you can use camel casing like maya : uvTilesChange
# but keep the first letter lower case as uppercase for the first is used for Class
def uv_tile_change(*args):
# we parse fields with the value captured in the dictionnary
textField_obj_name = cmds.textField(UI_DIC['textField00'], query=True, text=True )
textField_obj = cmds.textField(UI_DIC['textField01'], query=True, text=True )
textField_obj2 = cmds.textField(UI_DIC['textField02'], query=True, text=True )
# if there is nothing in the ui, dont execute anything
if not textField_obj_name:
return
# we are using few funcitons to find the place2dTexture of an object
shape_mesh = cmds.ls(textField_obj_name, dag=True, type='shape', ni=True)
if not shape_mesh:
# if the object doesnt exists, stop here
return
# we look for the shading engine of the object
hist = cmds.listHistory(shape_mesh, future=True)
sgs = cmds.ls(hist, type="shadingEngine")
# from the sgs, we look for the place2dTexture
place2dTextures = [i for i in cmds.listHistory(sgs) if cmds.ls(i, type='place2dTexture')]
# instead of a while, just use a python for loop
for p2t in place2dTextures:
cmds.setAttr(p2t + ".repeatU", float(textField_obj))
cmds.setAttr(p2t + ".repeatV", float(textField_obj2))
def make_window():
width = 180
height = 150
if (pmc.window('flattenVertwindow', exists=True)):
pmc.deleteUI('flattenVertwindow')
# sizeable works for me
pmc.window('flattenVertwindow', title="TEST",widthHeight=(width,height), sizeable=False)
main_layout = pmc.columnLayout(adjustableColumn=True)
# use the flag parent, it is more clear
pmc.text("Choisir nom de l'objet", parent=main_layout)
# choose either cmds or pymel, do not use both
pmc.separator(h=10, parent=main_layout)
# here is an example on how parent flag is useful
# we create a sub layout and parent it to the main one
# we parent all the controls to the sub layout
picker_layout = pmc.rowLayout(nc=3, parent=main_layout, width=width, ad1=True)
# we capture the name of the control on creation inside the dict
UI_DIC['textField00'] = pmc.textField(parent=picker_layout, w=width)
# take the habit to not put string in the command flag
pmc.button(label='<', command= picker, parent=picker_layout)
pmc.separator(h=10, parent=main_layout)
pmc.text("Taille des Tiles", parent=main_layout)
pmc.separator(h=10)
UI_DIC['textField01'] = pmc.textField(parent=main_layout)
UI_DIC['textField02'] = pmc.textField(parent=main_layout)
red= pmc.button(label='Change UV TILE ', command= uv_tile_change, parent=main_layout)
pmc.showWindow('flattenVertwindow')
make_window()

Related

Calling function from UI class in maya resulting in nonetype error

I'm building a toolbox UI using python in Maya, and I keep on getting a Nonetype error when I call one of the imported functions. This is the script for the toolbox:
class Toolbox():
import maya.cmds as cmds
def __init__(self):
self.window_name = "mlToolbox"
def create(self):
self.delete()
self.window_name = cmds.window(self.window_name)
self.m_column = cmds.columnLayout(p = self.window_name, adj = True)
cmds.button(p=self.m_column,label = 'MyButton', c=lambda *arg: cmds.polySphere(r = 2))
cmds.button(p=self.m_column, label = 'Make_Control', command = lambda *args: self.ControlBTN())
cmds.button(p=self.m_column, label = 'Find Center of All Selected', command = lambda *args: self.CenterBTN())
cmds.button(p=self.m_column, label = 'Find Center of Each Selected Object', command = lambda *args: self.IndiCenterBTN())
self.colorname = cmds.textField(placeholderText = 'Enter color name...')
cmds.button(p=self.m_column, label = 'ChangeColor', command = lambda *args: self.colorBtn())
self.MinAndMax = cmds.textField()
cmds.button(p=self.m_column, label = 'Random Scatter', command = lambda *args: self.ScatterBTN())
cmds.showWindow(self.window_name)
cmds.button(p=self.m_column, label = 'Select Everything', command = lambda *args: self.selectBTN())
def CenterBTN(self):
import CenterSelected
CenterSelected.Locator()
def ScatterBTN(self):
import Scatter
value = cmds.textField(self.MinAndMax, q=True)
Scatter.RandomScatter(value)
cmds.intField(self.moveMin, self.moveMax, self.rotMin, self.rotMax, self.scaleMin, self.scaleMax, e=True, text='')
def IndiCenterBTN(self):
import ManySelected
ManySelected.LocatorMany()
def colorBtn(self):
import ColorControl
value = cmds.textField(self.colorname, q=True, text = True)
ColorControl.colorControl(value)
cmds.textField(self.colorname, e=True, text='')
def selectBTN(self):
import tools
tools.selectAll()
def delete(self):
if cmds.window(self.window_name, exists=True):
cmds.deleteUI(self.window_name)
def ControlBTN(self):
import CreateControl
CreateControl.createControl()
myTool = Toolbox()
myTool.create()
And this is the function that I'm having trouble with:
def RandomScatter(MinAndMax):
import random
import maya.cmds as cmds
Stuff = cmds.ls(sl=True)
i=0
for i in range(random.randint(1,100)):
Stuff.append(cmds.duplicate(Stuff))
cmds.move( (random.randint(MinAndMax[0], MinAndMax[1])), (random.randint(MinAndMax[0], MinAndMax[1])), (random.randint(MinAndMax[0], MinAndMax[1])), Stuff[i], absolute=True )
cmds.rotate( (random.randint(MinAndMax[2], MinAndMax[3])), (random.randint(MinAndMax[2], MinAndMax[3])), (random.randint(MinAndMax[2], MinAndMax[3])), Stuff[i], absolute=True )
cmds.scale( (random.randint(MinAndMax[4], MinAndMax[5])), (random.randint(MinAndMax[4], MinAndMax[5])), (random.randint(MinAndMax[4], MinAndMax[5])), Stuff[i], absolute=True )
i = i+1
RandomScatter() works fine as long as I call it on it's own using a RandomScatter([a, b, c, d, e, f]) format, but when I try to call it Toolbox(), I get "Scatter.py line 21: 'NoneType' object has no attribute 'getitem'" as an error. It also happens when I try to use the intField() command instead of textField(). The UI window builds just fine; the error only happens after I enter input into the text field and press the button that's supposed to call RandomScatter(). It seems like the input isn't making it to the MinAndMax list, so when it reaches "cmds.move( (random.randint(MinAndMax[0]," it can't find anything to put in the MinAndMax[0] slot, or any of the slots after that, but I can't figure out why. Does anyone have any advice?
I didn't test your code and didn't read it totally, but I can already say that your strange "lambda" usage doesn't make any sens.
lambda *args: self.ControlBTN()
this lambda execute self.ControlBTN during the cmds.button definition and provide to it a function which return None.
that like doing that:
self.ControlBTN()
function = def func(): return None
cmds.button(p=self.m_column, label = 'Make_Control', command=function)
I advise you to reread the documentation about the "python lambda".
replace this by:
cmds.button(p=self.m_column, label = 'Make_Control', command=self.ControlBTN)
...
def ControlBTN(self, *args)
...
That should help
good luck
As written self.MinAndMax is a text field; even if you get the value from it it'll be a string and you won't be able to index into it to get the individual items -- and your indexing will be thrown off if any of the numbers are negative or have decimals. The lazy solution is to use a FloatFieldGrp which lets you have 2-4 numberic inputs. It's a bit annoying to get at all of the values at once (see the way it's done below) but it will avoid many issues with trying to parse the text field.
Also, this line doesn't make sense in context:
cmds.intField(self.moveMin, self.moveMax, self.rotMin, self.rotMax, self.scaleMin, self.scaleMax, e=True, text='')
You're not seeing the error because it's failing in the previous line, but the first argument ought to be the name of an existing intField.
In any case, I'd refactor this a bit to keep the argument parsing out of the scatter function, so you can separate out the working logic from the UI parsing logic. It'll make it much easier to spot where things have gone off the rails:
import random
import maya.cmds as cmds
class TestGUI(object):
def __init__(self):
self.window = cmds.window()
self.layout = cmds.rowLayout(nc=3)
self.min_xyz = cmds.floatFieldGrp( numberOfFields=3, label='min', value1=-10, value2=-10, value3=-10 )
self.max_xyz= cmds.floatFieldGrp( numberOfFields=3, label='min', value1=10, value2=10, value3=10 )
cmds.button(label='scatter', c = self.scatter)
cmds.showWindow(self.window)
def scatter(self, *_):
selected = cmds.ls(sl=True)
if not selected:
cmds.warning("select something")
return
min_xyz = (
cmds.floatFieldGrp(self.min_xyz, q=True, v1=True),
cmds.floatFieldGrp(self.min_xyz, q=True, v2=True),
cmds.floatFieldGrp(self.min_xyz, q=True, v3=True)
)
max_xyz = (
cmds.floatFieldGrp(self.max_xyz, q=True, v1=True),
cmds.floatFieldGrp(self.max_xyz, q=True, v2=True),
cmds.floatFieldGrp(self.max_xyz, q=True, v3=True)
)
print "scatter settings:", min_xyz, max_xyz
rand_scatter(selected, min_xyz, max_xyz)
def rand_scatter(selection, min_xyz, max_xyz):
dupe_count = random.randint(1, 10)
duplicates = [cmds.duplicate(selection) for n in range(dupe_count)]
for dupe in duplicates:
destination = [random.randint(min_xyz[k], max_xyz[k]) for k in range(3)]
cmds.xform(dupe, t=destination, absolute=True)
print (dupe, destination)
t = TestGUI() # shows the window; hit 'scatter' to test
My version changes your logic a bit -- you were duplicating your selection list, which would cause the number of items to grow exponentially (as each duplication would also duplicate the previous dupes). I'm not sure if that's inline with your intention or not.
You can avoid the extra lambdas by including a *_ in the actual button callback; it's a minor maya irritant that buttons always have that useless first argument.
As an aside, I'd try not to do imports inside of function bodies. If the imported module is not available, it's better to know at the time this file is imported rather than only when the user clicks a button -- it's much easier to spot a missing module if you do all your imports in a block at the top.

How do I connect custom Attributes to node inputs?

I'm finally in the end game of the Helper Joint script I'm working on and only 1 last problem stands in my way. Here is how the script is supposed to work. Create a joint, name it "Parent_Joint" then create a "multDoubleLinear" node in the node editor, name it "bob, Hit "Load Parent Joint" after selecting the joint you created and hit add attribute. In an ideal world where I'm smarter on this subject, The custom attribute added to the joint would be plugged into bob's "input1" instead I get an error saying "# Error: The source attribute 'Parent_Joint_HelperJntAttr' cannot be found."
In terms of what I've already tried, I put connectAttr beneath addAttr as common sense would dictate that first the attribute must be created before it's connected: but despite this it's just refusing to connect. I know the fault doesn't fall on the "bob.input1" node, because it only brings up the prefixed attribute name for 'Parent_Joint_HelperJntAttr: so my guess is it's just simply my lack of knowledge in writing this particular procedure.
import maya.cmds as cmds
if cmds.window(window, exists =True):
cmds.deleteUI(window)
window = cmds.window(title='DS Attribute adder')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Parent Joint', c = set_textfield)
def add_Attribute(_):
text_value = cmds.textField(sld_textFld, q = True, text=True)
if text_value:
print "attrAdded:"
cmds.addAttr(ln=text_value +'_HelperJntAttr', defaultValue=5.0, minValue=0, attributeType='float', keyable=True)
cmds.connectAttr( text_value +"_HelperJntAttr", 'bob.input1')
else:
cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='add attribute', c = add_Attribute)
cmds.showWindow(window)
I know how to use the connectAttr command on default attributes in maya, but where I fall flat is custom attributes. My hope is I come out of this knowing how to write code that both creates and connects the custom attributes of the joint. Thank you in advance for your help
The way you were using addAttr, it was including the joint's name in the attribute name. Attributes are separated with a ., not an _, so your connectAttr also fails because of that.
You also need to initialize your window variable to some default value or it fails on the line where you check if it exists (but window isn't defined at that point).
Here is the script adding the attribute and connecting it as expected:
import maya.cmds as cmds
window = "" # Need to initialize this variable first or it crashes on next line.
if cmds.window(window, exists =True):
cmds.deleteUI(window)
window = cmds.window(title='DS Attribute adder')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Parent Joint', c = set_textfield)
def add_Attribute(_):
text_value = cmds.textField(sld_textFld, q = True, text=True)
if text_value:
print "attrAdded:"
# Attribute must be created this way.
cmds.addAttr(text_value, ln='HelperJntAttr', defaultValue=5.0, minValue=0, attributeType='float', keyable=True)
# Attribute is separated with a dot.
cmds.connectAttr(text_value + ".HelperJntAttr", 'bob.input1')
else:
cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='add attribute', c = add_Attribute)
cmds.showWindow(window)

maya python textField + object name Assistance Please

Here is my plan. First create a joint and open your node editor. When you got your joint created, name it "A_Joint" hit "load joint" after running the script, then hit "test" upon hitting test, you should get a node created with the name "A_Joint_firstGuy"
the objective of this script is to create a node based on the name of whatever you loaded into the textField. It will take the name of the selected object and add it to the front of the name of the node
Atleast thats what should happen, but in truth I lack the knowledge to figure this out and every google search has thus far been fruitless. The script is down below for anyone willing to take a crack at it, thank you for your time and I hope to hear back from you:
https://drive.google.com/file/d/1NvL0MZCDJcKAnVu6voVNYbaW0HurZ6Rh/view?usp=sharing
Or here, in SO format:
import maya.cmds as cmds
if cmds.window(window, exists =True):
cmds.deleteUI(window)
window = cmds.window(title = "DS Selection Loader Demo", widthHeight=(300, 200) )
cmds.columnLayout(adjustableColumn=True)
def sld_loadHelperJoint():
sel = cmds.ls(selection=True)
selString = " ".join(sel)
add = cmds.textField('sld_surfaceTextHJ', edit=True, text=selString)
#def sld_loadParentJoint():
# sel = cmds.ls(selection=True)
# selString = " ".join(sel)
# add = cmds.textField('sld_surfaceTextPJ', edit=True, text=selString)
def createNode():
testNode = cmds.createNode( 'transform', selString = "sld_surfaceTextHJ", name = '_firstGuy' )
cmds.columnLayout(adjustableColumn=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
cmds.button( label='Load Helper Joint', command = "sld_loadHelperJoint()")
cmds.setParent('..')
#cmds.columnLayout(adjustableColumn=True)
#name = cmds.textField('sld_surfaceTextPJ', width =240)
#cmds.button( label="Load Parent Joint", command = "sld_loadParentJoint()")
#cmds.setParent('..')
testNode = cmds.createNode( 'transform', name = textField +'_firstGuy' )
# you must first create "def" group for the attributes you
# want to be created via button, "testNode" is our example
# Connect the translation of two nodes together
#cmds.connectAttr( 'firstGuy.t', 'secondGuy.translate' )
# Connect the rotation of one node to the override colour
# of a second node.
#cmds.connectAttr( 'firstGuy.rotate', 'secondGuy.overrideColor' )
cmds.showWindow (window)
OK, there's a few things going on here to consider.
First, Maya gui widgets look like strings -- just like you make a polyCube and it comes back to you as the string 'pCube1', a widget will come back as a string like 'myWindow' or 'textField72'. Just like working with scene objects, you always need to capture the results of a command so you know what it's really called -- you can't guarantee you'll get the name you asked for, so always capture the results.
So you want to do something like this, just to get the graphics going:
window = cmds.window(title='selection loader demo')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
load_button = cmds.button( label='Load Helper Joint')
cmds.show_window(window)
If you needed to ask what's in the textField, for example, you'd do:
text_contents = cmds.textField(sld_textFld, q=True, text=True)
You notice that's with the variable, not the string, so we're sure we have whatever worked.
To make the button use that information, though, the button script needs to have access to the variable. There are several ways to do this -- it's a common stack overflow question -- but the easiest one is just to define the button command where you already have that variable. So the above sample becomes:
window = cmds.window(title='selection loader demo')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
add = cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Helper Joint', c = set_textfield)
cmds.showWindow(window)
There are two bits here.
One is the _ in the function definition; Maya buttons always call their functions with one argument. There's nothing magical about the underscore, it's just Python slang for "ignore me" -- but if you don't have an argument in the function def, it will fail.
The more important bit is that the button is given the function definition directly, without quotes. You are saying call this function when clicked. If you use the string version -- a holdover from MEL -- you will run into problems later. The reasons are explained in detail here but the TLDR is don't use the the string form. Ever.
Now that the structural pieces are in place, you should be able to either the node creation to the function I called set_textfield() or make a second button\function combo that does the actual node creation, something like:
window = cmds.window(title='selection loader demo')
column = cmds.columnLayout(adj=True)
sld_textFld = cmds.textField('sld_surfaceTextHJ', width =240)
def set_textfield(_):
sel = cmds.ls(selection=True)
cmds.textField(sld_textFld, edit=True, text=sel[0])
load_button = cmds.button( label='Load Helper Joint', c = set_textfield)
def make_node(_):
text_value = cmds.textField(sld_textFld, q = True, text=True)
if text_value:
print "created:", cmds.createNode('transform', n=text_value +'_firstGuy')
else:
cmds.warning("select an object and add it to the window first!")
node_button = cmds.button( label='Make Node', c = make_node)
cmds.showWindow(window)

Cannot get LabelFrame widget to display on ttk notebook (python 3.5.1)

I have an application in which the (tkinter) LabelFrame widget on the tab of a ttk Notebook will not update itself. In a much simplified version of that program (code extracts below) the widget will not even appear on the GUI.
Everything else on the GUI works properly, including updating the tab titles, the application title (and icon) and updating the Label, Checkbutton and Radiobutton widgets on all notebook tabs. Using the ttk versions (e.g. ttk.LabelFrame) to create those widgets did not fix the issue. I also tried using “update_idletasks” (some think this as a kludge) immediately after updating the widget attributes without success.
In the real application the text of all GUI widgets change according to the state of a “choose language” widget on the same GUI (for details, see: Need Python/tkinter GUI to dynamically update itself (for a multi-lingual GUI)). It’s known that all GUI text values (the WidgetName["text”] attribute), including the missing LabelFrame, are being correctly updated to match the state of that widget.
Is there something “special” about LabelFrame widgets on Notebook tabs? What (probably simple) thing am I overlooking?
Also, any confirmation/denial by others will help determine if the problem is unique to my system - a distinct possiblity since my machine is administered by corporate IM (who don't have the best record when handling the needs of unusual users like me).
Thanks
The following is a complete example of the problem. When run, the LabelFrame widget (which should appear at (0, 0) of Tab 1) does not appear. Clicking on the "language" widget causes everything else to display text in the language selected by the "language" widget.
From “LanguageInUse.py” the code that switches languages:
import sys
c_cedille_lower = "\u00E7" # UTF 8/16 (code page 863?) French character
e_circumflex_lower = "\u00EA"
English = 'English' # string shown on GUI
Francais = 'Fran' + c_cedille_lower + 'ais' # string shown on GUI
DefaultLanguage = Francais
DefaultLanguage = English # comment out this line to use French
user_language = DefaultLanguage # set up language shown on GUI
# Define all language-dependent GUI strings (as "Application-Level" globals)
ComplianceMessage = None
ReportTab1Title = None
ReportTab2Title = None
XCheckbuttonMessage = None
XLabelFrameMessage = None
XLabelMessage = None
XRadioButtonMessage = None
'''=========================================================================='''
def SetGuiLanguage( user_language ) :
global ComplianceMessage, LanguagePrompt
global ReportTab1Title, ReportTab2Title
global XLabelFrameMessage, XCheckbuttonMessage, XLabelMessage, XRadioButtonMessage
if ( user_language == English ):
LanguagePrompt = "Language"
ReportTab1Title = "Message Counts"
ReportTab2Title = "Communications Compliance"
XLabelFrameMessage = "LabelFrame"
XCheckbuttonMessage = "Checkbox"
XLabelMessage = "Label"
XRadioButtonMessage = 'Radiobutton'
ComplianceMessage = "Compliance (engish)"
elif ( user_language == Francais ):
LanguagePrompt = "Langage"
ReportTab1Title = "Comtes de message"
ReportTab2Title = "Compliance Communications"
XLabelFrameMessage = "LabelFrame en " + Francais
XCheckbuttonMessage = "Checkbox en " + Francais
XLabelMessage = "Label en " + Francais
XRadioButtonMessage = "Radiobutton en " + Francais
ComplianceMessage = "Compliance - " + Francais
else:
print (' An unknown user language was specified' )
sys.exit()
return
'''=========================================================================='''
SetGuiLanguage( user_language ) # initialize all tkinter strings at startup
'''========================== End of File ================================'''
From “SelectReports.py”) the code that builds the notebook:
from tkinter import Checkbutton, Radiobutton, Label, LabelFrame, Frame
from tkinter import ttk
import LanguageInUse
# Locally defined entities importable by other modules (often
# Tkinter Application level objects whose language can be changed)
ComplianceMessageText = None
NotebookFrame = None
XCheckbutton = None
XLabel = None
XLabelFrame = None # NOT APPEARING ON THE GUI
XRadiobutton = None
'''=========================================================================='''
def TabbedReports( ParentFrame ) :
global ComplianceMessageText, NotebookFrame
global SelectReportFrame, UplinkFileWarningText
global XCheckbutton, XLabel, XLabelFrame, XRadiobutton
# Builds the notebook and it's widgits
NotebookFrame = ttk.Notebook( ParentFrame )
NotebookFrame.grid( row = 0, column = 1 )
Tab1Frame = Frame( NotebookFrame )
Tab2Frame = Frame( NotebookFrame )
NotebookFrame.add( Tab1Frame )
NotebookFrame.add( Tab2Frame )
# Create widgets on Tab 1
XLabelFrame = LabelFrame( Tab1Frame ) # NOT APPEARING ON GUI
XCheckbutton = Checkbutton( Tab1Frame )
XLabel = Label( Tab1Frame )
XRadiobutton = Radiobutton( Tab1Frame )
XLabelFrame.grid( row = 1, column = 0 ) # NOT ON GUI
XCheckbutton.grid( row = 1, column = 1 )
XLabel.grid( row = 2, column = 0 )
XRadiobutton.grid( row = 2, column = 1 )
XLabelFrame.configure( text = LanguageInUse.XLabelFrameMessage ) # NOT ON GUI
XCheckbutton.configure( text = LanguageInUse.XCheckbuttonMessage )
XLabel.configure( text = LanguageInUse.XLabelMessage )
XRadiobutton.configure( text = LanguageInUse.XRadioButtonMessage )
# .tab() gives same effect as .configure() for other widget types
NotebookFrame.tab( 0 , text = LanguageInUse.ReportTab1Title )
NotebookFrame.tab( 1 , text = LanguageInUse.ReportTab2Title )
# Create the only widget on Tab 2 (uses other method to specify text)
ComplianceMessageText = Label( Tab2Frame )
ComplianceMessageText.grid( row = 0, column = 0 )
ComplianceMessageText['text'] = LanguageInUse.ComplianceMessage
return
From “ChangeLanguageOnGui.py” the code that updates all notebook widgets:
import sys, os
from tkinter import StringVar, Radiobutton, PhotoImage
#from TkinterRoot import root
import LanguageInUse
import SelectReports
'''=========================================================================='''
def ChangeLanguageOnGui() :
SelectReports.XLabelFrame.configure( text = LanguageInUse.XLabelFrameMessage ) # NOT ON GUI
SelectReports.XCheckbutton.configure( text = LanguageInUse.XCheckbuttonMessage )
SelectReports.XLabel.configure( text = LanguageInUse.XLabelMessage )
SelectReports.XRadiobutton.configure( text = LanguageInUse.XRadioButtonMessage )
# .tab() gives the same effect as .configure() for other widget types
SelectReports.NotebookFrame.tab( 0 , text = LanguageInUse.ReportTab1Title )
SelectReports.NotebookFrame.tab( 1 , text = LanguageInUse.ReportTab2Title )
SelectReports.ComplianceMessageText['text'] = LanguageInUse.ComplianceMessage
'''=========================================================================='''
def SetUpGuiLanguage( LanguageFrame ) :
GUI_Language = StringVar( value = LanguageInUse.user_language )
#---------------------------------------------------------------------------
def switchLanguage():
LanguageInUse.user_language = GUI_Language.get()
LanguageInUse.SetGuiLanguage( LanguageInUse.user_language )
ChangeLanguageOnGui()
return
#---------------------------------------------------------------------------
UsingEnglish = Radiobutton( LanguageFrame, indicatoron = False,
variable = GUI_Language,
command = lambda: switchLanguage(),
value = LanguageInUse.English )
UsingFrancais = Radiobutton( LanguageFrame, indicatoron = False,
variable = GUI_Language,
command = lambda: switchLanguage(),
value = LanguageInUse.Francais )
UsingEnglish.grid( row = 0, column = 0 )
UsingFrancais.grid( row = 1, column = 0 )
UsingEnglish.configure( text = LanguageInUse.English )
UsingFrancais.configure( text = LanguageInUse.Francais )
From "TkinterRoot.py" the code that makes root importable everywhere (explictly importing this avoided problems such as IntVar() being unavailable during the intitialization phase of other modules):
from tkinter import Tk # possibly the worlds shortest useful python module
root = Tk() # makes root an importable "Application Level" global
And finally "A.py", the mainline file:
from TkinterRoot import root
from tkinter import LabelFrame
from tkinter import ttk
import ChangeLanguageOnGui, LanguageInUse, SelectReports, sys
LanguageFrame = None
if __name__ == "__main__":
LanguageChoice = LanguageInUse.English
if ( LanguageChoice == LanguageInUse.English ) :
LanguageInUse.user_language = LanguageChoice
elif ( LanguageChoice == 'Francais' ) :
LanguageInUse.user_language = LanguageInUse.Francais
else :
print( "Unknown Language: " + LanguageChoice )
sys.exit()
mainframe = ttk.Frame( root )
mainframe.grid( row = 0, column = 0 )
LanguageFrame = LabelFrame( mainframe, text = LanguageInUse.LanguagePrompt )
LanguageFrame.grid( row = 0, column = 0 )
ChangeLanguageOnGui.SetUpGuiLanguage( LanguageFrame )
SelectReports.TabbedReports( mainframe )
try:
root.mainloop()
except:
print( 'Exception Occurred' )
sys.exit()
The Environment is 64-bit Python 3.5.1, 64-bit Win 7 Enterprise SP 1, 64-bit Eclipse Mars 2 (the Java EE IDE edition) running 64-bit PyDev 5.1.2.201606231256. The "one user" (no admin rights) version of Pydev was used, this required Microsoft patch KB2999226 (part of Win10) to run on Win7. Eventual target distribution is a 32-bit Windows app (so it can run on 32 & 64 bit Windows) - and if/when time permits - Linux.
There’s one minor complication to keep in mind: In the real program several packages are being used. Inside each package each function is isolated inside its own file. All objects (e.g. the tkinter widgets) that must be externally visible (or need be shared amongst the package’s files) are declared in the package’s _ _ init.py _ _ file. Functions that must be externally visible are explicitly imported into _ _ init.py _ _ by using relative imports (e.g. “from .Function1 import Function1” ).
Your title is misleading because you place the LabelFrame in a Frame, not directly on a Notebook tab. Your problem is that the labelframe does not appear in its parent frame. The Notebook is irrelevant and all associated code should have been deleted before posting.
Even the frame is irrelevant in that the same problem arises when putting the labelframe directly in root. Here is minimal code that demonstrate both the problem and a solution.
from tkinter import Tk
from tkinter import ttk
root = Tk()
lf1 = ttk.LabelFrame(root, text='labelframe 1')
lf2 = ttk.LabelFrame(root, text='labelframe 2', width=200, height=100)
lf1.pack()
lf2.pack()
I ran this on Win 10 with 3.6a2, which comes with tk 8.6.4. Only lf2 is visible because the default size of a labelframe, as with a frame, is 0 x 0. A non-default size arises either from explicit sizing or from contents. Somewhat surprisingly, the label does not count as content and does not force a non-default size. I reproduced the same result with the labelframe in a frame (your situation) and on a tab.

Connect Transparency to keyframe animation Maya MelPython

I have a workaround to connect the transparency of a Metrial to the keyframe bar in Maya. I create a new Material press "s" for a keyframe got to the keyframe 10, set the transparency to 0 and press again "s".
So you are able to fade the transparency beteween the 1 and 10 keyframe.
I want to script this in python and I have no idea how to do this.
here is what I've made. I tested it with basic Maya materials, but it should work for any material wich has a transparency attribute (note that it won't work for surface shaders as the transparency attribute is called outTransparency, but you could fix it by changing the function so you could also pass it the attribute name).
I got the getShaderFrom (obj) function in this topic and just converted it in Python (it's in MEL).
I made it as simple and detailled as possible but it could probably be optimized. Anyway, hope this answered your question!
from maya import cmds
def getShaderFrom (obj):
'''
List all materials for the given object
'''
# List the shapes of the given object
shapes = cmds.ls (obj, objectsOnly = 1, dagObjects = 1, shapes = 1)
# List the shading engines connected to the shapes
shadingEngines = cmds.listConnections (shapes, type = "shadingEngine")
# List the materails connected to the shading engines
rawMaterials = cmds.ls ((cmds.listConnections (shadingEngines)), materials = 1)
# Remove duplicated occurences of materials
materials = []
for mat in rawMaterials:
if mat not in materials:
materials.append(mat)
return materials
def keyTransparency (startFrame, endFrame, material):
'''
Will key the transparency of
a given materials between
selected frames
'''
# Set transparency value at 1
cmds.setAttr (material + ".transparency", 1,1,1, type = "double3")
# Add a keyframe at last frame
cmds.setKeyframe (material, attribute = "transparency", time = endFrame, inTangentType = "linear", outTangentType = "linear")
# Add a keyframe at first frame
cmds.setKeyframe (material, attribute = "transparency", time = startFrame, inTangentType = "linear", outTangentType = "linear")
# Set transparency value at 0
cmds.setAttr (material + ".transparency", 0,0,0, type = "double3")
def doKeyTransparencyForMaterials (startTime, endTime):
'''
Main function
Call functions: 'getShaderFrom', 'keyTransparency'
'''
if ((not isinstance(startTime, int)) or (not isinstance(endTime, int))):
cmds.error ("You must provide start and end frame numbers (int).")
# List selected objects
selection = cmds.ls (selection = 1)
if len(selection) == 0:
cmds.error ("Nothing is selected.")
# List all materials
for obj in selection:
materials = getShaderFrom (obj)
# Key transparency
for mat in materials:
keyTransparency (startTime, endTime, mat)
print "Transparency has been successfully keyed for: " + (", ".join(materials)) + "\n",
doKeyTransparencyForMaterials (1, 10)
This is what I did.
import maya.cmds as cmds
######### Delete all existing Shader ##############
cmds.delete (cmds.ls(type='shadingDependNode'))
cmds.delete (cmds.ls(type='shadingEngine'))
######### Delete all existing Shader ##############
######### Delete all Keyframes between 0 and 20 ##############
cmds.setKeyframe( 'lambert1', t='1', at='transparency' )
cmds.cutKey ( t=(0,20) )
######### Delete all Keyframes ##############
#create Blau shader
Blau = cmds.shadingNode('phong', asShader=True, n='Blau', )
cmds.setAttr(Blau + '.color', 0.163,0.284,0.5)
#create KeyFrame Transparency ###Blau####
cmds.setKeyframe( 'Blau', t='1', at='transparency' )
cmds.setAttr ( 'Blau.transparency', 1,1,1, type = "double3")
cmds.setKeyframe( 'Blau', t='15', at='transparency' )
######### update Time to 2 and back to 0 ##############
cmds.currentTime ( 2, update=True )
cmds.currentTime ( 0, update=True )
######### update Time to 2 and back to 0 ##############
The setKeyFrame for lambert1 line is just to make the script running if no Key exists before.
I adjust the current time because your material is shown transparent if you apply it the first time.
PS: Blau is Blue in German

Categories