get item on list inside function

21 views
Skip to first unread message

Adynsky

unread,
Jun 9, 2018, 1:38:06 AM6/9/18
to Python Programming for Autodesk Maya
Hi guys,

I'm new and starting learning python. I'm trying my self to create anim control using the curve and here I'm stuck to get item of function :

def groupParent(groups, prefix, suffix):
    grps = []
    for i in range(len(groups)):
        grps.append(mc.createNode('transform', n="%s%s%s_%s" % (prefix, suffix, groups[i], GROUP)))
        if i > 0:
            mc.parent(grps[i], grps[i - 1])
 
def matchPosition(objectOri, object):
    mc.delete(mc.parentConstraint(objectOri, object, mo=False))
 
def createControl(suffix='ctrl',
                  groups=['zro'],
                  gimbal=None,
                  gimbalSize=0.5,
                  shape=SQUARE,
                  connect=[]):
    for obj in mc.ls(sl=1):
        ctrl    = controller(shape)
        renCtrl = mc.rename(ctrl, prefixName(obj))
        grpPrnt = groupParent(groups, renCtrl, suffix.title())
        mc.parent(renCtrl, grpPrnt)
        finName = mc.rename(renCtrl, '%s_%s' % (prefixName(obj), suffix))

        doMatch = matchPosition(obj, grpPrnt)
 
I run it and have the error "Target list was empty or contained no valid targets." So, how do I do to get item the list on grps[0] in function groupParent and then assigned to grpPrnt variable?! Thanks in advance!

 
 

Justin Israel

unread,
Jun 9, 2018, 1:51:07 AM6/9/18
to python_in...@googlegroups.com
Hi. You are missing a return statement in your groupParent function. So you end up having grpPrnt=None

def groupParent(groups, prefix, suffix):
    grps = []
    for i in range(len(groups)):
        grps.append(mc.createNode('transform', n="%s%s%s_%s" % (prefix, suffix, groups[i], GROUP)))
        if i > 0:
            mc.parent(grps[i], grps[i - 1
])
    return grps

Also, in the createControl function, be careful when passing default list values for the function arguments. The groups param is currently being used as read-only and the connect isn't being used. But if you ended up using them to append, you might find surprising results when the default list keeps growing on every call. Its better to use a lit:

def foo(myList=None):
    if myList is None:
        myList = ['default']

 
 

--
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/4129465e-02e4-4ef6-94fe-8abf14c225c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adien Dendra

unread,
Jun 10, 2018, 11:31:04 PM6/10/18
to python_in...@googlegroups.com
Hi Justin,

Thanks for the solved! It works now. Regarding the function I didn't get about createControl function that you mentioned. Can you explaining means about more growing on every call?! That param will use for the connection of the constraint also parent later using dictionary as it is :

      if connect:
            def parentObject(ctrl, obj):
             mc.parent(obj, ctrl)
            dic = {'parentCons': mc.parentConstraint,
                   'pointCons': mc.pointConstraint,
                   'orientCons': mc.orientConstraint,
                   'scaleCons': mc.scaleConstraint,
                   'parent': parentObject}
            for con in connect:
               dic[con](parCtrl, obj)

 
I'm still thinking to make them as function.. :D Thank you!

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

--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1k5CRuC%2B4g9Y2uf3kfsNjqrVZVbgCYBL9ZNu30sos3UQ%40mail.gmail.com.

Justin Israel

unread,
Jun 11, 2018, 1:30:31 AM6/11/18
to python_in...@googlegroups.com
On Mon, Jun 11, 2018 at 3:31 PM Adien Dendra <adien....@gmail.com> wrote:
Hi Justin,

Thanks for the solved! It works now. Regarding the function I didn't get about createControl function that you mentioned. Can you explaining means about more growing on every call?!

def foo(aList=[]):
    aList.append(1)
    return aList
foo()
# [1]
foo()
# [1, 1]
foo()
# [1, 1, 1]

This happens when you use a mutable default value like a list or dictionary instance, and you write to that default value. Python only keeps a since instance of that default for the entire function definition. It is just a common python gotcha to keep in mind. You wouldn't see this problem if you only ever read from the variable and not add to it.

 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
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.
--
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/CAE5XL93dmFDEcSCDvm%2Brr_XtnSEn_B6ZVc9Z73cyAET5-C6M_A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages