Help with a closure in Maya and Python.

66 views
Skip to first unread message

Padraig Ó Cuínn

unread,
Oct 28, 2016, 1:41:00 AM10/28/16
to Python Programming for Autodesk Maya
Hi everyone. 

I am trying to figure a way out of exiting a closure and executing the nested function within it. 
So far I have only seen on the net closures within the 1 file. let me show you what I mean. 

#File_1

def pressButton():
   
for nodes in file_2.selection():
       
print nodes


pressButton is nested in a function called create_window

#File_2

def selection():
    pymel
.core.selection(n="transforms")

selection is the function I want to call from pressButton() but is imported from another file.

I am looking for most efficient way to escape this closure while still maintaining functionality of the UI.

Thanks in advance.

Marcus Ottosson

unread,
Oct 28, 2016, 2:19:55 AM10/28/16
to python_in...@googlegroups.com
I can't understand the problem. Would it be possible to rephrase the question, and possibly supply an example UI to illustrate what you are experiencing problems with?

An imported module follows the same rules as any object; like a class. So your example doesn't necessarily need to include multiple files, which could simplify things.

Padraig Ó Cuínn

unread,
Oct 28, 2016, 11:46:26 AM10/28/16
to Python Programming for Autodesk Maya
Hi Marcus,

I am trying to get to selection() which is in a maya code file, The UI is built under a create_window() function, in a UI specific file. The code is separated from the UI. 

under create_window() I have another function nested in it for the button to call selection() but it is not reaching due to the enclosure,

File_1
from file_2 import selection as sel


def create_window():
   
     
def pushButton():
         file_2
.sel()

File_2
import pymel.core


def selection():
    pymel
.core.selection(n="transforms")

as you can see there are two files and due to the closure I can't execute the selection function. I know I could easily throw it under the pushbutton inside the UI but i strictly want UI code alone and maya code alone on another file. 

Thanks again

Robert White

unread,
Oct 28, 2016, 11:58:37 AM10/28/16
to Python Programming for Autodesk Maya
Does this work by chance?

# File_1
from file_2 import selection as sel


def create_window():
   
     
def pushButton():

         
sel()


Padraig Ó Cuínn

unread,
Oct 28, 2016, 1:50:46 PM10/28/16
to Python Programming for Autodesk Maya
Nope no difference

Robert White

unread,
Oct 28, 2016, 2:16:46 PM10/28/16
to Python Programming for Autodesk Maya
What error exactly are you getting when you try that?

Padraig O Cuinn

unread,
Oct 28, 2016, 2:18:46 PM10/28/16
to python_in...@googlegroups.com

I don’t get any errors it just doesn’t pass but it does freeze up on me.

 

Padraig O Cuinn

Technical Director / Artist

Justin Israel

unread,
Oct 28, 2016, 3:21:55 PM10/28/16
to python_in...@googlegroups.com


On Sat, 29 Oct 2016, 7:18 AM Padraig O Cuinn <patchquin...@gmail.com> wrote:

I don’t get any errors it just doesn’t pass but it does freeze up on me.

Please provide a small reproduction of the problem via pastebin or gist. After Roberts suggestion to correct a naming issue, I can't see anything else wrong in your small snippets of code. 

 

Padraig O Cuinn

Technical Director / Artist

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/005c01d23147%24b83f7050%2428be50f0%24%40Gmail.com.
For more options, visit https://groups.google.com/d/optout.

Alok Gandhi

unread,
Oct 28, 2016, 11:02:59 PM10/28/16
to python_in...@googlegroups.com
Please provide a small reproduction of the problem via pastebin or gist. After Roberts suggestion to correct a naming issue, I can't see anything else wrong in your small snippets of code. 
I agree, a repro would help. Also, I do not see a call to pushButton() in your create_window(), it is just defined there. Maybe you are calling it somewhere but we do not see that in your example code. Of course, you would have to call it within the scope of create_window().

Another very minorish comment would be your use mixed 'snake case' (create_window()) and 'lower camel case' (pushButton()) in function signatures. It is recommend to use a uniform naming convention. So either use create_window() with push_button() or use createWindow() with pushButton()

Also, a few points about your use of the term closure - 
What you have is simply a nested function. On the other hand, a CLOSURE is a function object that remembers values in enclosing scopes regardless of whether those scopes are still present in memory. You can find more about them here

On Sat, Oct 29, 2016 at 3:21 AM, Justin Israel <justin...@gmail.com> wrote:


On Sat, 29 Oct 2016, 7:18 AM Padraig O Cuinn <patchquin...@gmail.com> wrote:

I don’t get any errors it just doesn’t pass but it does freeze up on me.

Please provide a small reproduction of the problem via pastebin or gist. After Roberts suggestion to correct a naming issue, I can't see anything else wrong in your small snippets of code. 

 

Padraig O Cuinn

Technical Director / Artist

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1h_A1r4qeos06OQpC9Gjr5yPF-CaSexvHEAe5Rcquyzg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages