2014-05-23 2 views
0

저는 블렌더에서 파이썬을 가르치기 때문에 스크립트를 사용하여 간단한 연산자를 만들려고했습니다. 스크립트는 아래에 있습니다 - 의도는 현장에서 네 개의 (스폿) 램프를 선택하고 에너지를 변경하는 것입니다 (기본적으로 조명을 켜고 끕니다). 그러나 스크립트를 실행하려고하면 "Python 스크립트 실패"라는 오류 메시지가 나타납니다. 코드에 어떤 문제가 있는지 누구나 볼 수 있습니까?블렌더 : 파이썬 스크립트가 실패했습니다 (간단한 연산자 생성)

import bpy 


def main(context): 
    for ob in context.scene.objects: 
     print(ob) 

class LightsOperator(bpy.types.Operator): 

    bl_idname = "object.lights_operator" 
    bl_label = "Headlight Operator" 

    @classmethod 
    def poll(cls, context): 
     return context.active_object is not None 

    def execute(self, context): 
     light1 = bpy.data.objects['headlight1'] 
     light2 = bpy.data.objects['headlight2'] 
     light3 = bpy.data.objects['headlight3'] 
     light4 = bpy.data.objects['headlight4'] 

     if light1.energy==0.0: 
      light1.energy = 0.8 
     else: 
      light1.energy = 0.0 

     if light2.energy==0.0: 
      light2.energy = 0.8 
     else: 
      light2.energy = 0.0 

     if light3.energy==0.0: 
      light3.energy = 0.8 
     else: 
      light3.energy = 0.0 

     if light4.energy==0.0: 
      light4.energy = 0.8 
     else: 
      light4.energy = 0.0 

     return {'FINISHED'} 

    def register(): 
     bpy.utils.register_class(LightsOperator) 


    def unregister(): 
     bpy.utils.unregister_class(LightsOperator) 

if __name__ == "__main__": 
register() 

# test call 
bpy.ops.object.lights_operator() 
+1

실제 들여 쓰기가 맞습니까? – jonrsharpe

+0

블렌더에 관한 질문을 게시하려면 [이 전용 StackExchange 사이트] (http://blender.stackexchange.com/) –

+0

블렌더 전문 파이썬 밖에서 파이썬을 배우는 것이 좋습니다 ... 블렌더 파이썬이 사용하기 쉽습니다 unpythonic 또는 반 직관적 인 것으로 간주되는 몇 가지 사항. –

답변

0

첫 번째 문제는 들여 쓰기 (이 편집으로 변경 있는지 확실하지 않습니다)입니다 - 그들은 안 클래스의 그들에게 부분하게 등록하고 등록 해제 들여 쓰기, 그들을 모듈 만들기 위해 그들을 해제 들여 기능. 이렇게하면 register()가 호출되어 클래스를 사용할 수있게됩니다. bpy.ops.object.lights_operator()

주된 문제는 에너지가 객체의 속성이 아니라면 객체 아래의 에너지 속성을 찾을 수 있다는 것입니다. 빛. 당신이 만들 수

if light1.data.energy==0.0: 
    light1.data.energy = 0.8 
else: 
    light1.data.energy = 0.0 

다른 어떤 개선 - 설문 조사 기능에서

당신은 더 구체적인 수 있습니다. 선택한 것을 선택하는 대신, 원하는 것을 선택하십시오.

return context.active_object.type == 'LAMP' 

모든 개체에 대해 동일한 코드를 다시 입력하는 대신 루프와 테스트를 사용하여 모든 개체에 동일한 코드를 사용할 수 있습니다. 이 짧은 스크립트로 당신을 떠날 수 있습니다 -

import bpy 

class LightsOperator(bpy.types.Operator): 
    bl_idname = "object.lights_operator" 
    bl_label = "Headlight Operator" 

    @classmethod 
    def poll(cls, context): 
     return context.active_object.type == 'LAMP' 

    def execute(self, context): 
     for object in bpy.data.objects: 
      # object.name[:9] will give us the first 9 characters of the name 
      if object.type == 'LAMP' and object.name[:9] == 'headlight': 
       if object.data.energy == 0.0: 
        object.data.energy = 0.8 
       else: 
        object.data.energy = 0.0 
     return {'FINISHED'} 

def register(): 
    bpy.utils.register_class(LightsOperator) 


def unregister(): 
    bpy.utils.unregister_class(LightsOperator) 

if __name__ == "__main__": 
    register() 

# test call 
bpy.ops.object.lights_operator() 
+0

새 설문 조사 기능으로 문제가 해결되었다고 생각합니다. 고마워요! – user3669644

관련 문제