Announcement

Collapse
No announcement yet.

Python UI callbacks question?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Python UI callbacks question?

    Hi,
    I ran into a wall when trying to implement Python UI callbacks in Maya. Looking for some help to sort this out.
    env: Windows 7, Maya 2013

    I set up a custom folder structure as below:
    root_folder
    vray_script_folder
    __init__.py
    function01.py
    function02.py
    function03.py
    userSetup.py

    An environment variable is specified with the path pointing to the userSetup.py.

    userSetup.py
    Code:
    def sampleFunction():
        import vray_script_folder
        ### menu creation stuff
        cmds.menuItem(p=customMenu, l='function label', c='import vray_script_folder; vray_script_folder.function01()')
        cmds.menuItem(p=customMenu, l='function label', c='import vray_script_folder; vray_script_folder.function02()')
    __init__.py
    Code:
    import maya.cmds as cmds
    try:
        from function01 import *
        from function02 import *
    except:
        print 'something is wrong'
    function01.py
    Code:
    import maya.cmds as cmds
    def function01(arg1, arg2):
        cmds.setAttr(stuff)
        cmds.setAttr(stuff2)
    It seems to me the code is executed just fine as I am able to see and interact with the custom menu in Maya until when I execute the function01, Maya throws me a 'module' object has no attribute 'function01' error. Did I not import the function01 properly or something?

    Thanks!
    Last edited by jasonhuang1115; 05-04-2013, 11:29 AM.
    always curious...

  • #2
    I think that __init__.py should contain something different, but I don't know Python well enough to know what; I'll check. Looking at our own Python module, I would assume that in your case __init__.py just needs to be empty.

    Best regard,
    Vlado
    I only act like I know everything, Rogers.

    Comment


    • #3
      jasonhuang1115:
      Have you tried to remove all maya related code and to try this same file structure in a normal python interpreter?
      V-Ray developer

      Comment


      • #4
        hmm....the __init__.py contains the code to load Vray plugin and set Vray as the renderer in the Render Globals, plus importing python scripts placed in the vray_script_folder. I will try to find the issue through a normal python interpreter...
        always curious...

        Comment


        • #5
          Do you have an __init__.py file in the root folder?

          Right now, you aren't referencing the vray_script_folder package properly in your function. Assuming that your root folder is "script_folder" try this:

          def function01():
          import script_folder
          ### menu creation stuff
          cmds.menuItem(p=customMenu, l='function label', c='import script_folder.vray_script_folder; script_folder.vray_script_folder.fucntion01()')
          cmds.menuItem(p=customMenu, l='function label', c='import script_folder.vray_script_folder; script_folder.vray_script_folder.fucntion02()')


          Alternately, you could have an import statement in your root level __init__.py that imports the vray_script_folder.

          I'd also be careful naming your modules and functions the same thing. It looks like you misspelled "fucntion01", but if you had spelled it properly, you would be importing a module and function with the same name into the same namespace.

          Comment


          • #6
            mfessenden, I have updated my sample folder structure and correct couple typos that create confusions. The indentation shows the folder/file structure. There is only one script folder called vray_script_folder. I do not have an __init__.py in the root folder. What I have in the root_folder is the userSetup.py where vray_script_folder is imported and custom menu is created. I did put an __init__.py in the vray_script_folder to try importing functions. What you suggest for the function01.py is what I tried to achieved in the userSetup.py.

            Could you take a look at the sample code again and let me know what you think?
            Last edited by jasonhuang1115; 05-04-2013, 11:35 AM.
            always curious...

            Comment


            • #7
              Try renaming your functions differently from the modules they are stored in.

              Comment

              Working...
              X