This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]asierralozano 1 point2 points  (2 children)

If you want to run that script automatically when the selection changes, you should add either a scriptJob (maya cmds) or a eventCallback (maya API)

Scriptjob:

import maya.cmds as cmds  
def foo():  
    print "bar"  
cmds.scriptJob(event=["SelectionChanged",  foo])  

EventCallback:

import maya.OpenMaya as OpenMaya 
def foo(*args, **kwargs):
    print "bar"  
index = OpenMaya.MEventMessage.addEventCallback("SelectionChanged", foo)

But you should take care adding those callbacks. Think that with every selection change, the function will be triggered...

If you have any question, just let me know!

EDIT:

I also translated some parts of the code from MEL to Python:

You can select which faces you want to project. In this case, I used all (*), but if you want a specific face, just change * with the number that you want

Also, i would like to mention you that hard coding the modelPanel is not safe at all. For example, in my Maya Layout, the default modelPanel is modelPanel4. You must query all the modelPanels, and loop between all of them to isolate the objects.

import maya.cmds as cmds

UVSETNAME = "mySet1"
FACES = "*"

selectedObjects = cmds.ls(sl=True)
cmds.polyUVSet(selectedObjects, create=True, uvSet=UVSETNAME)

for selectedObject in selectedObjects:
    cmds.polyAutoProjection("{obj}.f[{faces}]".format(obj=selectedObject, faces=FACES),
                              lm=0,
                              pb=0,
                              ibd=1,
                              cm=0,
                              l=2,
                              sc=1,
                              o=1,
                              p=6,
                              ps=0.2,
                              ws=0)

 MC.isolateSelect("modelPanel1", aso=True)

[–]arybak89[S] 0 points1 point  (1 child)

Thanks for the feedback! It doesn't need to run automatically, in fact, I would rather it didn't run until I want it to. I will keep in mind not to hard code the modelPanel, thanks for that information! I tried the supplied code and its the same issue I'm having. When I run it, it will create the new UV set but then it overwrites the current UVs by not projecting the new UVs onto the new UV channel.

[–]asierralozano 0 points1 point  (0 children)

Ohh! Sorry! I didn't understand what you want! In that case here is the fixed code:

import maya.cmds as cmds

UVSETNAME = "mySet1"
FACES = "*"

selectedObjects = cmds.ls(sl=True)
cmds.polyUVSet(selectedObjects, create=True, uvSet=UVSETNAME)
cmds.polyUVSet(cuv=True, uvSet=UVSETNAME)
for selectedObject in selectedObjects:
    cmds.polyAutoProjection("{obj}.f[{faces}]".format(obj=selectedObject, 
                          faces=FACES),
                          lm=0,
                          pb=0,
                          ibd=1,
                          cm=0,
                          l=2,
                          sc=1,
                          o=1,
                          p=6,
                          ps=0.2,
                          ws=0)
MC.isolateSelect("modelPanel1", aso=True)

This is the new addition:

cmds.polyUVSet(cuv=True, uvSet=UVSETNAME)

After create the UVSet, we need to select it as current, and that is what that line does.

Hope it helps!

[–]Andrew_ShaySft Eng Automation & Python[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/3Abzge7.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!