Home Website Youtube GitHub

Basic questions about custom steps and python

Hey,

I’ve managed to build a quite complicated rig for a school assignment without any pre or post custom steps. Being unable to rebuild the rig after creating all the custom attributes and connections left me feeling that I’m not using mGear to its fullest. For the next rigs, I’d like to create all these custom attributes and connections with custom steps. I think I found the right place in the mGear documentation but I’m unable to make it work. Miquel’s tutorial series stops on steps quite a lot but covers only importing mesh, skins and creating gimmick joints.

So my main question would be where to start learning about it? I’m specifically interested in creating attributes, connecting them and setting the ranges of input/output connections.
Another question for future reference. I’d like to create 2D cutout character with switchable textures. Is it possible to automate this sort of rigging with custom steps?: https://www.youtube.com/watch?v=C5wu0P4tjyk Is it even a right approach for this sort of thing?

For example, what am I doing wrong in this script?:

import mgear.shifter.custom_step as cstp
import mgear.maya.attribute as att

    class CustomShifterStep(cstp.customShifterMainStep):
      def __init__(self):
        self.name = "addAttributes"

      def run(self, stepDict):
        att.addEnumAttribute(self.local_C0_ctl, "Name", ["Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter"] )

Thank you and sorry if it’s too basic what I’m asking for!

PS Here’s the clip I did for my school assignment:

2 Likes

Hi Sander,

It looks like self.local_C0_ctl might be your problem.

“self” is a Python convention that refers to the class. So in this case, it is referring to CustomShifterStep. This just lets you refer to methods (functions that are inside classes) and properties (attributes inside the class) by using stuff like this:

self.run

So if you add a method inside your class, you can run it from any other place inside that class.

class TestClass:
    def some_method(self):
        self.myFavouriteAttribute = 104

    def add_tangent_control(self):
        DO STUFF HERE

    def another_method(self):
        self.add_tangent_control
        # self.myFavouriteAttribute is available in all the methods of this class, TestClass.
        print(self.myFavouriteAttribute)

To learn more about classes and stuff, you’ll want to learn about object oriented programming in Python.

To access the actual rig control objects in your Maya scene, there are at least two ways.

  1. The most direct way is to just call the object using PyMEL.

By the way, one other Python convention is to not “alias” or nickname your imports. So I recommend importing it as attribute, not as att. This will help you stay organized when you have 25 different post scripts and are trying to search your functions. “Wait… did I call it att? attr? A bit of both?”

Also… are you using an older version of mGear? In mGear 3, it should be mgear.core.attribute, not mgear.maya.attribute. I forget what it used to be in mGear 2.6. Wow, now this reply is getting long…

import mgear.core.attribute as attribute
localControl = pm.PyNode('local_C0_ctl')
attribute.addEnumAttribute(localControl, "Name", 0, ["Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter"] )
  1. A 2nd way is to use the stepDict which is a dictionary (I think it is a dictionary) of the objects in the rig that is sort of built-in to the CustomShifterStep class. I don’t really know. I haven’t read the code for this. And I don’t know where any documentation to find the list of items inside is. All I know is you can call some objects like this. For example, to find the top group of the rig, you can use this:

oModel = stepDict['mgearRun'].model

As for sneakily slipping in a 2nd question about how to do 2D cutout characters, I recommend starting another separate thread in Rigging General.

2 Likes

AND, by the way, if you want to keep things simple as you are learning, if you are just writing basic PyMEL scripts, you can skip the boilerplate CustomShifterStep(cstp.customShifterMainStep) stuff. You only really need that class if you intend to use the stepDict and other properties of that class.

But for basic PyMEL, you can just run an empty text file. And then it will be less confusing to remember the distinctions between Python vs. mGear vs. Maya PyMEL commands. For example, this is a perfectly valid post script. This will run just fine as a complete script:

my_basic_post_script.py:

import pymel.core as pm
import mgear.core.attribute as attribute
localControl = pm.PyNode('local_C0_ctl')
attribute.addEnumAttribute(localControl, "Name", 0, ["Alejandro", "Ed", "Kathryn", "Presila", "Sean", "Peter"] )
3 Likes

Chris!

Thank you so much for the long, insightful answer and clearing stuff out for me (and hopefully other beginners out there). This will get me rolling for sure! I hope I’ll find my way to the proper object oriented way of writing Python in the future. At the moment I’ll be happy to just see it work :slight_smile: !

Yes, the 2D part kind of slipped in there. You’re probably right that it deserves a separate thread.