maya python 定时器多线程问题
2017-06-27 17:17
302 查看
在maya实现一下python定时器线程控制
不过简单的可行,建立物体等操作会有崩溃问题发生
并报出 r6025 pure virtual function call或者'Cannot convert data of type string[] to type string
然后就崩溃了
但是平常的移动之类没什么问题
可以用来刷新一些东西
另外这种方法不会阻塞maya的主线程,也就是说你可以一边操作一边看
以下代码操作注意有可能会导致maya崩溃
上面的就会崩溃
下面的就不会,估计是创建的问题(需要新建一个叫sphere的物体)
from threading import Timer
import time
class InfiniteTimer():
"""A Timer class that does not stop, unless you want it to."""
def __init__(self, seconds, target):
self._should_continue = False
self.is_running = False
self.seconds = seconds
self.target = target
self.thread = None
def _handle_target(self):
self.is_running = True
self.target()
self.is_running = False
self._start_timer()
def _start_timer(self):
if self._should_continue: # Code could have been running when cancel was called.
self.thread = Timer(self.seconds, self._handle_target)
self.thread.start()
def start(self):
if not self._should_continue and not self.is_running:
self._should_continue = True
self._start_timer()
else:
print("Timer already started or running, please wait if you're restarting.")
def cancel(self):
if self.thread is not None:
self._should_continue = False # Just in case thread is running and cancel fails.
self.thread.cancel()
else:
print("Timer never started or failed to initialize.")
def tick():
cmds.polySphere(r=True)
print('shenmifangke')
# Example Usage
t = InfiniteTimer(1.5, tick)
t.start()
但是测试后发现很容易崩溃
这是什么原因呢
import threading
import time
import maya.utils as utils
def example(interval, ):
global run_timer = True
def your_function_goes_here():
cmds.polySphere(r=True)
while run_timer:
time.sleep(interval)
utils.executeDeferred(your_function_goes_here)
# always use executeDeferred or evalDeferredInMainThreadWithResult if you're running a thread in Maya!
t = threading.Thread(None, target = example, args = (1,) )
t.start()
然后又找到了上面的代码,完全不会崩溃
原因就是
作者代码里也写到了
这个就是在建立物体后没有延迟和maya产生的冲突使得maya崩溃了
然后上面代码当然能进行改造了
参考网址
https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds https://stackoverflow.com/questions/21164697/how-to-execute-a-maya-mel-procedure-at-regular-intervals http://download.autodesk.com/global/docs/maya2012/zh_cn/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d28e683677
不过简单的可行,建立物体等操作会有崩溃问题发生
并报出 r6025 pure virtual function call或者'Cannot convert data of type string[] to type string
然后就崩溃了
但是平常的移动之类没什么问题
可以用来刷新一些东西
另外这种方法不会阻塞maya的主线程,也就是说你可以一边操作一边看
以下代码操作注意有可能会导致maya崩溃
import threading as thd import time import maya.cmds as cmds global aaa aaa = 1 def fn(): global aaa aaa += 1 cmds.polySphere(n='mySphere'+str(aaa),sx=1, sy=1, r=1) cmds.select( clear=True ) thd.Timer(1,fn).start() fn()
上面的就会崩溃
下面的就不会,估计是创建的问题(需要新建一个叫sphere的物体)
import threading as thd import time import maya.cmds as cmds global aaa aaa = 1 def fn(): global aaa aaa += 1 cmds.setAttr( 'sphere.translateX', aaa ) cmds.select( clear=True ) thd.Timer(1,fn).start() fn()然后下面是可以使用的定时器,但是关闭后无法重开
from threading import Timer,Thread,Event import time class myTimer(): def __init__(self,t,hFunction): self.t=t self.hFunction = hFunction self.thread = Timer(self.t,self.handle_function) def handle_function(self): self.hFunction() self.thread = Timer(self.t,self.handle_function) self.thread.start() def start(self): self.thread.start() def cancel(self): self.thread.cancel() def printer(): print time.time() t = myTimer(1,printer) def defaultButtonPush(*args): print 'shenmifangke.' def defaultButtonPush2(*args): t.start() def defaultButtonPush3(*args): t.cancel() cmds.window( width=150 ) cmds.columnLayout( adjustableColumn=True ) cmds.button( label='test', command=defaultButtonPush ) cmds.button( label='开启', command=defaultButtonPush2) cmds.button( label='关闭', command=defaultButtonPush3 ) cmds.showWindow()后来网上查到这个是可以使用的,这个在cancel后可以重开
from threading import Timer
import time
class InfiniteTimer():
"""A Timer class that does not stop, unless you want it to."""
def __init__(self, seconds, target):
self._should_continue = False
self.is_running = False
self.seconds = seconds
self.target = target
self.thread = None
def _handle_target(self):
self.is_running = True
self.target()
self.is_running = False
self._start_timer()
def _start_timer(self):
if self._should_continue: # Code could have been running when cancel was called.
self.thread = Timer(self.seconds, self._handle_target)
self.thread.start()
def start(self):
if not self._should_continue and not self.is_running:
self._should_continue = True
self._start_timer()
else:
print("Timer already started or running, please wait if you're restarting.")
def cancel(self):
if self.thread is not None:
self._should_continue = False # Just in case thread is running and cancel fails.
self.thread.cancel()
else:
print("Timer never started or failed to initialize.")
def tick():
cmds.polySphere(r=True)
print('shenmifangke')
# Example Usage
t = InfiniteTimer(1.5, tick)
t.start()
但是测试后发现很容易崩溃
这是什么原因呢
import threading
import time
import maya.utils as utils
def example(interval, ):
global run_timer = True
def your_function_goes_here():
cmds.polySphere(r=True)
while run_timer:
time.sleep(interval)
utils.executeDeferred(your_function_goes_here)
# always use executeDeferred or evalDeferredInMainThreadWithResult if you're running a thread in Maya!
t = threading.Thread(None, target = example, args = (1,) )
t.start()
然后又找到了上面的代码,完全不会崩溃
原因就是
executeDeferred
作者代码里也写到了
这个就是在建立物体后没有延迟和maya产生的冲突使得maya崩溃了
然后上面代码当然能进行改造了
参考网址
https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds https://stackoverflow.com/questions/21164697/how-to-execute-a-maya-mel-procedure-at-regular-intervals http://download.autodesk.com/global/docs/maya2012/zh_cn/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d28e683677
相关文章推荐
- python多线程的问题示例
- 多线程环境下用swig做python扩展库时的问题:如何做阻塞函数的扩展?
- Python多线程问题的资料查找与汇总by tsy
- python 多线程问题
- Python的多线程性能问题和并发问题
- python 多线程时,urllib2的古怪问题
- python 多线程 join 的 细节问题 注意使用事项
- 【iOS开发-55】图片轮播案例:scrollView的分页、滚动条、利用代理控制定时器和Page Control以及多线程问题
- 多线程环境下用swig做python扩展库时的问题:如何做阻塞函数的扩展?
- python多线程简单同步问题
- [Python] Python 多线程 Envent 解决数据共享问题
- 关于python的多线程问题
- Java 中的多线程-两种创建方式,定时器的应用,线程的安全问题可以用银行转账来说明
- python多线程ctrl-c退出问题
- python 多线程服务器验证,实现断开重连,信号量解决主线程Ctrl+C不退出问题,多线程打印错行问题
- Python多线程下的_strptime问题
- python多线程ctrl-c退出问题
- 关于 Maya Python API 中的简单类型的一些问题
- python关于多线程正则爬取邮件地址造成大量回溯CPU爆满,界面卡顿的问题
- Python 中用 Ctrl+C 终止多线程程序的问题解决