Detecting Close Scene or Change Scene - python

HI I'm working with Pyqt4 to make a UI in Maya, but I want that the UI close or refresh when the user open or change Scene.
Is there a way in python to detect this change?

scriptJob is what you need.
The following might with some customization might be helpful.
import maya.cmds as cmds
def refresher():
# the function which does the closing/refreshing
pass
cmds.scriptJob(e=["NewSceneOpened", refresher])
cmds.scriptJob(e=["SceneOpened", refresher])
cmds.scriptJob(e=["flushingScene", refresher])

Related

Click Drag Function Maya Python

I am trying to write my 1St Maya Python Script......I know I have a ways to go before I don't keep getting dozens of Syntax Errors....But it's a simple script with no UI, and any even tidbits or clues I can get to get it to work will set me off with scripting.
Just a simple fuction of 'Clicking and dragging' visibilities on or off of the Display Layer boxes..the main function I am trying to define is:
draggerContext?
or
cmds.selectPref(clickBoxSize=True)
how to implement the fuction on/off..
any clues would be of help as the trickiest part of this script is defining the Function of CLick Drag select...
Thanks
my script that does not work yet:
import maya.cmds as cmds
#Click and drag to Turn On/Off Visibilities of the Display Layers
draggerContext_id = "dga"
def dga():
cmds.selectPref(clickBoxSize=True)
cmds.selectcmds.selectPref(clickBoxSize=True)
if:
clickdragBoxSize.sel=True(layerEditorLayerButtonVisibilityChange):
cmds.selectPref(clickBoxSize=True)
else:
cmds.select(layerEditorLayerButtonVisibilityChange=True)
SelectLayerEditorButtonVisibility()

Maya Python: Button always at the center of the window

I'm starting experimenting with Maya python, and I'm trying to do some UI.
I came across to a really strange problem, I can't get a button to stay in the center of the windows.
I've tried different things but nothing seems to work, here is the code:
import maya.cmds as cmds
cmds.window( width=200 )
WS = mc.workspaceControl("dockName", retain = False, floating = True,mw=80)
submit_widget = cmds.rowLayout(numberOfColumns=1, p=WS)
cmds.button( label='Submit Job',width=130,align='center', p=submit_widget)
cmds.showWindow()
this is a simple version but still, I can't get it to work.
can someone help me?
I honestly don't know the answer as anytime I have to dig into Maya's native UI stuff it makes me question my own life.
So I know it's not exactly what you're asking for, but I'll opt with this: Use PySide instead. At first glance it might make you go "woah, that's way too hard", but it's also a million times better (and actually easier). It's much more powerful, flexible, has great documentation, and also used outside of Maya (so actually useful to learn). Maya's own interface uses the same framework, so you can even edit it with PySide once you're more comfortable with it.
Here's a bare-bones example to create a centered button in a window:
# Import PySide libraries.
from PySide2 import QtCore
from PySide2 import QtWidgets
class MyWindow(QtWidgets.QWidget): # Create a class for our window, which inherits from `QWidget`
def __init__(self, parent=None): # The class's constructor.
super(MyWindow, self).__init__(parent) # Initialize its `QWidget` constructor method.
self.my_button = QtWidgets.QPushButton("My button!") # Create a button!
self.my_layout = QtWidgets.QVBoxLayout() # Create a vertical layout!
self.my_layout.setAlignment(QtCore.Qt.AlignCenter) # Center the horizontal alignment.
self.my_layout.addWidget(self.my_button) # Add the button to the layout.
self.setLayout(self.my_layout) # Make the window use this layout.
self.resize(300, 300) # Resize the window so it's not tiny.
my_window_instance = MyWindow() # Create an instance of our window class.
my_window_instance.show() # Show it!
Not too bad, right?

Python : Add a new Tab in Maya Main UI

I'm a beginner in programing in Maya, and I want to create a new Window INSIDE the main UI of Maya. The ideal place is in the same place that the attribute editor. I know it's possible because XGen does that, but I really do not find how :'(
Like here !
You can create a window with maya.cmds scripting and make this window dockable in maya
https://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/CommandsPython/dockControl.html
Using the dockControl example:
import maya.cmds as cmds
myWindow = cmds.window()
buttonForm = cmds.formLayout( parent = myWindow )
cmds.button( parent = buttonForm )
allowedAreas = ['right']
cmds.dockControl( area='right', content=myWindow, allowedArea=allowedAreas )
you can create any arbitrary window and enforce/allow it to dock at a specific location.
The above example will spawn and restrict 'myWindow' to the Right side of your UI, unless you undock it...
Extending the allowedAreas attribute gives more location options if you want to use them.

First time loading a window in new scene, it opens two

I have this code.....
import sys
sys.path.insert(0, '...')
import rotTool
reload(rotTool)
rotTool()
First time loading it into maya I get two windows. I realize it's because of the reload there, but without it, it won't load if you closed the window and need to reopen (while in the same session). How best should I fix this?
Thanks in advance
Let's say inside rotTool.py it looks something like this:
from PySide import QtGui
class RotTool(QtGui.QWidget):
def __init__(self, parent = None):
super(RotTool, self).__init__(parent)
self.resize(400, 400)
def run(self):
self.show()
It's a class that inherits from a QWidget, and when you call the run function, it opens up a window and shows it. Maybe you're not using PySide but the same idea should apply.
In Maya, if I need to open this window I execute:
import rotTool # Import module
myWin = rotTool.RotTool() # Create an instance of the class
myWin.run() # Call the instance's run method to open it up
If I close the window, I can just execute the last 2 lines again to create a new instance that opens the window. There could be a bit more to it than that, like making sure 2 windows can't be open at the same time, but this is how I would generally go about it.
I'm not sure what's in your module so it's hard to give specific advice to it. Instead of opening your window at the bottom of your script, you might just need to wrap it around a function, like my run() example, so that it doesn't automatically open on an import or reload. Hope that helps!

Interacting with a gtk.container while gtk.main() is executing?

Experimenting with a battery monitor icon at the moment in Python using pygtk and egg.trayicon to create an icon to display a battery icon/tooltip.
I seem to be able to add the icon and the tooltip text, but when it then reaches the gtk.main() stage I need a way to modify these so it can then show the updated values.
I've tried gobject.idle_add() and gobject.timeout_add() without much luck, not sure where to go from this.
Anyone got any ideas?
EDIT: Perhaps not the clearest of questions.
I need to loop, fetching information from acpi while running and apply it to widgets inside the gtk container.
EDIT 2: Ok, it's properly down now. The issue was that I wasn't returning anything inside my callback. I just gave it "return 123" and now it's happily chugging away in my system tray, notifying me of my battery percentage :)
This example works for me:
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
import gobject
import gtk
from egg import trayicon
label = gtk.Label("Over here")
def callback(widget, ev):
label.set_text("You found me")
def timeout():
label.set_text("What are you waiting for?")
tray = trayicon.TrayIcon("TrayIcon")
box = gtk.EventBox()
box.add(label)
tray.add(box)
tray.show_all()
box.connect("button-press-event", callback)
gobject.timeout_add(3000L, timeout)
gtk.main()
Without seeing your code, it's hard to tell what doesn't work.

Categories