In MAYA 2009, is it possible to capture the cube rotate event? - python

I need to call a function ( Maya-Python ) based on cube rotationX. For that I have to capture the event, programmatically.
I tried using while loop but It stucks in the loop, Nothing can be done in that time.
I tried theading (python), still same.
Can it be done this or other way? If yes, How?
Maya 2009 in Windows XP
Some failed code references:
import maya.cmds as cmds
while (count < 90):
lock = cmds.getAttr('pCube1.rotateX',lock=False)
print lock
count = count + 1
Here Python wise:
#!/usr/bin/python
import thread
import time
# Define a function for the thread
def cubeRotateX( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
try:
thread.start_new_thread( cubeRotateX, ("Thread-1", 2, ) )
except:
print "Error: unable to start thread"
while 1:
pass

It sounds like a scriptJob may be what you're after. Here's a simple example below. However, in this example the callback will only be called when you release the mouse from rotating.
import maya.cmds
def myRotateCallback():
print 'do something'
maya.cmds.scriptJob( attributeChange=['pCube1.rotateX', myRotateCallback] )
If you want to receive continuous callbacks while rotating the cube, you can do that at the maya API level with MNodeMessage::addNodeDirtyPlugCallback.

Related

How to continue a code while a loop is already running

I created a code that shows a real time clock at the beginning (works by a loop and refreshing itself in every 1 sec using \r )
But I want to run the rest of the code while the clock is ticking (continuously). But this isn't going any further while the loop is running.
I think there is no need to write the code of the clock.
If you want to have a task running, while using another you can use multi-threading. This means you tell your processor two different tasks and it will be continued as long as you tell it to work. See here a post about multithreading and multiprocessing. You can use the thread function of python for this.
Here a small example:
import threading
import time
# Define a function for the thread
def print_time( threadName, delay):
count = 0
while count < 10:
time.sleep(delay)
count += 1
print ("%s: %s" % ( threadName, time.ctime(time.time()) ))
def counter(threadName, number_of_counts, delay):
count=0
while count < number_of_counts:
print ("%s: %s" % ( threadName, count))
time.sleep(delay)
count +=1
# Create two threads as follows
threading.Thread(target=print_time, args=("Thread-1", 1, )).start()
threading.Thread(target=counter, args=("Thread-2", 100, 0.1,)).start()
for further information check the documentation. Note that thread has been renamed to _thread in python 3

timer function with interval parameter in python

I call static function of the class and expected it will run per 3 seconds and only 5 times.. But it's not stop at counter =5, goes on running
I searched and found sys.exit(0) can stop the timer. Whats wrong with it ?
def senLogtoBackup():
threading.Timer(3, senLogtoBackup).start()
AccessLog.AccessLog.backupAccessLog("logbackups","example.log")
try:
senLogtoBackup()
if AccessLog.AccessLog.Counter == 5:
sys.exit(0) # expects I it will terminate the timer. but it still counts
Class definition:
class AccessLog:
Counter =0
#staticmethod
def backupAccessLog(target, source):
AccessLog.Counter+=1
print "counter",AccessLog.Counter
you searched wrong, take a look at os._exit
you should use "t.cancel" to stop timer.

Getting the drawing off the main thread

I remember seeing a post somewhere about being able to get the drawing in python off the main thread, but I can't seem to find it. My first attempt goes something like this but it doesn't work. It doesn't crash initially (it does eventually) but no drawing takes place. The idea is that options is a map of drawing functions each of which draws to a pyqtgraph, or a QTWidget, etc
from threading import *
from Queue import *
anObject1 = DrawingObject()
anObject2 = DrawingObject()
anObject3 = DrawingObject()
options = {
0 : anObject1.drawing_func,
1 : anObject2.drawing_func,
2 : anObject3.drawing_func,
3 : updateNon,
}
def do_work(item): #item is a tuple with the item at 0 is the index to which function
#print str(item) + "\n"
options[item[0]](item)
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
#This function is a callback from C++
def callback(s, tuple):
#options[tuple[0]](tuple) #this works
q.put(tuple) #this does not
num_worker_threads = 3
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
My understanding is that it is not possible to draw to a QWidget outside the main GUI thread. You can find many references to this in the Qt forums and documentation. However, it is possible to start a subprocess that draws into an image in shared memory, and then display the image in the main process. This is the approach taken by pyqtgraph/widgets/RemoteGraphicsView.py; see examples/RemoteSpeedTest.py for an example of this.

Python counting keyboard interrupts

s = signal.signal(signal.SIGINT, signal.SIG_IGN)
os.wait()
signal.signal(signal.SIGINT, s)
Currently, I have the above code. This working fine for me at the moment if someone wants to press insane amount of Ctrl+c
But I want to have an arbitrary count instead of continually ignoring it as the above. For example, I want to sys.exit() instead of let os.wait() keep going if I receive 5 Ctrl+c.
So how can i count ctrl+c?
signal can accept a functor:
import signal
import sys
class S:
cnt = 0
def __call__(self, signum, frame):
self.cnt += 1
if (self.cnt == 5):
sys.exit()
signal.signal(signal.SIGINT, S())
You obviously need a variable that you increase and compare to five.
EDIT: I did not assume you were able to handle signals but not able to introduce a python variable, but oh well:
counter = 0
def handle_sigint(signal_number, _frame):
if counter >= 5:
sys.exit(-1) #or whatever you want
else:
counter += 1
signal.signal(signal.SIGINT, handle_sigint)

Use of python progress bar in script

I want to show progress bar in my script because it takes a lots of time to execute while working on huge files.I gone through the python progressbar module
and examples also its good and very intresting to use but as per examples all values are predefine .As we can't guess the max execution time of programm or function.So i am not able to figure out how should i use progress bar function in my sctipt
for data in files:
crawl_data(data)
this is the crawl_data function which take time so how can i set the progress bar values
pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=300).start()
for i in range(300):
time.sleep(0.01)
pbar.update(i+1)
pbar.finish()
how can i define this range and maxval values in above lines of code.
This is what I got working.
Stdout
Working: | Elapsed Time: 0:00:10
Python
import time
import progressbar
import threading
def crawl_data(data):
# sleep for 10 seconds
time.sleep(10)
# end of def crawl_data
def main():
data = 'some data'
widgets = ['Working: ', progressbar.AnimatedMarker(), ' ',
progressbar.Timer()]
pbar = progressbar.ProgressBar(widgets=widgets)
# creating thread to run crawl_data()
thread = threading.Thread(target=crawl_data,
args=(data,))
thread.daemon = True
# starting thread and progress bar
thread.start()
pbar.start()
i = 1
# continuous loop until crawl_data thread is not alive
while True:
# update every second
time.sleep(1)
pbar.update(i)
if not thread.is_alive():
pbar.finish()
break
# end of if thread is not alive
i += 1
# end of continuous loop until crawl_data thread is not alive
# prints a new line
print
# end of def main
# run main
main()
If you can't guess the execution time, a progress bar is worthless (remember most of the old MS progress bars?). You are probably looking for something like a activity indicator. Since web2.0 it is common to use something rotating.

Categories