Home Technical Talk

Scripting beginner :)

polycounter
Offline / Send Message
Phoenix995 polycounter
Hey guys

i wanted to start learning scripting, and that's why i want to write a small script helping me UV unwrap

what i want to create is a tool that allows me to select 2 vertices in my uv editor and align them on the U or V axis by rotating the entire cluster.

could you guys help me or give me any advice how to start with something like that?
thank you guys in advance!

script.png

Replies

  • Deadly Nightshade
    Options
    Offline / Send Message
    Deadly Nightshade polycounter lvl 10
    Nightshade UV Editor 2.0.1 has this feature. Select an edge or two UV's and run "Orient Edge". See my signature for the download link.

    If you still want to code this yourself then you need to calculate the arc tangent in degrees. You will get a result ranging between -90 and +90 degrees - which is the value that you need to orient the UV shell. Use a switch-statement (MEL) or a big if/elif/else -statement (Python). Also, before orienting you wanna calculate the centerpoint of the bounding box of the selection. This can be done with polyEvaluate() using the boundingBoxComponent2d -flag.

    Also if you wanna do this on multiple shells then you need to create a function which calculates all UV shells in the selection. This can be done by the function getUvShellsIds() on a mesh (Python only).
  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    Have you done any scripting before? That might be a bit too difficult for a first script.
    But basically just store the X/Y of each vertex, treat them as a right triangle to get their X/Y distances and hypotenuse, then get the rotation angle in degrees and rotate the entire shell based on that.
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    thank you very much for the answers!
    no i never scripted before
    are there any tutorial out there, where i could learn what i need to konw to write such a script?
  • Deadly Nightshade
    Options
    Offline / Send Message
    Deadly Nightshade polycounter lvl 10
    I highly recommend this book if you are just starting out:
    [ame="http://www.amazon.com/Maya-Python-Games-Film-Reference/dp/0123785782"]Maya Python for Games and Film: A Complete Reference for Maya Python and the Maya Python API: Adam Mechtley, Ryan Trowbridge: 9780123785787: Amazon.com: Books[/ame]
  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    For MEL, the built in documentation is quite good overall so I'd read that.
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    string $listuv[] = `ls -sl -fl`;

    float $uv1[]=`polyEditUV -q $listuv[0]`;
    float $uv2[]=`polyEditUV -q $listuv[1]`; //
    This should save you a couple of lines
    float $uvresult[];
    $uvresult[0]=`abs ($uv2[0]-$uv1[0])`;
    $uvresult[1]=`abs ($uv2[1]-$uv1[1])`;
    float $angle=`atan ($uvresult[1]/$uvresult[0])`;
    $angle=`rad_to_deg ($angle)`; //
    And there you have the angle

    polyEditUVShell -pu $uv1 [0] -pv $uv1[1] -r 0 -a $angle;


    thats what i have so far ...
    somehow it works somethimes, and sometimes not

    can you guys tell me where i made the mistake?
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    i think i got it :)
    ConvertSelectionToUVs;
    string $listuv[] = `ls -sl -fl`;

    float $uv1[]=`polyEditUV -q $listuv[0]`;
    float $uv2[]=`polyEditUV -q $listuv[1]`;

    float $uvresult[];
    $uvresult[0]=`abs ($uv1[0]-$uv2[0])`;
    $uvresult[1]=`abs ($uv1[1]-$uv2[1])`;
    float $angle=`atan ($uvresult[0]/$uvresult[1])`;
    $angle=`rad_to_deg ($angle)`;

    if ($uv1[1]>$uv2[1])
    {
    $angle=$angle*(-1);
    }

    if ($uv1[0]>$uv2[0])
    {
    $angle=$angle*(-1);
    }

    polyEditUVShell -pu $uv1[0] -pv $uv1[1] -a $angle;
  • DireWolf
    Options
    Offline / Send Message
    Rotating 1 degree is a neat idea.

    For more accuracy you may want to use Python's Math module. It gives you similar atan method along with pi and degrees.
    from maya import cmds
    from math import atan2, degrees, pi
    
    # Assuming you have 2 UV selected
    UVs = cmds.ls(sl=1, fl=1)  
    
    p1 = cmds.polyEditUV(UVs[0], q=1, u=1)
    p2 = cmds.polyEditUV(UVs[1], q=1, u=1)
    
    difX = p2[0] - p1[0]
    difY = p2[1] - p1[1]
    
    # Math stuff to find the degree of a line connecting the 2 points
    rads = atan2(difY, difX)
    rads %= 2*pi
    degs = degrees(rads)
    
    # Rotate the shell using the first point as pivot
    cmds.polyEditUVShell(pu=p1[0], pv=p1[1], a=-degs)
    
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    hey thank you :)

    why is is more accurate to use python?
    and whats the difference?
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    and what i just found out:
    markup menus do not support phyton -.-
  • DireWolf
    Options
    Offline / Send Message
    What mark up menu are you talking about?

    Accuracy - when you have 2 positions of a straight line, you can use math to find the exact angpe that line is doing. You know, math stuff. Python offers you the function to do exactly that.

    In a sense you can re-write that function yourself if you know the math behind it.
  • Phoenix995
    Options
    Offline / Send Message
    Phoenix995 polycounter
    thanks for the answer, but the mel script that i used, also used math and trigenometrie to calculate how much to rotate, i dont see, why it should not be as accurate as the phython script.

    i'm talking about the marktin menu in maya, my bad :)

    1428016817262
  • DireWolf
    Options
    Offline / Send Message
    I soppose it can be! Sorry if I missinform you, I havent done a whole lot of Mel :)
  • Deadly Nightshade
    Options
    Offline / Send Message
    Deadly Nightshade polycounter lvl 10
    I don't think Python is any more "accurate" at math than MEL is. The reasons why you pick Python over MEL are others:

    -Much faster (something which becomes clear when you deal with many objects/components or perform math-based operations)
    -Much better syntax - more readable and not as hard to fuck up with
    -You still have access to all MEL-commands via the python modules pymel.core.mel and maya.mel
    -Classes

    Then, when you need even more speed you will have to learn the Maya API and C++ (you can code Maya API with python as well but the usage is more limited. With C++ you have full access to Maya's API)

    I started out learning MEL, then Python > Maya.cmds > PyMEL and now I really don't see ANY use of MEL unless you want to become a
    Technical Artist. Python is simply less troublesome.

    Now, you still need to get your head around the Maya commands so learning MEL isn't a waste obviously. Just don't get too caved-in on it.
Sign In or Register to comment.