将字符串转换为浮点值

huangapple go评论74阅读模式
英文:

how to convert string to float value

问题

import maya.cmds as cmds

# 获取第一个选择的物体
sel = cmds.ls(sl=True)[0]

# 获取第一个选择的物体的动画曲线
animCurves = cmds.listConnections(sel, type='animCurve')

# 获取动画曲线的时间范围
startTime = cmds.findKeyframe(animCurves[0], which='first')
endTime = cmds.findKeyframe(animCurves[0], which='last')

# 获取用户输入的偏移值
offset = float(cmds.promptDialog(title='Offset', message='输入偏移值:', button=['OK', 'Cancel'], defaultButton='OK', cancelButton='Cancel', dismissString='Cancel'))

# 遍历所有其他选择的物体,将动画粘贴并加上偏移
for obj in cmds.ls(sl=True)[1:]:
    cmds.copyKey(sel, time=(startTime, endTime))
    cmds.pasteKey(obj, time=(startTime + offset, endTime + offset))
英文:

I have this script where I'd like to copy animation based on user selection and paste back with offset. For the offset I'd like to use the input value from user but I get error in string to float, can someone help me to fix this ?
Thanks in advance

import maya.cmds as cmds

# Get the first selection
sel = cmds.ls(sl=True)[0]

# Get the animation curves from the first selection
animCurves = cmds.listConnections(sel, type='animCurve')

# Get the time range of the animation curves
startTime = cmds.findKeyframe(animCurves[0], which='first')
endTime = cmds.findKeyframe(animCurves[0], which='last')

# Get the offset value from user input
offset = float(cmds.promptDialog(title='Offset', message='Enter offset value:', button=['OK', 'Cancel'], defaultButton='OK', cancelButton='Cancel', dismissString='Cancel'))

# Loop through all other selections and paste animation with offset
for obj in cmds.ls(sl=True)[1:]:
    cmds.copyKey(sel, time=(startTime, endTime))
    cmds.pasteKey(obj, time=(startTime + offset, endTime + offset))

答案1

得分: 1

maya.cmds.promptDialog 在第一次调用时,返回用户点击的按钮的标签,或者如果用户关闭了提示而没有点击标记按钮,则返回dismissString标志的值(在这种情况下为'Cancel')。如果再次使用query=Truetext=True调用它,然后返回用户的文本。

英文:

maya.cmds.promptDialog does not return the text the user typed, or at least, not the first time you call it:

> Return value
>
> string Indicates how the dialog was dismissed. If a button is pressed then the label of the button is returned. If the dialog is closed then the value for the flag ds/dismissString is returned.
>
> In query mode, return type is based on queried flag.

The first time you call it, it returns either the label of the button the user clicked, or the value of the dismissString flag ('Cancel' in this case) if the user closed the prompt without clicking one of the labeled buttons.

If you call it again with query=True and text=True, then it returns the user's text:

closed_how = cmds.promptDialog(title='Offset', message='Enter offset value:', button=['OK', 'Cancel'], defaultButton='OK', cancelButton='Cancel', dismissString='Cancel')

if closed_how == 'OK':
    try:
        offset = float(cmds.promptDialog(query=True, text=True))
    except ValueError:
        # User entered invalid text. Do something about that.

huangapple
  • 本文由 发表于 2023年4月10日 23:40:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定