#Error:ImportError:没有名为samples.lightIntensity的模块#

时间:2012-06-27 00:29:38

标签: python module maya

这是我的剧本:

import maya.cmds as cmds

def changeLightIntensity(percentage=1.0):
    """
    Changes the intensity of each light the scene by percentage.

    Parameters:
        percentage - Percentage to change each light's intensity. Default value is 1.

    Returns:
        Nothing.
    """
    #The is command is the list command. It is used to list various nodes
    #in the  current scene. You can also use it to list selected nodes.
    lightInScene = cmds.ls(type='light')

    #If there are no lights in the scene, there is no point running this script
    if not lightInScene:
        raise RunTimeError, 'There are no lights in the scene!'

    #Loop through each light
    for light in lightInScene:
        #The getAttr command is used to get attribute values of a node
        currentIntensity = cmds.getAttr('%s.intensity' % light)
        #Calculate a new intensity
        newIntensity = currentIntensity * percentage
        #Change the lights intensity to the new intensity
        cmds.setAttr('%s.intensity' % light, newIntensity)
        #Report to the user what we just did
        print 'Changed the intensity of light %s from %.3f to %.3f' % (light, currentIntensity, newIntensity)


import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)

我的错误:

  

ImportError:没有名为samples.lightIntensity的模块

有什么问题?我可以这样做吗?解决方案是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎正在关注this教程。你误解的是你的代码示例中的最后两行不应该是脚本的一部分,但是它们意味着在解释器中运行前面的代码。如果您再看一下教程,您会看到主代码示例上方有一个标题,表示 lightIntensity.py ,而第二个较小的代码示例前面是“要运行这个脚本,在脚本编辑器中输入...“

所以,这个:

import samples.lightIntensity as lightIntensity
lightIntensity.changelightIntensity(1.2)

不应该以该格式存在于您的文件中。

你可以做两件事。两者都应该解决问题,并允许您运行代码,但我更喜欢第二个易用性。

  1. 保存没有最后两行的代码lightIntensity.py,并在python shell中,(在命令行上启动python,IDLE,无论你使用什么),并在提示后,键入import lightIntensity以导入脚本,并lightIntensity.changelightIntensity(1.2)调用脚本中的函数。

  2. 或者,您可以修复脚本以使其运行而不尝试导入自身。为此,请移除import行并将最后一行更改为changelightIntensity(1.2)