-2

I'm new to python language and I'm trying to understand the self concept.

I have defined the following two methods, and I am wondering what changes will have to be made in the code to include them in a class and where self would be used.

import maya.cmds as cmds
# This function returns the animCurves connected to particular node 

def animCurveNode():

    animCurveName = []
    mySel = cmds.ls(sl = True)
    filePath = "D:\sdk.txt"
    attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
    for each in mySel:
        list = cmds.listConnections(each, d = False,s = True)
        animCurveName.extend(list)

    return animCurveName

# This function return the animCurves and the name of the atribute to connect as the first memebr in the list

def attrAnimCurve():
    attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
    animCurveNames = animCurveNode()
    animCurveNames.insert(0,attrName)
    return animCurveNames
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Why do you want to use a class here? It's quite important to know what to use the `self` for. – geckon May 05 '15 at 14:30

3 Answers3

0

self is conventionally the first argument of class methods (functions bound to a class instance). self is used as a reference to the object instance in question, and is thus used to read and write that instance's attributes.

This explicit passing of the object reference also allows for setting methods outside of the class definition:

class Foo:
  def __init__(self, data):
    self.data = data
  def get(self): return self.data

foo = Foo([1,2,3])
Foo.get_data_as_set = lambda self: set(self.data)
print foo.get()
print Foo.get_data_as_set(foo)

The above prints

[1, 2, 3]
set([1, 2, 3])
EvenLisle
  • 4,672
  • 3
  • 24
  • 47
0

What you have are not methods. They're not in any class. See the simplest example below:

import maya.cmds as cmds


class MyClass:

    # This method returns the animCurves connected to particular node
    def animCurveNode(self):

        animCurveName = []
        mySel = cmds.ls(sl = True)
        filePath = "D:\sdk.txt"
        attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
        for each in mySel:
            list = cmds.listConnections(each, d = False,s = True)
            animCurveName.extend(list)

        return animCurveName

    # This method return the animCurves and the name of the atribute to connect as the first memebr in the list
    def attrAnimCurve(self):
        attrName = "l_eyeBrow_Sliders.eyeBrow_start_updn"
        animCurveNames = animCurveNode()
        animCurveNames.insert(0,attrName)
        return animCurveNames

However you didn't really specify what you want to use self for. For example, what variables should be a part of the objects.

For more reading about self take a look at this:

What is the purpose of self?

Community
  • 1
  • 1
geckon
  • 8,316
  • 4
  • 35
  • 59
  • There's no point in making those functions part of a class if they are not sharing any data between them. – Daniel Roseman May 05 '15 at 14:25
  • This is what I tried to express in my answer. The OP should specify why he wants to use a class in the first place. – geckon May 05 '15 at 14:26
0

Thanks for the reply ... Im confused with the concept of what variables should be a part of the objects. for example in my code

class MyClass:

    # This method returns the animCurves connected to particular node
    def animCurveNode(self):

        animCurveName = []
        mySel = cmds.ls(sl = True)

or should it be

class MyClass:

    # This method returns the animCurves connected to particular node
    def animCurveNode(self):

        self.animCurveName = []
        self.mySel = cmds.ls(sl = True)