我在python中有这段代码:
"{0}.currentTime += 1;".format(hairSyst)
其中hairSyst
是前面定义的字符串。我不明白为什么我会收到语法错误。我的目标是在maya中设置一个表达式,并且表达式有点长,我将整个事情粘贴到下面,也许你可以建议一个更好的方法来做它。
expr = ("if ({0}.autoOverlap == 1){".format(firstControl.getName())
"{0}.currentTime += 1;".format(hairSyst)
"{0}.currentTime += 1;".format(nucleus)
"float $refresh_tx = {0}.translateX;".format(cube)
"float $refresh_ty = {0}.translateY;".format(cube)
"float $refresh_tz = {0}.translateZ;".format(cube)
"float $refresh_rx = {0}.rotateX;".format(cube)
"float $refresh_ry = {0}.rotateY;".format(cube)
"float $refresh_rz = {0}.rotateZ;".format(cube)
"}else if({0}.autoOverlap == 0){".format(firstControl.getName())
"{0}.currentTime = 1;".format(hairSyst)
"{0}.currentTime = 1;".format(nucleus)
"}"
)
答案 0 :(得分:1)
最好将其设为format
字符串:
expr = """if ({0}.autoOverlap == 1){{
{1}.currentTime += 1;
{2}.currentTime += 1;
float $refresh_tx = {3}.translateX;
float $refresh_ty = {3}.translateY;
float $refresh_tz = {3}.translateZ;
float $refresh_rx = {3}.rotateX;
float $refresh_ry = {3}.rotateY;
float $refresh_rz = {3}.rotateZ;
}}else if({0}.autoOverlap == 0){{"
{1}.currentTime = 1;
{2}.currentTime = 1;
}}""".format(firstControl.getName(), hairSyst, nucleus, cube)
请注意,使用"""
三重引号代替"
单引号来格式化多行字符串。
编辑:
如果原始字符串包含{ }
,我们必须按{{ }}
按class LeftExplorerItem : public Component, public MouseListener {
public:
LeftExplorerItem(String name = "LeftExplorerItem") : Component(name), isActive(false) {
setSize(100, 20);
addMouseListener(this, true);
}
void paint(Graphics& g) override {
if (!isActive) g.setColour(Colour(40, 40, 40));
else g.setColour(Colour(150, 190, 255));
g.fillRoundedRectangle(2, 2, getWidth() - 4, getHeight() - 4, 4);
g.setColour(Colours::white);
g.drawText("Frame #", 40, 0, 100, 25, Justification::centredLeft);
}
void mouseEnter(const MouseEvent& event) override {
AlertWindow("", "", AlertWindow::AlertIconType::InfoIcon);
isActive = true;
}
void mouseExit(const MouseEvent& event) override {
isActive = false;
}
void mouseUp(const MouseEvent& event) override {
AlertWindow("", "click", AlertWindow::AlertIconType::InfoIcon);
}
void resized() override {
}
private:
bool isActive;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LeftExplorerItem)
};
转义它们:
6.1.3。格式字符串语法
str.format()方法和Formatter类共享相同的语法 对于格式字符串(虽然在Formatter的情况下,子类可以 定义自己的格式字符串语法。)
格式字符串包含由大括号括起的“替换字段” {}。任何未包含在大括号中的内容都被视为文字 文本,未更改地复制到输出。 如果您需要包含 文字文本中的大括号字符,可以通过加倍来逃避: {{和}}。
答案 1 :(得分:0)
你必须连接这些字符串。现在,eval()
看到您传入了13个字符串,但eval()
只接受了一个字符串。所以做这样的事情:
expr = ("if ({0}.autoOverlap == 1){ ".format(firstControl.getName()) +
"{0}.currentTime += 1; ".format(hairSyst) +
"{0}.currentTime += 1; ".format(nucleus) +
"float $refresh_tx = {0}.translateX; ".format(cube) +
"float $refresh_ty = {0}.translateY; ".format(cube) +
"float $refresh_tz = {0}.translateZ; ".format(cube) +
"float $refresh_rx = {0}.rotateX; ".format(cube) +
"float $refresh_ry = {0}.rotateY; ".format(cube) +
"float $refresh_rz = {0}.rotateZ; ".format(cube) +
"}else if({0}.autoOverlap == 0){ ".format(firstControl.getName()) +
"{0}.currentTime = 1; ".format(hairSyst) +
"{0}.currentTime = 1; ".format(nucleus) +
"}"
)
我还在每个字符串后面添加了空格,以确保它们不会相互碰撞。