请选择 进入手机版 | 继续访问电脑版

[Python] 【已解决】求助rhinopython提示SyntaxError: unexpected token

[复制链接]
20626 xysong56 发表于 2016-7-3 19:14:42 楼主
本帖最后由 筑梦NARUTO 于 2016-7-5 08:57 编辑
  1. import rhinoscriptsyntax as rs

  2. def smoothingvector(point,prev_point,next_point,s):
  3.     pm = (prev_point + next_point)/2
  4.     va = rs.VectorCreate(pm,point)
  5.     vm = rs.VectorScale(va,s)
  6.     return vm
  7.    
  8. def smoothingcurve(curve_id,s):
  9.     curve_point = rs.CurvePoints(curve_id)
  10.     new_curve_point=[]
  11.    
  12.     for i in range(1,len(curve_point)-1):
  13.         vm = smoothingvector(curve_point[i],curve_point[i-1],curve_point[i+1],s)
  14.         new_curve_point.append(rs.PointAdd(curve_point[i],vm)

  15.     knot = rs.CurveKnots(curve_id)
  16.     degree = rs.CurveDegree(curve_id)
  17.     weights = rs.CurveWeights(curve_id)
  18.     new_curve_id = rs.AddNurbsCurve(new_curve_points,knot,degree,weights)
  19.     if new_curve_id: rs.DeleteObject(curve_id)
  20.     return new_curve_id

  21. def iterativeshortencurve():
  22.     curve_id = rs.GetObject("Open curve to smooth",4,True)
  23.     if curve_id is None or rs.IsCurveClosed(curve_id): return
  24.    
  25.     min = rs.Distance(rs.CurveStartPoint(curve_id),rs.CurveEndPoint(curve_id))
  26.     max = rs.CurveLength(curve_id)
  27.     goal = rs.GetReal("goal length",0.5*(min+max),min,max)
  28.     if goal is None: return
  29.    
  30.     while rs.CurveLength(curve_id)>goal:
  31.         rs.EnableRedraw(False)
  32.         curve_id = smoothingcurve(curve_id, 0.1)
  33.         rs.EnableRedraw(True)
  34.         if curve_id is None: break
  35.         
  36. iterativeshortencurve()
复制代码
QQ图片20160703190628.jpg
关于大陆地区Rhino原厂培训中心
 楼主| xysong56 发表于 2016-7-3 19:16:21
2
照着rhinopython101码的,不知道为什么提示语法错误
 楼主| xysong56 发表于 2016-7-3 19:18:20
3
 楼主| xysong56 发表于 2016-7-3 19:39:03
4
rhinopython101 的42页内容
QQ图片20160703193358.png
筑梦NARUTO 发表于 2016-7-3 22:35:42
5
new_curve_point.append(rs.PointAdd(curve_point[i],vm))这句少写了一个括号
筑梦NARUTO 发表于 2016-7-3 22:53:24
6
错误有点多,我给你修改了,这下就可以运行达到书上的效果了。
  1. #coding=utf-8
  2. import rhinoscriptsyntax as rs

  3. def smoothingvector(point,prev_point,next_point,s):
  4.     pm = (prev_point + next_point)/2
  5.     va = rs.VectorCreate(pm,point)
  6.     vm = rs.VectorScale(va,s)
  7.     return vm
  8.    
  9. def smoothingcurve(curve_id,s):
  10.     curve_point = rs.CurvePoints(curve_id)
  11.     new_curve_point=[]
  12.     #还有一个逻辑错误就是你这里没有把起始点和终点加入列表
  13.     new_curve_point.append(curve_point[0])
  14.     for i in range(1,len(curve_point)-1):
  15.         vm = smoothingvector(curve_point[i],curve_point[i-1],curve_point[i+1],s)
  16.         #curve_point写错了
  17.         new_curve_point.append(rs.PointAdd(curve_point[i],vm))
  18.         #少了一个后括号
  19.     new_curve_point.append(curve_point[-1])
  20.     knot = rs.CurveKnots(curve_id)
  21.     degree = rs.CurveDegree(curve_id)
  22.     weights = rs.CurveWeights(curve_id)
  23.     new_curve_id = rs.AddNurbsCurve(new_curve_point,knot,degree,weights)
  24.     #new_curve_point写成new_curve_points了
  25.     #if new_curve_id: rs.DeleteObject(curve_id)
  26.     #把这句删了才能体现过程,达到和书的效果一样
  27.     return new_curve_id

  28. def iterativeshortencurve():
  29.     curve_id = rs.GetObject("Open curve to smooth",4,True)
  30.     if curve_id is None or rs.IsCurveClosed(curve_id): return
  31.    
  32.     min = rs.Distance(rs.CurveStartPoint(curve_id),rs.CurveEndPoint(curve_id))
  33.     max = rs.CurveLength(curve_id)
  34.     goal = rs.GetReal("goal length",0.5*(min+max),min,max)
  35.     if goal is None: return
  36.    
  37.     while rs.CurveLength(curve_id)>goal:
  38.         rs.EnableRedraw(False)
  39.         curve_id = smoothingcurve(curve_id, 0.1)
  40.         rs.EnableRedraw(True)
  41.         if curve_id is None: break
  42.         
  43. iterativeshortencurve()
复制代码


QQ截图20160703225850.png
 楼主| xysong56 发表于 2016-7-4 19:09:15
7
筑梦NARUTO 发表于 2016-7-3 22:35
new_curve_point.append(rs.PointAdd(curve_point,vm))这句少写了一个括号

月神好厉害
您需要登录后才可以回帖 登录 | 注册成为会员

本版积分规则