[英]TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType
I need the edges of the object to be painted in color.我需要将 object 的边缘涂上颜色。 I know python very poorly, but my teacher gave this code and this is what happened with it.
我知道 python 很差,但是我的老师给出了这个代码,这就是它发生的事情。
def triangleArea(half_edge):
a = len (half_edge)
b = len (half_edge.edge_next)
c = len (half_edge.edge_next.edge_next)
s = (a + b + c) / 2
area = math.sqrt(s * (s - a)*(s - b)*(s - c))
return area
def angle(half_edge):
v1 = half_edge.vertex
v2 = prev(half_edge).vertex
v3 = half_edge.edge_next.vertex
angle = math.acos(((v2.co.x - v1.co.x)*(v3.co.x - v1.co.x)+(v2.co.y - v1.co.y)*(v3.co.y - v1.co.y)+(v2.co.z - v1.co.z)*(v3.co.z -v1.co.z))/(len(prev(half_edge))*len(half_edge)))
if (angle > math.pi / 2):
print (">90")
return angle
def curvature(v):
anglesSum = 0
completeArea = 0
for e in h_edge_list:
if (v == e.vertex):
start_edge = e
anglesSum += angle(start_edge)
completeArea += triangleArea(start_edge)
half_edge = start_edge.edge_next
cur = (2*math.pi - anglesSum)/completeArea
color (half_edge, cur, 0.13, 0.29)#farbime z intervalu 0 az 2/3
return cur
for e in h_edge_list:
print (curvature(e.vertex))
bmesh.free() # free and prevent further access
error in the last lines of code with the S operator enter image description here S 运算符的最后几行代码出错 在此处输入图像描述
Your angle
function returns None
when this condition is False当此条件为 False 时,您的
angle
function 返回None
if (angle > math.pi / 2):
print (">90")
return angle
This is how return
in your current angle
function looks like这就是您当前
angle
function 的return
方式
def angle(half_edge):
v1 = half_edge.vertex
v2 = prev(half_edge).vertex
v3 = half_edge.edge_next.vertex
angle = math.acos(((v2.co.x - v1.co.x)*(v3.co.x - v1.co.x)+(v2.co.y - v1.co.y)*(v3.co.y - v1.co.y)+(v2.co.z - v1.co.z)*(v3.co.z -v1.co.z))/(len(prev(half_edge))*len(half_edge)))
if (angle > math.pi / 2):
print (">90")
return angle
return None
It returns None if the (angle > math.pi / 2)
condition is False
如果
(angle > math.pi / 2)
条件为False
,则返回 None
Maybe you want to return the angle
value, whatever the condition is (this makes the if statement unnecessary), by reducing its indentation tab也许你想返回
angle
值,无论条件是什么(这使得 if 语句变得不必要),通过减少它的缩进标签
def angle(half_edge):
v1 = half_edge.vertex
v2 = prev(half_edge).vertex
v3 = half_edge.edge_next.vertex
angle = math.acos(((v2.co.x - v1.co.x)*(v3.co.x - v1.co.x)+(v2.co.y - v1.co.y)*(v3.co.y - v1.co.y)+(v2.co.z - v1.co.z)*(v3.co.z -v1.co.z))/(len(prev(half_edge))*len(half_edge)))
if (angle > math.pi / 2):
print (">90")
return angle
This if
statement will return None
if this condition is False
如果此条件为
False
,则此if
语句将返回None
if (angle > math.pi / 2):
print (">90")
return angle
You'll want to add at least an else
statement to your angle function.您需要在角度 function 中至少添加一条
else
语句。
The angle function is returning NoneType if the condition inside is not satisfied, and the += operator can't be used with NoneType values.如果不满足内部条件,则角度 function 将返回NoneType ,并且 += 运算符不能与 NoneType 值一起使用。 Try returning 0 instead.
尝试返回 0。
if (angle > math.pi / 2):
print (">90")
return angle
else:
return 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.