Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

In maya rotate, have an input variable toggle for degrees, radians, etc

In maya rotate, have an input variable toggle for degrees, radians, etc

For some reason, we have to enter the rotates in cmds.rotate as '150deg' or something similar. Why? It would make more sense to me if we put in a number instead of a string and have an additional variable that sets it to degrees, radians, etc. This whole 'have to enter a number and string' thing is really unintuitive, since it's like the only commonly used function that does it

2 Comments
tkaap2
Autodesk


For workflows that use multiple unit types, it would indeed be useful to have a parameter-time flag.

 

But it's not the case that you -must- specify a unit. It's optional. You can certainly just pass in a numerical value. The `move` command works the same way -- you can specify units in the parameter values, but it's optional.

 

Below are some examples and workflow ideas that you can use in your scripts that might make life easier:

# All of these examples assume that you have this cube selected.
# I tend to run these scriptlets in chunks -- just a few lines at at time 
# to see the effect on the cube:
cmds.file(new=True,force=True)
cmds.polyCube()

# These will each rotate the cube by units in 
# the current angular unit, whichever it is set to:
# "degree" or "radian"
# 
cmds.rotate(1,0,0,r=True)
cmds.rotate(10,0,0,r=True)

# You can change the current angular unit.  Then the
# effect of using the raw numerical values will change
#
cmds.currentUnit(angle="degree")
cmds.currentUnit(angle="radian")


# These will each rotate the cube by the specified
# units.
cmds.rotate('1.0deg',0,0,r=True)
cmds.rotate('1.0rad',0,0,r=True)

# This is for lunatics, but it works.  This will
# rotate X by radians, Y by degrees, and Z by the current 
# angular unit.
cmds.rotate('.785rad','45deg',45)

# Some string formatting could be used to make it 
# convenient to only pass numerical values to the
# rotate command:
rotationValues = (1,2,3)
# degrees 
cmds.rotate("%fdeg"%rotationValues[0],"%fdeg"%rotationValues[1],"%fdeg"%rotationValues[2],r=True)
# radians
cmds.rotate("%frad"%rotationValues[0],"%frad"%rotationValues[1],"%frad"%rotationValues[2],r=True)


# You could even separate out the string formatting step:
def degreeRotations(value):
    return ("%fdeg"%val for val in value)
def radianRotations(value):
    return ("%frad"%val for val in value)    
# degrees 
cmds.rotate(*degreeRotations(rotationValues),r=True)
# radians
cmds.rotate(*radianRotations(rotationValues),r=True)


# You could also wrap the conversion logic, so that it
# operates with an input parameter
from math import degrees
def degreesRotationValues(value, inputIsDegrees=True):
    """ This method returns rotation values in degrees"""
    if inputIsDegrees:
        return ("%fdeg"%val for val in value)
    else:
        return ("%fdeg"%degrees(val) for val in value)

# Then your call to rotate does just work on an input
#  parameter toggle:
degreesValues = (45,45,45)
cmds.rotate(*degreesRotationValues(degreesValues),r=True)

radiansValues = (.785,.785,.785)
cmds.rotate(*degreesRotationValues(radiansValues,inputIsDegrees=False),r=True)


# You cold also rework this to query and store the 
# current angular unit, do your rotations purely as
# numerical values, then restore (if needed) the 
# previous angular unit.  This would avoid all
# string formatting steps:

# Store the current angular unit
currAngularUnit  = cmds.currentUnit(q=True,angle=True)
if currAngularUnit is not "deg":
    cmds.currentUnit(angle="degree")

# Rotate degree values directly, always in degrees
degreesValues = (45,45,45)
cmds.rotate(*degreesValues,r=True)

# Rotate radian values in degrees after converting them:
radiansValues = (.785,.785,.785)
degreesValues = map(degrees,radiansValues)
cmds.rotate(*degreesValues,r=True)
    
# Restore the angular unit
if currAngularUnit is not currAngularUnit:
    cmds.currentUnit(angle=currAngularUnit)

    

 

Peteman12
Advocate

I don't know what to say. I've run into issues where not using '90deg' resulted in errors.

Can't find what you're looking for? Ask the community or share your knowledge.

Submit Idea