Execfile with loaded script errors



  • On 31/07/2018 at 20:04, xxxxxxxx wrote:

    Hi everyone,

    I've got two scripts that seemingly look like they're working correctly but put both together they don't. The main part is a script that iterates through all objects in a file, selects the ones with a specific name and then changes an attribute. However, that script doesn't work properly unless there's an object already selected so I am calling it from another script that will select the first object if none are already selected. Now I'm getting "not defined" errors from the main script because I guess it doesn't know that it's in the current document?

    import c4d
      
      
    def recur_iter(obj,ref) :
        
        while obj:
            if obj.GetName() == 'V1' and obj[c4d.LIGHT_VLTYPE] == 2:
                print obj.GetName() + ' Now Has No Volumetrics'
                obj[c4d.LIGHT_VLTYPE]=0
            elif obj.GetName() == 'V1' and obj[c4d.LIGHT_VLTYPE] == 0:
                print obj.GetName() + ' Now Has Volumetrics'
                obj[c4d.LIGHT_VLTYPE]=2
            recur_iter(obj.GetDown(),ref)
            obj = obj.GetNext()
      
    def recur_iter2(obj2,ref2) :
        
        while obj2:
            if obj2.GetName() == 'V2' and obj2[c4d.LIGHT_VLTYPE] == 2:
                print obj2.GetName() + ' Now Has No Volumetrics'
                obj2[c4d.LIGHT_VLTYPE]=0
            elif obj2.GetName() == 'V2' and obj2[c4d.LIGHT_VLTYPE] == 0:
                print obj2.GetName() + ' Now Has Volumetrics'
                obj2[c4d.LIGHT_VLTYPE]=2
            recur_iter2(obj2.GetDown(),ref2)
            obj2 = obj2.GetNext()
      
    def main() :
      
        ref = op[c4d.LIGHT_BRIGHTNESS]
        ref2 = op[c4d.LIGHT_BRIGHTNESS]    
        recur_iter(doc.GetFirstObject(),ref)
        recur_iter2(doc.GetFirstObject(),ref2)
        
        
        c4d.EventAdd()
       
      
    if __name__=='__main__':
        main()
    
    import c4d, os
      
      
      
    def main() :
        
        FirstObject = doc.GetFirstObject()
                
        selec = doc.GetActiveObject()
        
        if selec == None:
            FirstObject.SetBit(c4d.BIT_ACTIVE)
            
        else:
            pass
            
        c4d.EventAdd()         
        
      
        execfile(c4d.storage.GeGetC4DPath(c4d.C4D_PATH_LIBRARY_USER)+os.sep+"scripts"+os.sep+"VolumetricSwitch.py")
      
            
        c4d.EventAdd()
        
    if __name__=='__main__':
        main()
    


  • On 01/08/2018 at 09:40, xxxxxxxx wrote:

    Hi,

    It seems you're getting "not defined" errors for 'op' variable in the executed script with execfile().
    'op' is None when there is no selected object before running the main script.
    So in the executed script, if 'op' is None it should then be assigned the active object returned by doc.GetActiveObject().



  • On 02/08/2018 at 10:24, xxxxxxxx wrote:

    Thanks, that pointed me in the right direction. 
    This worked for me

        doc = c4d.documents.GetActiveDocument() # Get the open document
      
        obj = c4d.documents.BaseDocument.GetFirstObject(doc) # Get first object in hierarchy
        
        op = obj
    

Log in to reply