maya python 定时器多线程问题

在maya实现一下python定时器线程控制

不过简单的可行,建立物体等操作会有崩溃问题发生

并报出 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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值