# Nuke Python 数学

##### 使用矩阵

Setting Up Stereo

##### 案例
###### 绘制点

``````def paintPoints():
geoNode = nuke.activeViewer().node()
camera = nuke.selectedNode()
``````

``````if not camera.Class() in ( 'Camera', 'Camera2' ):
nuke.message( 'Please select a camera node first')
return
``````

``````geoKnob = geoNode['geo']
objs = geoKnob.getGeometry()
if not objs:
nuke.message( 'No geometry found in %s' % geoNode.name() )
``````

• 迭代所有物体
• 获得每个物体的世界坐标
• 迭代每个物体上的所有点
• 获取每个点的本地坐标，并乘以此点的父物体的变换矩阵来获取此点的世界位置：
``````pts = []
for o in objs:
objTransform = o.transform()
for p in o.points():
worldP = objTransform * nuke.math.Vector4( p.x, p.y, p.z, 1 )
pts.append( [worldP.x, worldP.y, worldP.z] )
``````

``````def paintPoints():
geoNode = nuke.activeViewer().node()
camera = nuke.selectedNode()

if not camera.Class() in ( 'Camera', 'Camera2' ):
nuke.message( 'Please select a camera node first')
return

geoKnob = geoNode['geo']
objs = geoKnob.getGeometry()
if not objs:
nuke.message( 'No geometry found in %s' % geoNode.name() )

pts = []
for o in objs:
objTransform = o.transform()
for p in o.points():
worldP = objTransform * nuke.math.Vector4(p.x, p.y, p.z, 1)
pts.append( [worldP.x, worldP.y, worldP.z] )
``````

``````curvesKnob = nuke.createNode( 'RotoPaint' )['curves']
``````

``````task  = nuke.ProgressTask( 'painting points' )
``````

``````for i, pt in enumerate( pts ):
break
task.setMessage( 'painting point %s' % i )
``````

``````stroke = nuke.rotopaint.Stroke( curvesKnob )
``````

``````pos= nukescripts.snap3d.projectPoint( camera, pt )
``````

``````ctrlPoint = nuke.rotopaint.AnimControlPoint( pos )
``````

``````stroke.append( ctrlPoint )
``````

``````curvesKnob.rootLayer.append( stroke )
``````

``````task.setProgress( int( float(i)/len(pts)*100 ) )
``````

``````import nuke
import nukescripts

def paintPoints():
'''
Rather experimental but kinda fun. This projects sleected 3D points through a camera ito screen space
and draws a dot for each using a paint stroke.
'''
# GET THE GEO NODE FROM THE CURRENTLY ACTIVE VIEWER
geoNode = nuke.activeViewer().node()

# WE EXPECT A CAMERA TO BE SELECTED
camera = nuke.selectedNode()
if not camera.Class() in ( 'Camera', 'Camera2' ):
nuke.message( 'Please select a camera node first')
return

# COLLECT ALL OBJECTS IN THE CURRENT GEO KNOB. QUIT IFNONE WERE FOUND
geoKnob = geoNode['geo']
objs = geoKnob.getGeometry()
if not objs:
nuke.message( 'No geometry found in %s' % geoNode.name() )

pts = []
for o in objs:
# CYCLE THROUGH ALL OBJECTS
objTransform = o.transform()
for p in o.points():
# CYCLE THROUGH ALL POINTS OF CURRENT OBJECT
worldP = objTransform * nuke.math.Vector4(p.x, p.y, p.z, 1)
pts.append( [worldP.x, worldP.y, worldP.z] )

# CREATE THE NODE THAT WILL HOLD THE PAINT STROKES
curvesKnob = nuke.createNode( 'RotoPaint' )['curves']

for i, pt in enumerate( pts ):
break
task.setMessage( 'painting point %s' % i )
# CREATE A NEW STROKE
stroke = nuke.rotopaint.Stroke( curvesKnob )
# PROJECT THE POINT TO SCREEN SPACE
pos = nukescripts.snap3d.projectPoint( camera, pt )
# CREATE ANE CONTROL POINT FOR
ctrlPoint = nuke.rotopaint.AnimControlPoint( pos )
# ASSIGN IT TO THE STROKE
stroke.append( ctrlPoint )
# ASSIGN TH E STROKE TO THE ROOT LAYER
curvesKnob.rootLayer.append( stroke )
# UPDARE PROGRESS BAR