必须多次导入Maya.cmds,否则Maya将按钮按下按钮忘记



至于某些背景,我的代码很可能是一团糟,主要是因为我一生中只有几个小时的脚本。

用于类IM创建具有所有标准行星的太阳系,也是用户输入自己的行星的选项。一切都很好,直到我尝试让用户用UI中的按钮(目前是一个丑陋的按钮,但仍然是按钮)在行星上产生行星。如果用户按下按钮,说"太阳",那么太阳就产生了,但是只有一次,如果他决定再次产生太阳或任何其他行星,则会出现一个错误,说明该模块不包含'polycube或多球'。因此,基本上,该按钮可以工作一次,然后不再起作用。如果我从按钮以外的任何地方都打电话给它们,它可以无限期地工作。我认为,由于他不认识polycube或polysphere,所以几乎好像我没有进口maya.cmds,所以让我们尝试一下。低和看见。

基本上我的问题如下,玛雅按下按钮时会忘记其导入的库吗?我该如何解决此问题,而无需重新IMPORT MAYA.CMDS中的每个功能?

import maya.cmds as maya
class create_body:
    def __init__(self, distance, radius, bonus_scale, r, g, b):
        import maya.cmds as maya
        self.radius  = radius * bonus_scale / 1000
        self.bonusScale = bonus_scale
        self.distanceScene = distance
        self.distanceMeter = distance*1000000000
        maya.polyCube()
        self.r = r
        self.g = g
        self.b = b
    def color_body_custom(self):
        import maya.cmds as maya
        value = maya.colorEditor()
        self.color = [float(i) for i in value.split()]
        self.r = self.color[0]
        self.g = self.color[1]
        self.b = self.color[2]
    def spawn_body(self):
        import maya.cmds as maya
        maya.polySphere(r = self.radius)
        maya.move(self.distanceScene, moveZ = True)
        maya.move(0, 0, 0, ".scalePivot", ".rotatePivot", absolute=True)
        maya.polyColorPerVertex(rgb=(self.r,self.g,self.b), colorDisplayOption=True)
    def animate_body(self):
        import maya.cmds as maya
        orbitTimeYears = self.get_orbital_time()*10
        key = str(orbitTimeYears) + 'sec'
        maya.setKeyframe(v=0, at='rotateY', t=['0sec'], itt = 'spline', ott = 'spline')
        maya.setKeyframe(v=-360, at='rotateY', t=[key], itt = 'spline', ott = 'spline')
        maya.selectKey(attribute='rotateY')
        maya.setInfinity(pri='linear', poi='linear')
    def get_orbital_time(self):
        import math
        orbitMeter = self.distanceMeter * 2 * math.pi
        gravConst = 132690600000000000000 / self.distanceMeter
        orbitSpeed = math.sqrt(gravConst)
        orbitTimeSec = orbitMeter / orbitSpeed
        orbitTimeYears = orbitTimeSec / 31556926
        return orbitTimeYears
class create_ui:
    def __init__(self, window_name):
        self.myPlanetarySystem = window_name
        # Make sure there's only one window open by deleting the window if it exists
        self.delete_ui()
        # Create the UI
        self.myp = maya.window(self.myPlanetarySystem)
        maya.rowColumnLayout(numberOfColumns=3, columnWidth=[(1, 150), (2, 75), (3, 75)], columnOffset=[(1, 'left', 5)])
        maya.showWindow()
        maya.window(self.myPlanetarySystem, e=True, title='TileGenerator', w=200, h=190)
        maya.button(label = 'Sun', command = partial(self.body, 0, 695.510, 10, True, 1, 1, 0))
        maya.button(label='Mercury', command = partial(self.body, 57.9, 2.439, 1000, False, 0.2, 0.2, 0))
        #Sun = create_body(0, 695.510, 10, 1, 1, 0)
        #Sun.spawn_body()
        #Mercury = create_body(57.9, 2.439, 1000, 0.2, 0.2, 0)
        #Mercury.spawn_body()
        #Mercury.animate_body()
        #Venus = create_body(108.2, 6.051, 1000, .5, 0.2, 0)
        #Venus.spawn_body()
        #Venus.animate_body()
        #Earth = create_body(149.6, 6.971, 1000, 0, 0, 1)
        #Earth.spawn_body()
        #Earth.animate_body()
        #Mars = create_body(227.9, 3.389, 1000, 0.6, 0.1, 0)
        #Mars.spawn_body()
        #Mars.animate_body()
        #Jupiter = create_body(778.5, 69.911, 100, 0.9, 0.8, 0.5)
        #Jupiter.spawn_body()
        #Jupiter.animate_body()
        #Saturn = create_body(1433.4, 58.232, 100, 0.8, 0.8, 0.7)
        #Saturn.spawn_body()
        #Saturn.animate_body()
        #Uranus = create_body(2876.6, 25.362, 100, 0.7, 0.8, 1.0)
        #Uranus.spawn_body()
        #Uranus.animate_body()
        #Neptune = create_body(4503.4, 24.622, 100, 0.3, 0.4, 0.7)
        #Neptune.spawn_body()
        #Neptune.animate_body()
    def delete_ui(self):
        if maya.window(self.myPlanetarySystem, exists=True):
            maya.deleteUI(self.myPlanetarySystem, window=True)
    def body(self, distance, radius, bonus_scale, is_sun, r = 0.5, g = 0.5, b = 0.5, *args):
        obj =  create_body(distance, radius, bonus_scale, r, g, b)
        obj.spawn_body()
        if not is_sun:
            obj.animate_body()
create_ui('myPlanetarySystem')

答案:显然是在较短版本中导入我的maya.cmds:Maya与该程序发生冲突,我应该看到了这一点。换句话说,如果我只是将maya.cmds导入CMD,就没有更多的问题。

最新更新