Basic Python Question

202 views
Skip to first unread message

Will Sharkey

unread,
Jan 7, 2015, 6:53:35 PM1/7/15
to python_in...@googlegroups.com
Hi, 

I've been picking up python recently and while doing a mundane task in Maya, I decided to quickly write a script for practice. Then I got really into it and looked up how to make a basic menu!!

I noticed a snag in my script. I'm simply changing the .overrideColor on the shape, but I noticed my script doesnt work it the shapes are not named the same, its not a big deal but I'd love to know the answer.:

import maya.cmds as mc

def redWireColour():
    selection = mc.ls( selection=True )

    for i in range (len (selection)): 
        mc.setAttr( selection[i] + '|curveShape.overrideEnabled', 1 )
        mc.setAttr( selection[i] + '|curveShape.overrideColor', 4 )
    


So the name 'curveShape' is hardcoded, which isnt good. How would one look up the shape name of the object and add it to the '.overrideEnabled'

Thanks in advance

Chris Gardner

unread,
Jan 7, 2015, 7:01:14 PM1/7/15
to python_in...@googlegroups.com
hey will,

here's a function to get the shape of a node (it looks complicated because it's been severely battle tested over the years)

def getShape(node):
    """
    Finds the shape node from a transform

    @param node: Object
    @type node: String

    @return: Shape node
    """
    if 'transform' in cmds.nodeType(node, inherited=True):
        if cmds.listRelatives(node, shapes=True):
            shapes = [x for x in cmds.listRelatives(node, shapes=True, fullPath=True)
                      if cmds.getAttr(x + '.intermediateObject') is False]
            if not shapes:
                return None
            return shapes[0]
        else:
            return None
    elif 'shape' in cmds.nodeType(node, inherited=True):
        return node


so then you could

def redWireColour():
    selection = mc.ls( selection=True )
    for i in selection:
        selShape = getShape(i)
        mc.setAttr( selShape + '.overrideEnabled', 1 )
        mc.setAttr( selShape + '.overrideColor', 4 )



btw, in python you don't need to loop over the selection by an index - you can just loop directly. saves some legwork.

cheers,
chrisg


--
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/3a701563-f870-40b6-b039-058ce61c6fd2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Gardner

unread,
Jan 7, 2015, 7:05:25 PM1/7/15
to python_in...@googlegroups.com
oh, sorry, you might need to replace the "cmds' in my function with "mc" - i use
import maya.cmds as cmds

where you obviously import it as "mc"

cheers,
chrisg

Will Sharkey

unread,
Jan 8, 2015, 9:41:17 AM1/8/15
to python_in...@googlegroups.com
thanks, that works perfectly. I dont quite understand all aspects of the function but I'm sure I will in a month or two. 

Interesting stuff, thanks again.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

AK Eric

unread,
Jan 8, 2015, 2:04:29 PM1/8/15
to python_in...@googlegroups.com
Optionally you can cut out a lot of cruft using PyMel:

def redWireColour():
    sel = pm.ls(selection=True)
    for item in sel:
        item.overrideEnabled.set(1)
        item.overrideColor.set(4)

One of the nice things it does is, if it can't find an attr on the transform, it'll auto look to the shape.

AK Eric

unread,
Jan 8, 2015, 2:14:03 PM1/8/15
to python_in...@googlegroups.com
d'oh, I forgot the :

import pymel.core as pm

part... ;)

Will Sharkey

unread,
Aug 8, 2015, 1:59:02 PM8/8/15
to Python Programming for Autodesk Maya
Hi,  I have some simple code that I'm trying to return the value for a floatSliderGrp but Im not sure why is failing. Any pointers for what Im doing wrong?

import maya.cmds as cmds

def intensity_value(*args): 
    selection = cmds.ls( selection=True )
    intValue = cmds.floatSliderGrp('Intensity', query=True, value=True)
    for sel in selection:
        cmds.setAttr('{0}.intensity'.format(sel),intValue) 

def exposure_value(*args): 
    selection = cmds.ls( selection=True )
    expoValue = cmds.floatSliderGrp('Exposure', query=True, value=True)
    for sel in selection:
        cmds.setAttr('{0}.aiExposure'.format(sel),expoValue)

def myWindow():
    if cmds.window('ws_lightParameter', exists = True):
       cmds.deleteUI('ws_lightParameter')
       
    cmds.window('ws_lightParameter')
    cmds.columnLayout() 
    cmds.floatSliderGrp(label='Intensity', field=True, maxValue=50.0, value=0, cc= intensity_value) 
    cmds.floatSliderGrp(label='Exposure', field=True, maxValue=50.0, value=0, cc= exposure_value) 
    cmds.showWindow('ws_lightParameter')

myWindow()

thanks!

