Reply with a code challenge

Condition[you must post challenge with some initial code you have got]

Q: how would you get the imageplane’s shape node if more than one object matches name: imagePlane1?

lets say I have following procedure to what if I pass the the transform node should give me list of connected shape nodes!!!

proc string[] getShapes( string $xform )
{
   string $shapes[];

   $shapes[0] = $xform;

   if ( "transform" == `nodeType $xform` )
   // If given node is not a transform, assume it is a shape
   // and pass it through
   {
      $shapes = `listRelatives -fullPath -shapes $xform`;
   }

   return $shapes;
}

or in python lc=cmds.listRelatives(cmds.listRelatives(‘frontShape’)[0])[0] ## I know this is not the right way to do…:wink:

ImagePlanes don’t have shapes - they don’t get transform nodes; they’re abstract nodes like sets or shaders.

Because they can’t be grouped, I’m not sure how you’d ever get more than one imagePlane1 anyway… but you could sort them by looking at all the cameras and checking for connections to the imagePlane attribute.

aww man…


    def connectedImgPlanes(self,shapNode):
        lc=""
        try:
            lc=cmds.listRelatives(cmds.listRelatives(shapNode)[0])[0]
        except TypeError,er:
               print "Type Error for %s %s" %(shapNode, er)
               return ""
        except ValueError, er2:
               print "Value Error for %s %s" %(shapNode, er2)
               return ""

        atribVal=cmds.getAttr(lc+".imageName")
        return atribVal

the above code results this


the idea behind is to grab attributes of all image planes at one place if needed to be alter later and avoid creating new node if file is not sourced in properly…



def imagePlane_index():
   vals = []
   imagePlanes = cmds.ls(type='imagePlane')
   for iPlane in imagePlanes:
       vals.append (iPlane, cmds.listConnections(iPlane + ".message", sh=True, type="camera") or [])
   returns dict(vals)  # return it as a dictionary

Returns the imageplanes and the camera(s) they belong to as a dictionary <imagePlane, [List of cameras]>

[QUOTE=Theodox;18618]


def imagePlane_index():
   vals = []
   imagePlanes = cmds.ls(type='imagePlane')
   for iPlane in imagePlanes:
       vals.append (iPlane, cmds.listConnections(iPlane + ".message", sh=True, type="camera") or [])
   returns dict(vals)  # return it as a dictionary

Returns the imageplanes and the camera(s) they belong to as a dictionary <imagePlane, [List of cameras]>[/QUOTE]

I know you didnt tested but some how I got it working!!!

Ah, missing parens in vals.append:


import maya.cmds as cmds
def imagePlane_index():
    vals = []
    imagePlanes = cmds.ls(type='imagePlane')
    for iPlane in imagePlanes:
        vals.append ( (iPlane, cmds.listConnections(iPlane + ".message", sh=True, type="camera") or []) )
    return dict(vals)  # return it as a dictionary

That’s why i get for doing it on my ipad

off-topic: if you have batteries included in a python modules, but then the battery of your ipad dies you would have to get ipad replaced from Apple because you cant change the batteries yourself.

which means all the time you spend in for python working on including batteries in python was not as important as it was important to have a lifetime rechargeable battery in an ipad…

[QUOTE=sanjeev;18623]off-topic: if you have batteries included in a python modules, but then the battery of your ipad dies you would have to get ipad replaced from Apple because you cant change the batteries yourself.

which means all the time you spend in for python working on including batteries in python was not as important as it was important to have a lifetime rechargeable battery in an ipad…[/QUOTE]

Except you can’t work in python on your Ipad, unless you jailbreak

dude just for a fancy looking ipad you preferred it over windows, I know you are elderly person and have more experience in life but If i say you can do more with windows based tab (unless you want to show-off you can do python on ipad) it also means you are giving in less and getting more out of windows…

I am just say I used to have 3G ipod 64GB but out of warranty and its battery stopped working and i had to spend like a replacement from US for $250(maybe more or less) with all data lost… some how I manage to re-sotore some stuff form my synced itunes, nowdays tabs make use of cloud storage (I hope u keep it sync’d)… that day I decided not to buy apple device again…

I have used windows 8CP , looking forward for a surface atleast in dreams…

btw is it really a good idea to convert list to dictionary like this… beside just that you made a list of each item is a tuple,

so Is it that what helped dict(vals) to properly convert to key value pair in dictionary would the results be same if list items are not tuple. like what if they are in straight just individual interger or string value in list? would the typecasting work then ?

dict ( list of tuples) creates a dictionary with the first tuple item as a key, the second item as value (I think it excepts if there are more than 2 items in any tuple.) The values could be lists or tuples themselve; the keys have to be a hashable type, so it could be a tuple but it could not be, say, a list.

I’m not sure off the top of my head if that recipe works for iterables other than tuples.

so I tried on [u’imagePlaneShape8’, u’imagePlane’],

and got this ValueError: dictionary update sequence element #0 has length 16; 2 is required #

once I had such a situation

and I came up with this


def lsttoDic(list)
    objDic={}
    for index,each in enumerate(reversed(selObjects)):
        if index%2==0: value=each
        elif index % 2!=0:
            key=each
            objDic[key]=value
    return objDic

this code will result in {u’imagePlaneShape8’: u’imagePlane’}

my question is “is it a neat solution?” of converting a list to a dictionary…?

later i discovered i dont need enumerate but never changed the code…

the dict() constructor expects an iterator of iterators that all have two objects in them.

[(dictPair1Key, dictPair1Value), (dictPair2Key, dictPair2Value), … ]

When it sees [u’imagePlaneShape8’, u’imagePlane’], it expects the first item in the list to be a 2-length iterator (key/value pair), but instead it gets a string (with 16 characters) and throws an exception.


the_dict = {}
import itertools
keys= itertools.islice(the_list, 0, None, 2)
vals = itertools.islice(the_list, 1, None, 2)
while vals:
   the_dict[next(keys)] =  next(vals) # automatically ignores if there's a key with no val

man u r a great teacher for me…

your experience weighs more but how I did, it didnt even required importing a module , now the question comes to mind is what is robust code ?

itertools is standard python, so there’s no distribution problems. It’s great for common list manipulations (and it’s implemented in C, so it’s usually faster than the identical code written in pure python).

In general, for completely generic problems that come up all over the place I like to use standard library stuff because it’s well tested. There are lots of subtle ways you can mess something up even when it’s simple, and because it’s simple that’s always the last thing you look at when debugging. Saves time for the real problems.

thanks!!!