摘要:本文将带你学习Maya中实现python定时器线程控制的3个技巧,希望本文对大家学Maya有所帮助。
在maya实现一下python定时器线程控制
不过简单的可行,建立物体等操作会有崩溃问题发生
并报出 r6025 pure virtual function call或者'Cannot convert data of type string[] to type string
然后就崩溃了
但是平常的移动之类没什么问题
可以用来刷新一些东西
另外这种方法不会阻塞maya的主线程,也就是说你可以一边操作一边看
以下代码操作注意有可能会导致maya崩溃
[python] view plain copy print? 1. import threading as thd 2. import time 3. import maya.cmds as cmds 4. 5. global aaa 6. aaa = 1 7. def fn(): 8. global aaa 9. aaa += 1 10. cmds.polySphere(n='mySphere'+str(aaa),sx=1, sy=1, r=1) 11. cmds.select( clear=True ) 12. thd.Timer(1,fn).start() 13. 14. fn()
上面的就会崩溃
下面的就不会,估计是创建的问题(需要新建一个叫sphere的物体)
[python] view plain copy print? 1. import threading as thd 2. import time 3. import maya.cmds as cmds 4. 5. global aaa 6. aaa = 1 7. def fn(): 8. global aaa 9. aaa += 1 10. cmds.setAttr( 'sphere.translateX', aaa ) 11. cmds.select( clear=True ) 12. thd.Timer(1,fn).start() 13. 14. fn()
然后下面是可以使用的定时器,但是关闭后无法重开
[python] view plain copy print? 1. from threading import Timer,Thread,Event 2. import time 3. 4. class myTimer(): 5. 6. def __init__(self,t,hFunction): 7. self.t=t 8. self.hFunction = hFunction 9. self.thread = Timer(self.t,self.handle_function) 10. 11. def handle_function(self): 12. self.hFunction() 13. self.thread = Timer(self.t,self.handle_function) 14. self.thread.start() 15. 16. def start(self): 17. self.thread.start() 18. 19. def cancel(self): 20. self.thread.cancel() 21. 22. def printer(): 23. print time.time() 24. 25. 26. t = myTimer(1,printer) 27. def defaultButtonPush(*args): 28. print 'shenmifangke.' 29. def defaultButtonPush2(*args): 30. t.start() 31. def defaultButtonPush3(*args): 32. t.cancel() 33. cmds.window( width=150 ) 34. cmds.columnLayout( adjustableColumn=True ) 35. cmds.button( label='test', command=defaultButtonPush ) 36. cmds.button( label='开启', command=defaultButtonPush2) 37. cmds.button( label='关闭', command=defaultButtonPush3 ) 38. cmds.showWindow() 后来网上查到这个是可以使用的,这个在cancel后可以重开 [python] view plain copy print? 1. from threading import Timer 2. import time 3. 4. 5. class InfiniteTimer(): 6. """A Timer class that does not stop, unless you want it to.""" 7. 8. def __init__(self, seconds, target): 9. self._should_continue = False 10. self.is_running = False 11. self.seconds = seconds 12. self.target = target 13. self.thread = None 14. 15. def _handle_target(self): 16. self.is_running = True 17. self.target() 18. self.is_running = False 19. self._start_timer() 20. 21. def _start_timer(self): 22. if self._should_continue: # Code could have been running when cancel was called. 23. self.thread = Timer(self.seconds, self._handle_target) 24. self.thread.start() 25. 26. def start(self): 27. if not self._should_continue and not self.is_running: 28. self._should_continue = True 29. self._start_timer() 30. else: 31. print("Timer already started or running, please wait if you're restarting.") 32. 33. def cancel(self): 34. if self.thread is not None: 35. self._should_continue = False # Just in case thread is running and cancel fails. 36. self.thread.cancel() 37. else: 38. print("Timer never started or failed to initialize.") 39. 40. 41. def tick(): 42. cmds.polySphere(r=True) 43. print('shenmifangke') 44. 45. # Example Usage 46. t = InfiniteTimer(1.5, tick) 47. t.start()
但是测试后发现很容易崩溃
这是什么原因呢
[python] view plain copy print? 1. import threading 2. import time 3. import maya.utils as utils 4. 5. def example(interval, ): 6. global run_timer = True 7. def your_function_goes_here(): 8. cmds.polySphere(r=True) 9. 10. while run_timer: 11. time.sleep(interval) 12. utils.executeDeferred(your_function_goes_here) 13. # always use executeDeferred or evalDeferredInMainThreadWithResult if you're running a thread in Maya! 14. 15. t = threading.Thread(None, target = example, args = (1,) ) 16. t.start()
然后又找到了上面的代码,完全不会崩溃
原因就是
executeDeferred
这个就是在建立物体后没有延迟和maya产生的冲突使得maya崩溃了
然后上面代码当然能进行改造了。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标常用软件之Maya频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号