0
$\begingroup$

Can some one help me with my blender ftp code, It runs fine in the text edit but when it turn it into a plugin and run it there is an error with initSceneProperties(bpy.context.scene) and I can not get to run at all.

bl_info = {
    "name": "FTP Download",
    "category": "Object",
}
from ftplib import FTP
import bpy
from bpy.props import *


def initSceneProperties(scn):
    bpy.types.Scene.Ip = StringProperty(
        name = "Ip")                      # IP
    scn['Ip'] = "000.00.000.000"

    bpy.types.Scene.Uname = StringProperty(
        name = "User Name")                # user name
    scn['Uname'] = "anonymous"

    bpy.types.Scene.Pword = StringProperty(
            name = "Password")              # password
    scn['Pword'] = ""    

    bpy.types.Scene.SFN = StringProperty(
                name = "Server File Name")      # server file name          =   SFN
    scn['SFN'] = "File name"    

    bpy.types.Scene.SFD = StringProperty(
                name = "Server File Directory")      # server file directory     =   SFD
    scn['SFD'] = "File directory"    

    bpy.types.Scene.CFN = StringProperty(
                name = "Computer File Name")      # computer file name          =   CFN
    scn['CFN'] = "File name"    

    bpy.types.Scene.CFD = StringProperty(
                name = "Computer File Directory")      # computer file directory     =   CFD
    scn['CFD'] = "File directory"    
    return
initSceneProperties(bpy.context.scene)


class UIPanel(bpy.types.Panel):
    bl_label = "Property panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):

        layout = self.layout
        scn = context.scene
        layout.prop(scn, 'Ip')
        layout.prop(scn, 'Uname')
        layout.prop(scn, 'Pword')
        layout.prop(scn, 'SFN')
        layout.prop(scn, 'SFD')
        layout.prop(scn, 'CFN')
        layout.prop(scn, 'CFD')
        layout.operator("ftp.download", text='Open', icon='FILE_FOLDER')
        layout.operator("ftp.upload", text='Save', icon='FILE_TICK')

class FTPDownload(bpy.types.Operator):
    bl_idname = "ftp.download"         # unique identifier for buttons and menu items to reference.
    bl_label = "FTP download"          # display name in the interface.

    def execute(self, context):
        scn = context.scene
        #------------------------------------#
        ip = scn['Ip']
        uname = scn['Uname']
        pword = scn['Pword'] 

        ftp = FTP(ip)                        # login
        print("Loging into", ip)             #
        print(ftp.login(uname,pword))#
        #------------------------------------# 
        sfd = scn['SFD']
        ftp.cwd(sfd)                         # Path
        filename = scn['SFN']                # Filename 
        parth = scn['CFD']                   # Destination parth 
        new_name = scn['CFN']                # Destination name
        #------------------------------------#
        local_filename = os.path.join(parth, new_name)
        file = open(local_filename,'wb')
        ftp.retrbinary('RETR ' + filename, file.write)
        print("Receiving file")
        print(ftp.quit())
        file.close()
        return {'FINISHED'}

class FTPUpload(bpy.types.Operator):
    bl_idname = "ftp.upload"           # unique identifier for buttons and menu items to reference.
    bl_label = "FTP upload"            # display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # enable undo for the operator.

    def execute(self, context):
        scn = context.scene
        #------------------------------------+
        ip = scn['Ip']
        uname = scn['Uname']
        pword = scn['Pword']      
        ftp = FTP(ip)                        # login
        print("Loging into", ip)             #
        print(ftp.login(uname,pword)) 
        #------------------------------------+
        parth = scn['CFD']                   # Path
        filename = scn['CFN']                # Filename
        new_name = scn['SFN']
        sfd = scn['SFD']
        ftp.cwd(sfd)      
        #------------------------------------+
        local_filename = os.path.join(parth, filename)
        ftp.storbinary('STOR ' +new_name, open(local_filename,'rb'))
        print("Receiving file")
        print(ftp.quit())
        return {'FINISHED'}


def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
$\endgroup$
0
$\begingroup$

After you define initSceneProperties() you then call the function, you need to remove that, to call the function when the addon is enabled you can add it to register(). The error you are getting is because when the addon is enabled there is no context (you are trying to use bpy.context.scene), you cannot access any of blender's data during the register process.

It would appear that you want to set a default value after you add properties to the scene class, to do this you can set the default value for the property.

bpy.types.Scene.Ip = StringProperty(name = "Ip", default="000.00.000.000")

That would lead you to the following version of your script -

