摘要:本文将带你了解Maya教程之maya python 定时器多线程问题,希望本文对大家学Maya有所帮助
本文将带你了解Maya教程之maya python 定时器多线程问题,希望本文对大家学Maya有所帮助
在maya实现一下python定时器线程控制
不过简单的可行,建立物体等操作会有崩溃问题发生
并报出 r6025 pure virtual function call或者'Cannot convert data of type string[] to type string
然后就崩溃了
但是平常的移动之类没什么问题
可以用来刷新一些东西
另外这种方法不会阻塞maya的主线程,也就是说你可以一边操作一边看
以下代码操作注意有可能会导致maya崩溃
import threading as thdimport timeimport maya.cmds as cmds global aaaaaa = 1def 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 thdimport timeimport maya.cmds as cmds global aaaaaa = 1def 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,Eventimport 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 Timerimport 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 Usaget = InfiniteTimer(1.5, tick)t.start()
但是测试后发现很容易崩溃这是什么原因呢
import threadingimport timeimport 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
//download.autodesk.com/global/docs/maya2012/zh_cn/index.html?url=files/Python_Python_in_Maya.htm,topicNumber=d28e683677
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Maya频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号