【问题标题】:def function gives invalid syntaxdef 函数给出了无效的语法
【发布时间】:2017-06-27 10:16:12
【问题描述】:

据我检查,缩进是正确的,没有缺少括号,我只在前几行中导入了包但我仍然收到无效的语法错误。

#!/usr/bin/python
import bpy
import mathutils
import numpy as np
from math import radians
from mathutils import Vector
from math import radians
from mathutils import Matrix
from bpy import context

def transform_mesh('parent', 'obj_to_be_transformed', (translate_x, translate_y, translate_z), (rot_x,rot_y,rot_z)):

    obj= bpy.data.objects[parent]
    obj1= bpy.data.objects[obj_to_be_transformed]
    initial_mat = obj1.matrix_world

    ...some code

    (x,y,z) = (translate_x, translate_y, translate_z)  
    orig_loc_mat = Matrix.Translation(orig_loc+ mathutils.Vector((x,y,z)))

    ...some more code

    eul = mathutils.Euler((radians(rot_x), radians(rot_y), radians(rot_z)), 'XYZ')
    rot_mat = eul.to_matrix().to_4x4()

    obj.matrix_world = orig_loc_mat * rot_mat * orig_rot_mat * orig_scale_mat 
    bpy.context.scene.update()

    return [initial_loc,initial_rot,initial_scale,loc,rot,scale]


transform_result= transform_mesh('Armature','Coil',(5,0,0),(0,0,1))
print (transform_result)

错误是:

Error:   File "D:\users\gayathri\Gayathri\Synthetic_data_generation\Final\HMI_Depth_coilA_final_final.blend\Untitled", line 18
def transform_mesh('parent', 'obj_to_be_transformed', (translate_x, translate_y, translate_z), (rot_x,rot_y,rot_z)):
                                                                                                          ^
SyntaxError: invalid syntax

location: <unknown location>:-1

【问题讨论】:

  • 你的论点不应该是字符串:transform_mesh(parent, obj_to_be_transformed, (translate_x, translate_y, translate_z), (rot_x,rot_y,rot_z)):
  • 哇,我不知道嵌套参数列表是可能的。
  • @mkrieger1:这些不是完全嵌套的参数,它们是作为参数的元组。
  • 好的,显然在较新的 Python 版本中不再可能:stackoverflow.com/a/28908040
  • @Nuageux 好的,现在它在 (translate_x, translate_y, translate_z) 处指出错误

标签: python python-2.7 syntax-error


【解决方案1】:
def transform_mesh('parent', 'obj_to_be_transformed',

应该是

def transform_mesh(parent, obj_to_be_transformed,

确定吗?

【讨论】:

    【解决方案2】:

    1- 从参数中删除字符串

    2- 从参数中删除元组并将它们归入函数中(添加一些检查可能很有用)

    所以,你来了:

    def transform_mesh(parent, obj_to_be_transformed, translate, rot):
        translate_x, translate_y, translate_z= translate
        rot_x,rot_y,rot_z = rot
    
        # etc
    
    
    transform_result= transform_mesh('Armature','Coil',(5,0,0),(0,0,1))
    print (transform_result)
    

    【讨论】:

    • 不客气。随意接受您喜欢的答案,以便您的问题被标记为已解决:)
    【解决方案3】:

    Python3不支持元组参数,但可以将其作为变量传递,定义函数后解包。

    def transform_mesh(translate_xyz):
        translate_x, translate_y, translate_z = translate_xyz    
    

    【讨论】:

    • 我使用 python 2.7 但会尝试仅作为参数传递并尝试。
    【解决方案4】:

    您需要提供变量作为函数的参数。

    尝试类似:

    def transform_mesh(parent, obj_to_be_transformed, t1, t2):
    

    尽管在您共享的代码中,您始终使用 t1 和 t2 作为元组。但是如果你想分别使用 x、y 和 z,你可以通过引用索引来实现:

    x = t1[0]
    y = t1[1]
    

    【讨论】:

    • 或者干脆x, y = t1
    • @mkrieger1 同意。但仅在我们单独需要所有元素的情况下。
    【解决方案5】:

    在这一行中,函数参数的传递方式不正确,

        def transform_mesh('parent', 'obj_to_be_transformed', (translate_x, translate_y, translate_z), (rot_x,rot_y,rot_z)):
    

    正确的语法是:

        def transform_mesh(parent, obj_to_be_transformed, *translate_xyz, *rot_xyz): #*translate_xyz and *rot-xyz are tuple parameter
    

    【讨论】:

      猜你喜欢
      • 2014-06-29
      • 2021-08-30
      • 2021-01-17
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多