CGTalk > Development and Hardware > Graphics Programming
To minimize the ads you see on this page create a CGTalk account and log in HERE
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 09-15-2012, 07:19 AM   #1
DEVILSAN
Earth Born>> goo.gl/mJh20
 
DEVILSAN's Avatar
portfolio
Sanjeev Kumar
MPC
India
 
Join Date: Jan 2005
Posts: 290
Question error on loading function external to class from function inside class

have a look at the code,
Code:
import maya.cmds as cmds dic={} print("dictionary is empty",dic) def main(): inst=btnUI() inst.create() class btnUI(object): def __init__(self, winName="winTheWindow"): self.winTitle = "The Window" self.winName = winName def create(self): if cmds.window(self.winName, exists=True): cmds.deleteUI(self.winName) dic['item1']='item1value' dic['item2']='item2value' cmds.window(self.winName, title=self.winTitle) self.mainCol = cmds.columnLayout( adjustableColumn=True ) self.btnA = cmds.button( label='Press Me - External Func',c='outsideFunc(dic)' ) self.btnb = cmds.button( label='Populate more items',c='populateDic()' ) cmds.showWindow( self.winName ) cmds.window(self.winName, edit=True, widthHeight=[250,75]) def populateDic(): dic['item3']='item3value' dic['item4']='item4value' dic['item5']='item5value' def outsideFunc(dicItems): print("Outside function, called from inside a class function") print(dicItems) if __name__ == '__main__': main()

all i am trying is to add more items to dictionary and populate with second button if necessary!!! everything goes fine, if i have this code in script editor and I run it clicking on the blue play button but if i import this script
import funtest
funtest.main()

the window loads up but i get error on clicking the button... So please tell me why is their a difference between executing the code from script editor and the other one by importing..

it shouild be same or if it is what is wrong with my logic ?

Last edited by DEVILSAN : 09-15-2012 at 08:27 AM.
 
Old 09-17-2012, 02:47 AM   #2
ThE_JacO
MOBerator-X
 
ThE_JacO's Avatar
CGSociety Member
portfolio
Raffaele Fragapane
That Creature Dude
Animal Logic
Sydney, Australia
 
Join Date: Jul 2002
Posts: 10,950
When you run it inside the script, that function is registered to the running main, and can be called by its name, because it will be topmost and has no namespace.

When you import the module, that function called the way you do isn't registered anywhere, so it can't be found, because its namespace would become:
importedModuleName.whateverFunction()

It's rather basic Python internals you really want to familiarise yourself with if you want to approach OOP in Python.

And any reason to not make it a method of that class anyway?
At the very least you will need to register it as a maya command if you don't want to embed it in the class as a method, so it shows up and can be called.
__________________
"As an online CG discussion grows longer, the probability of the topic being shifted to subsidies approaches 1"

Free Maya Nodes
 
Old 09-17-2012, 03:15 AM   #3
DEVILSAN
Earth Born>> goo.gl/mJh20
 
DEVILSAN's Avatar
portfolio
Sanjeev Kumar
MPC
India
 
Join Date: Jan 2005
Posts: 290
Quote:
Originally Posted by ThE_JacO
And any reason to not make it a method of that class anyway?
At the very least you will need to register it as a maya command if you don't want to embed it in the class as a method, so it shows up and can be called.
yes I also want the script to run in non-gui mode by passing command line however I haven't implemented that yet..
 
Old 09-17-2012, 03:18 AM   #4
DEVILSAN
Earth Born>> goo.gl/mJh20
 
DEVILSAN's Avatar
portfolio
Sanjeev Kumar
MPC
India
 
Join Date: Jan 2005
Posts: 290
Quote:
Originally Posted by ThE_JacO
When you run it inside the script, that function is registered to the running main, and can be called by its name, because it will be topmost and has no namespace.

When you import the module, that function called the way you do isn't registered anywhere, so it can't be found, because its namespace would become:
importedModuleName.whateverFunction()

So you mean , I would have to put the code that creates GUI outside the main block ? is it this how all python scripts for Maya are done ?
 
Old 09-17-2012, 12:02 PM   #5
ThE_JacO
MOBerator-X
 
ThE_JacO's Avatar
CGSociety Member
portfolio
Raffaele Fragapane
That Creature Dude
Animal Logic
Sydney, Australia
 
Join Date: Jul 2002
Posts: 10,950
What I mean is that from inside your class, you are invoking a command.
When you run with your script as __main__ and that also contains the class, it will look for the command you told it to run in there as well, find it, because it's in an available namespace, and run it.

However, when you import as a module, that command is not in the __main__ anymore, and will not be found.

If you want to run commands both from a GUI and from a batch process, or if anything is generic enough that it warrants existing decoupled from the guy, you should implement it, and then register it, as a command on its own, so that it can comfortably be called from anywhere.

Relying on a specific scope, especially one as usually unavailable as the script editor, is going to be pain through and through.
__________________
"As an online CG discussion grows longer, the probability of the topic being shifted to subsidies approaches 1"

Free Maya Nodes
 
Old 09-17-2012, 12:02 PM   #6
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:36 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.