Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This very first script is explained and commented on in detail to give you an idea of how to write a script. While creating a program, it is absolutely important to have a plan and a concept, because without these you will hardly ever get the desired result, especially as a beginner. With growing experience and short scripts it is not always to necessary to create a complete syntax structure, but for more complex programs it is mandatory.

 

Name

Objects2Vertices.rfs

Type

Batch script

Description

This script reads out the vertices of all objects in the scene and writes them into a list. The position data are then used to place a certain object at the vertices location. These objects will be created automatically.

...

This notation indicates that getObjects() will only catch true objects and write them to a list, which is represented with square brackets. getSelectedNodes() does not distinguish between node types – all nodes  are written to the list and it is up to you to sort out which one you can use. Just a tip: The getType() instruction will help you out here.

Since this script should only use true objects, the getObjects() statement is the best choice. Whenever there is more than one element inside a list you have to create a loop to make them accessible. The for … in loop will perfectly serve your needs, but before it can be executed, a counter variable has to be introduced and finally incremented:

...

The next step is to get the vertices from each single_object and the individual positions. Again there is more than one element, so the next variable will be a list, too:

...

The result from this second loop is vertex_position , which is a  vector – what you receive from getPosition() is a trio of three values for the X, Y and Z coordinates, which is considered a “vector” in RealFlow. This vector will then be used as a reference for the clone objects. To proceed, an object is needed for each vertex – here it is a sphere, but you can choose any other object. Creating objects is a little bit different from adding emitters or daemons, because each object has its own statement:

...

If you want to add another object type, you can write: addCube(), addTorus(), addRocket() and so on. Once it is created, RealFlow assigns a new name to it, for example “Sphere01”, “Sphere23”, “Sphere175” etc.

...

Unless the counter has reached 10, the name should be written as 01, 02, 03, 04 and so on. For this purpose the leading 0 is used and then combined with the string value of the counter: str(counter). With the “+” operator both elements are assembled together. Once 10 has been reached, the name just corresponds with the counter. Now the appropriate clone object can be identified. Finally it is necessary to translate the current vertex positions to a clone object and rescale it: 

...

Code Block
themeEclipse
languagepython
linenumberstrue
# Batch script

counter    = 1
objectList = scene.getObjects()

for single_object in objectList:
    vertexList = single_object.getVertices()

    for single_vertex in vertexList:
        vertex_position = single_vertex.getPosition()
        scene.addSphere()

        if (counter < 10):
            cloneObject_name= "Sphere0"+str(counter)

        else:
            cloneObject_name = "Sphere"+str(counter)

        cloneObject = scene.getObject(cloneObject_name)
        cloneObject.setParameter("Position", vertex_position)
        cloneObject.setParameter("Scale", Vector.new(0.2, 0.2, 0.2))
        counter += 1