bl_info = {
    "name": "FTP Download",
    "description": "Transfer files to/from an ftp server",
    "author": "Ashlin",
    "version": (1, 0),
    "blender": (2, 73, 0),
    "location": "3D View > Properties panel",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Object"}

from ftplib import FTP
import bpy
from bpy.props import *


def initSceneProperties():
    # server IP address
    bpy.types.Scene.Ip = StringProperty(
        name = "Ip", default="000.000.000.000")

    # ftp user name
    bpy.types.Scene.Uname = StringProperty(
        name = "User Name", default="anonymous")

    # password
    bpy.types.Scene.Pword = StringProperty(
        name = "Password", default="")

    # server file name
    bpy.types.Scene.SFN = StringProperty(
        name = "Server File Name", default="File name")

    # server file directory
    bpy.types.Scene.SFD = StringProperty(
        name = "Server File Directory", default="File directory")

    # local file name
    bpy.types.Scene.CFN = StringProperty(
        name = "Computer File Name", default="File name")

    # local file directory
    bpy.types.Scene.CFD = StringProperty(
        name = "Computer File Directory", default="File directory")


class UIPanel(bpy.types.Panel):
    bl_label = "FTP Downloader"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):

        layout = self.layout
        scn = context.scene
        layout.prop(scn, 'Ip')
        layout.prop(scn, 'Uname')
        layout.prop(scn, 'Pword')
        layout.prop(scn, 'SFN')
        layout.prop(scn, 'SFD')
        layout.prop(scn, 'CFN')
        layout.prop(scn, 'CFD')
        layout.operator("ftp.download", text='Open', icon='FILE_FOLDER')
        layout.operator("ftp.upload", text='Save', icon='FILE_TICK')

class FTPDownload(bpy.types.Operator):
    bl_idname = "ftp.download"         # unique identifier for buttons and menu items to reference.
    bl_label = "FTP download"          # display name in the interface.

    def execute(self, context):
        scn = context.scene
        #------------------------------------#
        ip = scn['Ip']
        uname = scn['Uname']
        pword = scn['Pword']

        ftp = FTP(ip)                        # login
        print("Loging into", ip)             #
        print(ftp.login(uname,pword))#
        #------------------------------------#
        sfd = scn['SFD']
        ftp.cwd(sfd)                         # Path
        filename = scn['SFN']                # Filename
        parth = scn['CFD']                   # Destination parth
        new_name = scn['CFN']                # Destination name
        #------------------------------------#
        local_filename = os.path.join(parth, new_name)
        file = open(local_filename,'wb')
        ftp.retrbinary('RETR ' + filename, file.write)
        print("Receiving file")
        print(ftp.quit())
        file.close()
        return {'FINISHED'}

class FTPUpload(bpy.types.Operator):
    bl_idname = "ftp.upload"           # unique identifier for buttons and menu items to reference.
    bl_label = "FTP upload"            # display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # enable undo for the operator.

    def execute(self, context):
        scn = context.scene
        #------------------------------------+
        ip = scn['Ip']
        uname = scn['Uname']
        pword = scn['Pword']
        ftp = FTP(ip)                        # login
        print("Loging into", ip)             #
        print(ftp.login(uname,pword))
        #------------------------------------+
        parth = scn['CFD']                   # Path
        filename = scn['CFN']                # Filename
        new_name = scn['SFN']
        sfd = scn['SFD']
        ftp.cwd(sfd)
        #------------------------------------+
        local_filename = os.path.join(parth, filename)
        ftp.storbinary('STOR ' +new_name, open(local_filename,'rb'))
        print("Receiving file")
        print(ftp.quit())
        return {'FINISHED'}

def register():
    bpy.utils.register_module(__name__)
    initSceneProperties()

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
|improve this answer|||||
$\endgroup$
  • $\begingroup$ How do I add it to register() ? I am still new to python. $\endgroup$ – Ashlin Aug 20 '15 at 20:42
  • $\begingroup$ Near the bottom of your script you have def Register(): add initSceneProperties() after the next line - match the indentation. $\endgroup$ – sambler Aug 21 '15 at 16:33
  • $\begingroup$ I have added initSceneProperties() after def Register(): but i don't know what you mean by match the indentation. Sorry about this but could you give me the code to make this work. All of this code was made from examples so I have little understanding how it works. $\endgroup$ – Ashlin Aug 21 '15 at 22:51
  • $\begingroup$ I suggest you spend an hour or two on an introduction python tutorial so you at least understand why the spaces are at the start of most lines. $\endgroup$ – sambler Aug 22 '15 at 10:55
  • $\begingroup$ Thank you so much, it is so hard to find help with blender python stuff. Thank you. $\endgroup$ – Ashlin Aug 23 '15 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.