1

这是我的脚本:

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 的模块

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

谢谢!

4

1 回答 1

0

看来您正在关注教程。您误解的是代码示例中的最后两行不应该是脚本的一部分,但它们是为了在解释器中运行以前的代码。如果您再看一下本教程,您会发现主代码示例上方有一个标题为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)

于 2012-06-27T01:56:31.510 回答