0

I'm not understanding any online documentation about how to make inheritance work.

I have this here:

import maya.cmds as cmds


class RigLegs():
    def __init__(self, *args):
        self.rigLegs()

    def rigLegs(self):
        self.items["side"] = "left"
        self.lIK = cmds.duplicate(self.lJoints["hip"], n = self.getName("hip_IK"))
        for i in self.lIK:
            newName = i[0].replace("_JNT", "_IK")
            cmds.rename(i, newName)

But it's complaining that there is no self.items - I have to inherit it from another class which is far too large to post here. Can anyone help me figure out how to do that? I've researched online and nothing makes any sense.

The other class is in a different file altogether.

1
  • class RigLegs(ParentClass)? Commented Apr 8, 2013 at 23:10

2 Answers 2

3

To inherit from another class do:

class RigLegs(base_class_name):

An Example:

class base_class():
    items = [1,2,3]

class pie(base_class):
    def __init__(self):
        print (self.items)

instance = pie()

You can read more in the Python Documentation


With imports:

file (apples.py)

class base():
    items = [1,3,4]

file (main_file.py)

import apples

class pie(apples.base):
    def __init__(self):
        self.pies()

    def pies(self):
        print(self.items)

instance =  pie()
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work. They're in different files, so I import it, and inherit the class by saying class RigLegs(UI_file_name.UI_class_name) it still claims that RigLegs has no attribute "items"
Are you sure you're importing it properly? It works perfectly fine for me if they are split into different files.
yes I know how to import it properly :( I'm using class RigLegs(jtAutoRigUI.RigUI): after import jtAutoRigUI
Post the appropriate area of code for the RigUI class into the question and i'll see if I can help. Right now though it looks as if there isn't actually an items variable in RigUI
It is far too large to paste into there, so I used a pastebin : pastebin.com/m3XctSMR
0

In the class declaration: class RigLegs(), you should include all the classes you want to inherit from, such as in:

class RigLegs(Ancestor1, Ancestor2):
    # Your code chere.

Don't forget that you still need to call the initialization logic of your ancestors.

    def __init__(self):
        super(RigLegs, self).__init__()

8 Comments

As the other suggestion says, it doesn't work. It still claims that there is no attribute "items".
@user1090427 Then this probably is because your ancestors do not define items, don't you think?
@user1090427 That's because you didn't call the ancestor's __init__ method; which is where items was defined ;). See my edit.
I've not come across having to use super before, I don't quite get what it's doing. But it also isn't working because I get the error: super() argument 1 must be type, not classobj
@user1090427 Your RigUI class needs to inherit from object: class RigUI(object). Otherwise, replace the super call with RigLegs.__init__(self).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.