【MaxScript】画直线&曲线

因为要画实验结果图,想把通过算法重建出来的line/curve和模型叠在一起进行展示,所以学习了一下用MaxScript画线的技能。

画直线Line

此处是直接创建了cylinder图形,设置了半径为0.01。调用的时候直接drawCylinderBetweenTwoPoints [0,0,0] [1,1,1]就行了。

fn drawCylinderBetweenTwoPoints pt1 pt2= 
    cylinder radius:0.01 pos:pt1 dir:(pt2-pt1) height:(distance pt1 pt2)

画曲线Curve

theShape = SplineShape pos:[x,y,z]
addNewSpline theShape
addKnot theShape 1 #corner 1
addKnot theShape 1 #corner 2
...
updateShape theShape

不过由于这里创建的是spline样条线,是没有粗细的,所以渲染之前需要在修改器列表中选择添加“可渲染样条线”,然后修改“径向”为合适的参数。

读文件

下面提供使用读文件方式来读取控制点坐标数据进行绘制的方案,代码参考来自于can use maxscript control the point curve points。原代码是从一个只存了一条curve的控制点坐标的txt文本中读取数据,而我的txt文本中存了多条curve的控制点数据,格式如下:

x1, y1, z1
x1, y1, z1
...
x1, y1, z1

x2, y2, z2
x2, y2, z2
...
x2, y2, z2

x3, y3, z3
...
...

其中每一行xi, yi, zi都是分别属于第i根curve的控制点,不同curve之间用空行隔开。完整代码如下:

dataFilename = getOpenFileName filename:"pointcloud.txt"
if dataFilename != undefined then
(
    textFilestream = openFile dataFilename mode:"rt"
    if textFilestream != undefined then
    (
        while not(eof textFilestream) do
        (
            ss = (readLine textFilestream) as stringstream
            x = (readToken ss) as float
            y = (readToken ss) as float    
            z = (readToken ss) as float 
            theShape = SplineShape pos:[x,y,z]
            addNewSpline theShape
            
            ss = (readLine textFilestream) as stringstream
            while not(eof ss) do
            (
                x = (readToken ss) as float
                y = (readToken ss) as float    
                z = (readToken ss) as float    
                
                -- assuming we only have one spline within the shape
                addKnot theShape 1 #corner #line [-x,y,z]     --add the tangents to the end here
                
                if not(eof textFilestream) do
                    ss = (readLine textFilestream) as stringstream
            )
            updateShape theShape   -- don't forget to update
        )
    )
)

你可能感兴趣的:(【MaxScript】画直线&曲线)