def noob(): function not defined

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 05 May 2013   #1
def noob(): function not defined

I can't seem to figure out why my button command won't call my function...
# Error: NameError: name 'printsomething' is not defined #



# testScript for maya python
import maya.cmds as cmds

# simple def
def printsomething(strToPrint):
    print strToPrint

if cmds.window('MainWindow', ex=True):
   cmds.deleteUI('MainWindow', window=True)

# create UI
jgMainWin = cmds.window('MainWindow', title ="Main Winow", wh=(200, 200))
jgMainCol = cmds.columnLayout(p=jgMainWin, w=200)
jgMainFrame = cmds.frameLayout(p=jgMainCol, label="testFrame", w=200)
jgMainBtn01 = cmds.button( p=jgMainFrame, label="testBtn", c='printsomething("Testing")')

# show the main window
cmds.showWindow(jgMainWin);

#this works...
printsomething("Test")


can anyone show me what I'm doing wrong?
__________________

gibby3d.com
 
Old 05 May 2013   #2
It looks like it works when I enter the code inside maya, but when I import/reload from a file it won't work, I'm guessing I have to make a def or module for the filename?

EDIT: Ok, so I found some helpful info here: http://forums.cgsociety.org/showthread.php?t=938576

so if my file is called jgTestPython.py..

# inside maya script editor
import jgTestPython as j
#or
reload(j)


#this works
j.printsomething("Testing")

but my button still throws the error.
__________________

gibby3d.com

Last edited by jgibz : 05 May 2013 at 06:57 PM.
 
Old 05 May 2013   #3
In general, having code that executes directly on import is a bad idea and it may be contributing to some of your confusion. Still, if you want to get the button working (it's a good learning exercise), you will need to import the printsomething function directly into maya's main namespace before running the other code:
from jgTestPython import printsomething

I would highly recommend wrapping your UI code in its own function to keep things clean:
# testScript for maya python
import maya.cmds as cmds

# simple def
def printsomething(strToPrint):
    print strToPrint

def show_ui():
    if cmds.window('MainWindow', ex=True):
       cmds.deleteUI('MainWindow', window=True)

    # create UI
    jgMainWin = cmds.window('MainWindow', title ="Main Winow", wh=(200, 200))
    jgMainCol = cmds.columnLayout(p=jgMainWin, w=200)
    jgMainFrame = cmds.frameLayout(p=jgMainCol, label="testFrame", w=200)
    jgMainBtn01 = cmds.button( p=jgMainFrame, label="testBtn", c='from jgTestPython import printsomething\nprintsomething("Testing")')

    # show the main window
    cmds.showWindow(jgMainWin);

Using this approach you can do a straight import and then call the show_ui function:
import jgTestPython
jgTestPython.show_ui()

I've modified the button command to import the printsomething function prior to calling the function. This will ensure that the function is available regardless of the namespace that the code is operating in.

There are lots of different ways of setting up UI code and calling commands. This is just one approach based on your example code.

-ChrisZ
 
Old 05 May 2013   #4
Thanks for the explanation, greatly appreciated
__________________

gibby3d.com
 
Old 05 May 2013   #5
Originally Posted by zeroeffect: There are lots of different ways of setting up UI code and calling commands. This is just one approach based on your example code.


Most of my UI in mel was pretty simple, so I never stored UI elements in variables, and honestly I'm still unclear as to why this is done. Is this a common approach for python?
__________________

gibby3d.com
 
Old 05 May 2013   #6
And what if I have no UI, is it common to use scripts like this:

import jgLimitInf as j
j.jgLimitInf(*args)


for example I've written my first script below without UI, perhaps you guys can tell me if my approach is correct?


# jgLimitInf.py
# check the number of influences on a skinned mesh above a certain number
# written to avoid error in MayaCE3 export with influences > 4.
# jgLimitInf("your mesh name","number of influences vertices should have")

import maya.cmds as cmds
import maya.mel as mel

def jgLimitInf(skinnedMesh, limitNum):

    #var to hold problem verts
    problemVerts = []

    #get verts in mesh
    cmds.select(clear=True)
    numVerts = cmds.polyEvaluate(skinnedMesh, v=True)
    cmds.select(skinnedMesh+'.vtx[0:'+str(numVerts)+']', add=True)
    skinnedVerts = cmds.ls(sl=True, fl=True)

    #find skin cluster
    meshCluster = mel.eval('findRelatedSkinCluster '+skinnedMesh)

    #find problem verts and select them
    for vert in skinnedVerts:
        influences = cmds.skinPercent(meshCluster, vert, query=True, transform=None)
        numInfluences = len(influences)

        if numInfluences > limitNum:
           print vert, " has "+str(numInfluences)+ " influences!"
           problemVerts.append(vert)

    if len(problemVerts) > 0:
       cmds.select(problemVerts);
       print " _______________ problem verts found:",len(problemVerts),"______________"
    else: 
         #if no verts found
         print "No vertices have more than "+str(limitNum)+" influences!"
__________________

gibby3d.com

Last edited by jgibz : 05 May 2013 at 01:31 AM.
 
Old 05 May 2013   #7
Originally Posted by jgibz: Most of my UI in mel was pretty simple, so I never stored UI elements in variables, and honestly I'm still unclear as to why this is done. Is this a common approach for python?
One of the reasons for storing a UI element in a variable (in MEL this value is the full path name of the element) is so that you can access the element at a later time. For example, if you create a renaming dialog with a text input field and an OK button, and pressing the button calls a function to rename the object, you would need to query the text field in the renaming function. By storing the text input field in a variable you have access to it in other functions/methods.

Originally Posted by jgibz: And what if I have no UI, is it common to use scripts like this
Importing the module and then calling functions inside of the module is common practice
 
Old 05 May 2013   #8
Awesome, thanks!
__________________

gibby3d.com
 
Old 05 May 2013   #9
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



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 08:56 AM.


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