Vizard 7 » Reference » 3D models » Creating 3D models on-the-fly » Changing vertices on-the-fly
7.6

Tutorial: Changing vertices on-the-fly

One major benefit of creating objects on-the-fly is that you can also modify individual vertices on-the-fly. You can also create a handle for a vertex so that it can be the source or destination of a link and so that actions can be applied to it.

Note: if you're going to be modifying your on-the-fly object frequently, include "myObj.dynamic()" within your script (where myObj is the name of your on-the-fly object). This will speed up the rendering of your object. To return the object to the default rendering mode, use "myObj.static()".

Change the position, color, or nomal vector of a vertex

Once you've made a layer, you can change individual vertices in that layer. Use <node3d:onthefly>.setVertex to move the vertex position, <node3d:onthefly>.setVertexColor to change the vertex color, and <node3d:onthefly>.setNormal to change the normal vector. For all of these commands, specify the vertex you would like to change (vertex indices begin at 0 and are assigned in the order in which they were added).  To get information about a vertex, use the corresponding 'get' commands.

import viz
viz.go()

#Create a layer.
viz.startLayer(viz.TRIANGLES)
viz.vertex(0,1,5)
viz.vertex(-1,2,5)
viz.vertex(1,2,5)
myTriangle= viz.endLayer()

#Notify Vizard that the layer will be modified frequently.
myTriangle.dynamic()

vizact.onkeydown('1', myTriangle.setVertex, 0, [0,3.5,5] )
vizact.onkeydown('2', myTriangle.setVertexColor, 1, viz.BLUE)

#Get the normal vector of the third (2) vertex.
def printNormal():
    print(myTriangle.getNormal(2))

vizact.onkeydown('3', printNormal)

Changing texture coordinates

Once you've made a layer, you can also change the textures coordinates using <node3d:onthefly>.setTexCoord.

#Create a layer.
viz.startLayer(viz.QUADS)
viz.texCoord(0,0)
viz.vertex(-1,1,5)
viz.texCoord(1,0)
viz.vertex(1,1,5)
viz.texCoord(1,1)
viz.vertex(1.5,3,5)
viz.texCoord(0,1)
viz.vertex(-1.5,3,5)
myQuad = viz.endLayer()
myQuad.texture(viz.add('image2.jpg'))

#Notify Vizard that the layer will be modified frequently.
myQuad.dynamic()

#Change the texture coordinates of the first vertex.
vizact.onkeydown('1', myQuad.setTexCoord, 0, [.5,.5] )

Applying actions and links to a vertex

To apply actions to a vertex or use it as the source or destination of a link, grab the vertex with the  <node3d:onthefly>.Vertex( <vertex index>) command and then proceed as you would with any other node.

#Example for creating a fan of triangles:
viz.startLayer(viz.TRIANGLE_FAN)
viz.vertex(0,1,5) #All the triangles have the first vertex as a point.
viz.vertex(-1.5,1.35,10) #The other points are taken in pairs.
viz.vertex(-.25,1.5,10)
viz.vertex(0,.8,10)
viz.vertex(.25,1.5,10)
viz.vertex(1.5,1.35,10)
myTrianglefan = viz.endLayer()

#Notify Vizard that the layer will be modified frequently.
myTrianglefan.dynamic()

#Grab a vertex.
tip = myTrianglefan.Vertex( 0 )
fadeInAndOut = vizact.sequence( [vizact.fadeTo(viz.RED,time=1),vizact.fadeTo(viz.WHITE,time=1)], viz.PERPETUAL )
#Apply an action to the vertex.
tip.addAction( fadeInAndOut )

Adding a new layer

Use <node3d:onthefly>.startLayer to add an on-the-fly layer to an existing on-the-fly object.

See also

In this section:

Creating 3D models on-the-fly-- the basics

Vertex properties

Creating models on-the-fly command table

Other sections:

3D model transform basics

Tutorial: On-the-fly objects

Linking basics

Action basics

Example scripts:

3D objects on-the-fly