Need basic python help :P

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 04 April 2013   #1
Need basic python help :P

Hey all new here and also new to python and mel scripts. I'm trying to make a simple city generator for a uni project, and have most of the UI done, but the main coding behind it all I am struggling with. I know there will be a lot of you who will probably cringe when they see my script so far . I will most likely have a few questions, but the first thing i'm trying to fix is getting the "Number of faces selected" button to work, I had help with this in class and had it working, but lost the file so i'm having to redo it myself . What I want the button to do is update the number of faces selected, but since its inside a definition he had to import another language (I think) to get the text box to update with the correct number selected when the button is clicked. If anyone could help or give me some sample code I would greatly appreciate it, but please keep it simple or explain since I am still a rookie to scripting .

    #Update selected faces
    cmds.separator(h = 15)
    def defaultButtonPush2(*args):
            
        faceNum = cmds.polyEvaluate( faceComponent=True )
            
        print 'Updated face selection.'
    cmds.button( label='Update face selection', command=defaultButtonPush2 )
    
    # query the number of selected faces
    cmds.separator(h = 15)
    cmds.text(label=" Number of faces selected:")
    faceNum = cmds.polyEvaluate( faceComponent=True )
    #cmds.channelBox( 'faceNum' )
    #cmds.text( label= faceNum )
    faceText=cmds.textField(w=250,h=25)
    cmds.textField(faceText, edit=True,text=faceNum)
    print faceNum


Here is what I have done atm, its just mostly UI:

import maya.cmds as cmds

def UI():
    
    #delete existing window if exists
    if cmds.window("CityGen", exists = True):
        cmds.deleteUI("CityGen")
            
    #create window
    window = cmds.window("CityGen", title = "City Generator", w = 430, h = 600, mnb = False, mxb = False, sizeable = True)

    #main layout
    mainLayout = cmds.columnLayout(w = 430, h = 600)
    
    #create banner
    imagePath = cmds.internalVar(upd = True) + "icons/CityGen2.jpg"
    cmds.image(w = 430, h = 100, image = imagePath)
    
    #Convert to ground plane button
    cmds.separator(h = 15)
    def defaultButtonPush1(*args):
        print "Ground plane created."
    cmds.button( label='Convert to ground plane', command=defaultButtonPush1 )
    
    #dropdown menu
    def printNewMenuItem( item ):
            print item
    cmds.separator(h = 15)
    cmds.optionMenu( label=' Face selection:', changeCommand=printNewMenuItem )
    cmds.menuItem( label='All faces' )
    cmds.menuItem( label='Random faces' )
    cmds.menuItem( label='Custom Selection' )
    
    #Update selected faces
    cmds.separator(h = 15)
    def defaultButtonPush2(*args):
            
        faceNum = cmds.polyEvaluate( faceComponent=True )
            
        print 'Updated face selection.'
    cmds.button( label='Update face selection', command=defaultButtonPush2 )
    
    # query the number of selected faces
    cmds.separator(h = 15)
    cmds.text(label=" Number of faces selected:")
    faceNum = cmds.polyEvaluate( faceComponent=True )
    #cmds.channelBox( 'faceNum' )
    #cmds.text( label= faceNum )
    faceText=cmds.textField(w=250,h=25)
    cmds.textField(faceText, edit=True,text=faceNum)
    print faceNum
    
    #slider test
    cmds.separator(h = 15)
    cmds.text(label=" % of faces to select")
    cmds.floatSliderGrp(field=True, en=False, minValue=0.0, maxValue=100.0, fieldMinValue=-0.0, fieldMaxValue=100.0, value=0)
    
    #min/max height values
    cmds.separator(h = 15)
    cmds.text(label=" Minimum building height:")
    minHeight=cmds.textField(w=250,h=25)
    minH = cmds.textField(minHeight, edit=True,text=10)
    cmds.separator(h = 5)
    maxH = cmds.text(label=" Maximum building height:")
    maxHeight=cmds.textField(w=250,h=25)
    cmds.textField(maxHeight, edit=True,text=20)
    
    #Generate City button
    cmds.separator(h = 15)
    def defaultButtonPush3(*args):
        cmds.polyExtrudeFacet( kft = False, ws = False, ltz=10 )
        print 'City Generated.'
    cmds.button( w = 300, h = 50, label='Generate City', command=defaultButtonPush3 )
    
    
    #show window
    cmds.showWindow(window)

UI()
 
Old 04 April 2013   #2
The easiest way to do it is to define the buttonb before the defs.

	b = cmds.button( label='Update face selection')
	def defaultButtonPush2(*args):			
		faceNum = cmds.polyEvaluate( faceComponent=True )
		print 'Updated face selection.'
		cmds.button(b, edit=True, label="You pressed me")

	cmds.button( edit=True, command=defaultButtonPush2 )


If you build UI's you should have a look at pymel. One of the advantages is that you can use a "with" statement and create a nice structure with a better overview, have a look here:
some code.
__________________
www.renderwiki.com - www.openmaya.net
 
Old 04 April 2013   #3
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 11:43 AM.


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