我想用从指定范围检索的随机量在给定轴上随机旋转一个对象列表。这是我想出来的:
import pymel.core as pm
import random as rndm
def rndmRotateX(targets, axisType, range=[0,180]):
for obj in targets:
rValue=rndm.randint(range[0],range[1])
xDeg='%sDeg' % (rValue)
#if axisType=='world':
# pm.rotate(rValue,0,0, obj, ws=1)
#if axisType=='object':
# pm.rotate(rValue,0,0, obj, os=1)
pm.rotate(xDeg,0,0,r=True)
targetList= pm.ls(sl=1)
randRange=[0,75]
rotAxis='world'
rndmRotateX(targetList,rotAxis,randRange)我使用pm.rotate()是因为它允许我指定是在世界空间还是在obj空间中完成旋转(据我所知,这与setAttr不同)。问题是,当我尝试运行以下代码时,它会引发以下错误:
# Error: MayaNodeError: file C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\pymel\internal\pmcmds.py line 140: #这一定与我为pm.rotate()输入参数的方式有关(我假设这是由于PyMel输出的行错误,这与它的参数转换函数有关),但我永远也找不出我做错了什么。:/
发布于 2012-12-16 16:15:45
就像直接调试你得到的东西一样...
问题01:区分大小写
pm.rotate("20deg",0,0)可以很好地工作,但pm.rotate("20Deg",0,0)会失败并抛出MayaNodeError,因为它认为您正在寻找一个名为“20Deg”的节点。基本上,您希望按照per:xDeg='%sdeg' % (rValue)来构建字符串
问题02:您依赖于pm.rotate()的隐式“将应用于选定对象”行为
在应用上述修复之前,您不会看到此问题,但如果您有两个选定的对象,并对它们运行(修补的) rndmRotateX函数,您将使这两个对象以完全相同的量旋转,因为pm.rotate()对选择(两个对象)进行操作,而不是对每个对象进行旋转。
如果您想要快速修复,则需要在旋转之前插入pm.select(obj)。你可能想要保存选择列表并恢复it...however IMHO,依赖这样的选择是一个非常糟糕的想法,所以我会让你接近Kim的答案。
发布于 2012-11-20 17:44:16
我想问题出在这一行。
pm.rotate(rValue,0,0, obj, os=1)obj应该是第一个参数,所以它应该是
pm.rotate(obj, (rValue,0,0), os=1)但是为了让它更漂亮,你可以使用
obj.setRotation((rValue,0,0), os=1)还有。使用pm.selected()而不是pm.ls(sl=1)。它看起来好多了
发布于 2013-05-02 01:17:28
做这件事的另一种方式..
from pymel.core import *
import random as rand
def rotateObjectsRandomly(axis, rotateRange):
rotateValue = rand.random() * rotateRange
for obj in objects:
PyNode(str(selected()) + ".r" + axis).set(rotateValue)
objectRotation = [[obj, obj.r.get()] for obj in selected()]
print "\nObjects have been rotated in the {0} axis {1} degrees.\n".format(axis, rotateValue)
return objectRotation
rotateObjectsRandomly("z", 360)由于rand.random()返回一个介于0- 1之间的随机值,我只是将其乘以我首选的user..or指定的rotateRange,我将把它全部去掉,并将其乘以360……
你也不需要所有的反馈,我只是觉得它在运行时看起来很好..
Objects have been rotated in the z axis 154.145898182 degrees.
# Result: [[nt.Transform(u'myCube'), dt.Vector([42.6541437517, 0.0, 154.145898182])]] # https://stackoverflow.com/questions/13468967
复制相似问题