11 Replies - 5051 Views - Last Post: 02 November 2011 - 09:42 AM Rate Topic: -----

#1 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Python in MAYA

Posted 01 November 2011 - 12:56 PM

Hello

I am a beginner and I have started teaching myself python (in maya).

However, I am stuck on this piece of code. I am trying to toggle the visibility of the object.

I have written this:

def function_name ("transform_name"):
    if s.visibility.isKeyable() and not s.visibility isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function



It says that there is a SyntaxError..but I'm not sure why??

I am trying to make a program that will allow me to reveal/hide an object (which I have specified in the transform name) at the command line.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Python in MAYA

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python in MAYA

Posted 01 November 2011 - 12:58 PM

You've encapsulated the variable name in quotes on line one. Don't do that.


def function_name (transform_name):
    if s.visibility.isKeyable() and not s.visibility isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function



Was This Post Helpful? 0
  • +
  • -

#3 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Python in MAYA

Posted 01 November 2011 - 01:07 PM

View PostMotoma, on 01 November 2011 - 12:58 PM, said:

You've encapsulated the variable name in quotes on line one. Don't do that.


def function_name (transform_name):
    if s.visibility.isKeyable() and not s.visibility isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function





Ok, I got rid of them..but there is still a syntax error... ??
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python in MAYA

Posted 01 November 2011 - 01:17 PM

On the second line you are missing a period (.) between s.visibility and isLocked():

 if s.visibility.isKeyable() and not s.visibility.isLocked():

Was This Post Helpful? 0
  • +
  • -

#5 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Python in MAYA

Posted 01 November 2011 - 01:22 PM

View PostSimown, on 01 November 2011 - 01:17 PM, said:

On the second line you are missing a period (.) between s.visibility and isLocked():

 if s.visibility.isKeyable() and not s.visibility.isLocked():


thanks..I added that:

def function_name (transform_name):
    if s.visibility.isKeyable() and not s.visibility.isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function



but it says SyntaxError: 'return' outside function #

I removed the indentation and it is executed with no errors...but nothing happens...is my code right??

I mean it doesn't seem to be working, so is there a problem with the way I have written it??
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python in MAYA

Posted 01 November 2011 - 01:33 PM

If you run some Python code just containing that function, it shouldn't, and won't do anything. When you need to use the function, you call it with the parameters:

def function_name (transform_name):
    if s.visibility.isKeyable() and not s.visibility.isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function

#Outside the function
x = function_name(parameter)



Your code doesn't actually do anything right now though, you give it transform_name as a parameter but never use it in the code, it returns value_of_function which hasn't been assigned anything.

You want to think:
What is the function called -> function_name
What do I want to input into the function -> transform_name (parameter)
What do I want the function to return -> value_of_function (return type)

This post has been edited by Simown: 01 November 2011 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Python in MAYA

Posted 02 November 2011 - 04:02 AM

View PostSimown, on 01 November 2011 - 01:33 PM, said:

If you run some Python code just containing that function, it shouldn't, and won't do anything. When you need to use the function, you call it with the parameters:

def function_name (transform_name):
    if s.visibility.isKeyable() and not s.visibility.isLocked():
        s.visibility.set(True)
        s.visibility.lock()
        print s.visibility.type()
    return value_of_function

#Outside the function
x = function_name(parameter)



Your code doesn't actually do anything right now though, you give it transform_name as a parameter but never use it in the code, it returns value_of_function which hasn't been assigned anything.

You want to think:
What is the function called -> function_name
What do I want to input into the function -> transform_name (parameter)
What do I want the function to return -> value_of_function (return type)



ok so I'v changed it slightly:

a=str(raw_input("Enter the name of your variable"))
def visibility(a): 
    if a.visibility(True): 
        cmds.hide(cmds.ls(type='a'))
    else cmds.showHidden(cmds.ls(type='a'):
        print a.visibility.type()


I made a variable: a, where the user inputs the variable and I want to use the variable in the code, to see if the object the user selected is visible or not; if it is visible I want to hide it and if it is hidden I want to reveal it. But again, I get a SyntaxError message.
Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python in MAYA

Posted 02 November 2011 - 06:17 AM

Your problem is on line 5; else takes nothing more. What you are looking for is elif.

This post has been edited by Motoma: 02 November 2011 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Python in MAYA

Posted 02 November 2011 - 08:08 AM

View PostMotoma, on 02 November 2011 - 06:17 AM, said:

Your problem is on line 5; else takes nothing more. What you are looking for is elif.


Again, I changed it but there is still a SyntaxError

a=str(raw_input("Enter the name of your variable"))
def visibility(a):
    if a.visibility(True):
        cmds.hide(cmds.ls(type='a'))
        elif cmds.showHidden(cmds.ls(type='a'):
            print a.visibility.type()



I really have no clue where the error is though??
Was This Post Helpful? 0
  • +
  • -

#10 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python in MAYA

Posted 02 November 2011 - 08:12 AM

elif needs to be indented to the same level as if.
You are missing the final ')' at the end of line 5, just before the ':'.
Was This Post Helpful? 0
  • +
  • -

#11 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Python in MAYA

Posted 02 November 2011 - 08:47 AM

View PostMotoma, on 02 November 2011 - 08:12 AM, said:

elif needs to be indented to the same level as if.
You are missing the final ')' at the end of line 5, just before the ':'.


Oh yea, thanks for that.
Ok so I got it to execute with no errors and I get a window telling me that Python is asking for an input.
I type in my input eg. polyCube but nothing happens...I want it to appear if its hidden or hide if its there..??
Was This Post Helpful? 0
  • +
  • -

#12 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python in MAYA

Posted 02 November 2011 - 09:42 AM

I don't know much of anything about Python programming in Maya, but I really doubt that this code is anywhere near what you need it to do. You would probably do well by joining the Maya-Python mail list.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1