damon shelton

unread,
Aug 8, 2015, 2:04:42 PM8/8/15
to python_in...@googlegroups.com

You need to make your slider group intensity you are just setting the labels. Put intensity without a flag right after the (   when you create the slider grp

--
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/73d27d10-bb91-4285-90cb-329ba74571f8%40googlegroups.com.

Will Sharkey

unread,
Aug 8, 2015, 2:45:08 PM8/8/15
to python_in...@googlegroups.com
got it, thanks for the help.

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/Br1xainAOw8/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAM9RXoJ70Qo7QtY-A_hq%2BW2vbNyxq_%3DvwOw92Gr12eY4rePw0Q%40mail.gmail.com.

Justin Israel

unread,
Aug 8, 2015, 4:51:34 PM8/8/15
to python_in...@googlegroups.com

You also might want to save the actual returned full path name of the float slider group in a variable and use that, instead of a literal string, when you look up the values. Those queries basically ask the entire Maya application for a control named "intensity". So if something else happens to have created a control named that, you may end up querying the wrong one. But the name returned from the control is the unique full name.


Will Sharkey

unread,
Aug 14, 2015, 11:36:25 AM8/14/15
to Python Programming for Autodesk Maya
I was just having another look at a piece of code I wrote for changing certain light parameters. 

When I run the code, Id love to be able to read that current value, then punch it in the respective field. It would also be cool if the UI Window refreshed on light selection, but that might be getting to fancy for my python skills!

Here's the code, appreciate any pointers :)

import maya.cmds as cmds

def intensity_value(colour_control): 
    selection = cmds.ls( selection=True )
    for sel in selection:
        cmds.setAttr('{0}.intensity'.format(sel),colour_control) 

def exposure_value(exposure_control): 
    selection = cmds.ls( selection=True )
    for sel in selection:
        cmds.setAttr('{0}.aiExposure'.format(sel),exposure_control)

def sample_value(sample_control): 
    selection = cmds.ls( selection=True )
    for sel in selection:
        cmds.setAttr('{0}.aiSamples'.format(sel),sample_control)

def myWindow():
    if cmds.window('ws_lightParameter', exists = True):
       cmds.deleteUI('ws_lightParameter')
       
    cmds.window('ws_lightParameter')
    cmds.columnLayout() 
    colour_control = cmds.floatSliderGrp(label='Intensity', field=True, maxValue=50.0, value=1, cc= intensity_value) 
    exposure_control = cmds.floatSliderGrp(label='Exposure', field=True, maxValue=50.0, value=0, cc= exposure_value)
    sample_control = cmds.intSliderGrp(label='Samples', field=True, minValue=1, maxValue=50.0, value=1, cc= sample_value)  
    cmds.showWindow('ws_lightParameter')

myWindow()





Justin Israel

unread,
Aug 14, 2015, 6:16:28 PM8/14/15
to Python Programming for Autodesk Maya

If this were a class, you would save those full path values from creating your controls, as member attributes, so that you could reference them again when you want to use the "edit" mode on the control commands to set a value.
In your case, you would have to use the "global" keyword to save them to global scope variables.

For following selection, that would require either a ScripJob, or directly through the Maya API callback messages. You would also have to handle the case of ambiguous values when you select 2 or more lights, if they have different values.


--
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.

Will Sharkey

unread,
Aug 25, 2015, 10:03:42 AM8/25/15
to python_in...@googlegroups.com
So, I know a couple of phrases in python, but I don't know how to string sentences. I just purchased a couple of tutorial bundles, I'll check back when I have a better structure for learning.

thanks for the help :)

--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/Br1xainAOw8/unsubscribe.
To unsubscribe from this group and all its topics, 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/CAPGFgA3z92a3_Jvig-yrhG-qxmxJ1fE6AJ6__Q-OxyJoabo58w%40mail.gmail.com.

Will Sharkey

unread,
Aug 25, 2015, 11:24:11 AM8/25/15
to Python Programming for Autodesk Maya
i just realised that you are in fact the author of these tutorials!

Well played :D

Justin Israel

unread,
Aug 25, 2015, 3:31:38 PM8/25/15
to Python Programming for Autodesk Maya


On Wed, 26 Aug 2015 3:24 AM Will Sharkey <willjs...@gmail.com> wrote:

i just realised that you are in fact the author of these tutorials!

Well played :D

:-)

Thanks for snagging them.


On Tuesday, August 25, 2015 at 10:03:42 AM UTC-4, Will Sharkey wrote:

So, I know a couple of phrases in python, but I don't know how to string sentences. I just purchased a couple of tutorial bundles, I'll check back when I have a better structure for learning.

thanks for the help :)

--
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/667bc6a4-293c-4d64-8469-cc464a4ee150%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages