必须多次导入maya.cmds,否则maya在按下按钮后会忘记它

时间:2019-03-08 00:24:54

标签: python-2.7 button scripting python-import maya

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

对于im类,使用所有标准行星创建太阳系,并且用户可以选择输入自己的行星。一切工作正常,直到我尝试让用户使用UI中的按钮(目前是丑陋的按钮,但仍然是按钮)来生成行星。如果用户按下一个按钮,说“ Sun”,那么太阳就产生了,但是只有一次,如果他随后决定再次产生太阳或其他任何行星,则产生错误,指出该模块不包含“ polycube或polysphere”。因此,基本上,该按钮只工作一次,然后不再工作。如果我从按钮以外的任何位置调用它们,则它将完美且无限期地工作。我想起了,因为他不了解polycube或polysphere,几乎就像我没有导入maya.cmds一样,所以让我们回想一下。低而见效。

所以基本上我的问题如下,当按下按钮时,maya是否会忘记其导入的库?以及如何解决此问题而不必在每个函数中重新导入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')

1 个答案:

答案 0 :(得分:0)

答案:显然是在较短的版本中导入了我的maya.cmds:maya与程序冲突,我应该已经看到了这种情况。换句话说,如果我只是将maya.cmds导入为cmds,就不会有其他问题。