Re: [Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-21 Thread Marcus Ottosson
Maybe an an iterative loop could work?

def find_special(start):
parent = start
while parent:
if cmds.nodeType(parent[0]) == "stkLevel":
return parent[0]

parent = cmds.listRelatives(parent, parent=True)

return None # not found

*Example*

cmds.file(new=True, force=True)
# Generate hierarchy
cmds.spaceLocator(name="selectMe")for group in range(5):
cmds.group()

cmds.group(name="special")
cmds.group()
# Select node
cmds.select("selectMe")
# Find "special"def find_special(start):
parent = start
while parent:
if parent[0] == "special":
return parent[0]

parent = cmds.listRelatives(parent, parent=True)

return None # not found

start = cmds.ls(selection=True)
special = find_special(start)
print(special)

​

On 21 September 2016 at 18:42, yann19  wrote:

> So I did a new code (after some more thinking...) Do feel free to
> criticize and give pointers..
> I tested a few scenarios and it seems to work, at least I think it did..
>
> However now I am bumping into another problem.
> Say, I have 2 same hierarchy of Root as seen in the attachment but the
> root nodes of these 2 are called 'Root1' and 'Root2' respectively.
>
> If I select |Root1|locator1|pCube1 and |Root2|locator2|pCube2, it will
> returns me "|Root1" as the result. But if I inverse my selection, "|Root2"
> will be my output result.
> What is the best way to get around it? Is putting in more checking or
> if-else statements a viable solution?
>
> Sorry for the spam of questions and codes..
>
> def get_selection():
>sel = cmds.ls(selection=True, long=True)
>stkLevel_node = cmds.ls(sel, type="stkLevel")
>if stkLevel_node:
>if len(stkLevel_node) > 1:
>cmds.warning("Please select 1 stkLevel node only")
>else:
>return stkLevel_node
>else:
> root_node = []
> for sel_path in sel:
> split_sel_path = sel_path.split('|')[1]
> if split_sel_path not in root_node:
> root_stack.append(split_sel_path)
> return cmds.ls(root_node, long=True)[0]
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/15fb02f2-7cbf-4b2c-a3ef-
> 58ccb8ebc86f%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Marcus Ottosson*
konstrukt...@gmail.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODx3G1G-aC3rtsLiEuNjr%2BOL0fR7u0adnef0EG7BNp-_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-21 Thread Justin Israel
return cmds.ls(root_node, long=True)[0]

You return the first item of the list that you build up. If you want all of
them, why don't you just return all of them?

On Thu, 22 Sep 2016, 5:43 AM yann19  wrote:

> So I did a new code (after some more thinking...) Do feel free to
> criticize and give pointers..
> I tested a few scenarios and it seems to work, at least I think it did..
>
> However now I am bumping into another problem.
> Say, I have 2 same hierarchy of Root as seen in the attachment but the
> root nodes of these 2 are called 'Root1' and 'Root2' respectively.
>
> If I select |Root1|locator1|pCube1 and |Root2|locator2|pCube2, it will
> returns me "|Root1" as the result. But if I inverse my selection, "|Root2"
> will be my output result.
> What is the best way to get around it? Is putting in more checking or
> if-else statements a viable solution?
>
> Sorry for the spam of questions and codes..
>
> def get_selection():
>sel = cmds.ls(selection=True, long=True)
>stkLevel_node = cmds.ls(sel, type="stkLevel")
>if stkLevel_node:
>if len(stkLevel_node) > 1:
>cmds.warning("Please select 1 stkLevel node only")
>else:
>return stkLevel_node
>else:
> root_node = []
> for sel_path in sel:
> split_sel_path = sel_path.split('|')[1]
> if split_sel_path not in root_node:
> root_stack.append(split_sel_path)
> return cmds.ls(root_node, long=True)[0]
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/15fb02f2-7cbf-4b2c-a3ef-58ccb8ebc86f%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA11Oy%2BA92sXmYsm1X-qP_Sr%2B6RK%3DuqzdjNZg%3D5cQhYjdw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-21 Thread yann19
So I did a new code (after some more thinking...) Do feel free to criticize 
and give pointers..
I tested a few scenarios and it seems to work, at least I think it did..

However now I am bumping into another problem.
Say, I have 2 same hierarchy of Root as seen in the attachment but the root 
nodes of these 2 are called 'Root1' and 'Root2' respectively.

If I select |Root1|locator1|pCube1 and |Root2|locator2|pCube2, it will 
returns me "|Root1" as the result. But if I inverse my selection, "|Root2" 
will be my output result.
What is the best way to get around it? Is putting in more checking or 
if-else statements a viable solution?

Sorry for the spam of questions and codes..

def get_selection():
   sel = cmds.ls(selection=True, long=True)
   stkLevel_node = cmds.ls(sel, type="stkLevel")
   if stkLevel_node:
   if len(stkLevel_node) > 1:
   cmds.warning("Please select 1 stkLevel node only")
   else:
   return stkLevel_node
   else:
root_node = []
for sel_path in sel:
split_sel_path = sel_path.split('|')[1]
if split_sel_path not in root_node:
root_stack.append(split_sel_path)
return cmds.ls(root_node, long=True)[0]

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/15fb02f2-7cbf-4b2c-a3ef-58ccb8ebc86f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-21 Thread yann19
@Justin
I was calling cmds.ls(sel, type='stkLevel') as a check to see if it is a 
stkLevel nodetype. If it is true and depending on how many items are being 
selected, it will check and iterate accordingly.
I believe my algorithm is not written properly here..  I am expecting 
stkLevel_node[0] to be returned as the fullpath of the Root node if the 
selection is 1 and if it is of stkLevel nodetype.

@Remi
I am planning to use cmds instead of pymel, rather than introducing a new 
module. WIthin my code, I have been using cmds, and hence I thought of 
standardizing it but I shall give a go to see if I can convert into cmds 
terms if possible

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/fa1e5a71-c17a-4fd0-9903-a2daf084725f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-20 Thread Justin Israel
> I got the issue, where if I select only 1 pCube#, and it returns nothing,
> though it seems to be working if I only select 1 Root Node
> While that is one of the issue, can someone kindly guide me if I am
> writing my code correctly?
>

I could see why this might happen. The first thing you do is grab the
current selection ("sel"). So if you have a 'pCube#' selection, then that
is the item in the list. Then you call cmds.ls(sel, type='stkLevel') on
that list. Are you expecting that to somehow resolve the root node for you?
Because after you check if its the only item in the list, you hit the
"else" branch and return stkLevel_node[0]. What does that actually return?
What is in that list?


> I asked this because first I am still not that great of a scripter and I
> feel that I am forcing my way through while following those conditions
>

Well I do see that you are not returning from your function after you print
warnings. Like, if there is nothing selection, you still run the rest of
your function.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0mdcp7LwoVHHVMUTWevw1%2BQCA3M5J%3DFue0a-KxRRoDXQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[Maya-Python] Issues with getting the root node of the hierarchy based on selection

2016-09-20 Thread yann19
Hi all, I need some help with this function of mine.
Suppose I have the following hierarchy as seen in the attachment.
Assuming that everything in that attachment is of custom nodes (I used 
locators, groups etc. for easy visual, i hope)
and I am trying to grab the Root Node in which its type is 'stkLevel'.

I am trying to fulfill a few conditions:

   1. User can select either the Root Node or the pCube#
   2. If User selects nothing in the scene, it will prompts a warning
   3. If User selects more than 2 Root Node, it will prompts a warning
   4. If User selects 1 or more 'pCube#', it will return the Root Node 
   within the hierarchy
   5. Ultimately, this function must return the full path of the Root Node

def get_stkLevel():
sel = cmds.ls(selection=True, l=True)
if len(sel) == 0:
cmds.warning("Please select something")


stkLevel_node = cmds.ls(sel, type = "stkLevel")
if len(sel) > 1:
if stkLevel_node:
cmds.warning("Please select a valid stkLevel node")
else:
root_list = []
for item in sel:
root_node = item.split('|')[1]
if root_node not in root_list:
root_list.append(root_node)
return cmds.ls(root_list, l=True)[0]
else:
return stkLevel_node[0]


I got the issue, where if I select only 1 pCube#, and it returns nothing, 
though it seems to be working if I only select 1 Root Node
While that is one of the issue, can someone kindly guide me if I am writing 
my code correctly?
I asked this because first I am still not that great of a scripter and I 
feel that I am forcing my way through while following those conditions



-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/ecb59d2c-9ab4-4141-99c0-41482ae77027%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.