Python Helper App
Placing Spheres at Mesh Vertices


(OSX and Linux Only)


return to main index



Introduction

This tutorial provides an explanation of how spheres can be rendered at the vertices of a polymesh by the use of a so-called "helper app" written in python.



Figure 1 (rollover)



The Python Code

The technique shown in this tutorial uses two supporting python scripts:
   shape_utils.py
   proc_data.py
and a python script that implements the actual "helper app":
   place_spheres.py
The shape_utils.py script should be saved in the maya/scripts folder.
The proc_data.py script can also be saved in the maya/scripts folder or, if the suggestions for customizing RfM 22 have been followed, the script should be saved in the rfm_scripts/python directory.

The place_spheres.py script should be saved in the Maya project directory.

/Users/user_name/Documents/maya/scripts/shape_utils.py
/Users/user_name/Documents/maya/scripts/proc_data.py
/Users/user_name/Documents/maya/projects/helper_demo/place_spheres.py

Using the Scripts in Maya

Assuming a project directory called "helper_demo" has been set in Maya, create a simple polymesh object such as a torus. From the RenderMan menu choose Archive->Create Run Program Node.



Figure 2


Parent the RenderManProgram node to the polymesh.



Figure 3


Adding an Interface

This section demonstrates how custom attributes should be added to the RenderManProgramShape tab.

In Maya's outliner choose, Display->Shapes.



Figure 4


In the outliner select the RenderManProgramShape shape node.

In the Attribute Editor for the RenderManProgramShape choose,
    Attributes->Add Attributes...



Figure 5


Create a float attribute named "radius",
a string attribute named "target", and
a boolean attribute named "use_local_space".

Make sure the names are lower case and that "Override nice name" is checked and that the nice name is also lower case. The new attributes will appear at the bottom of the panel. Make sure they have reasonable default values.



Figure 6


Connecting the Interface

A small python script will be used to update the RenderManProgramShape Data textfield when the values of the custom float, string and boolean attributes are adjusted. Copy and paste the following code into the Pre Shape Python Script text field.


import rfm2.api.strings as apistr; import proc_data; proc_data.PlaceSpheres(apistr.expand_string("<shape>"))

Note the code must be pasted as a single line of text.



Figure 7


Use the browse button of the Executable File Name text field to assign the path to the "place_spheres.py" helper app python script.



Figure 8


When the scene is rendered the line of code in the Pre Shape Python Script text field will call the proc_data.py script that will, in turn, call the get_coordinates() procedure implemented by the shape_utils.py script. The PlaceSpheres() procedure of the proc_data.py script also querries the custom attributes and finally assigns text to to the RenderManProgramShape "data" textfield. The text is passed to the helper app.

As a result no matter how the vertices of the mesh object move, the spheres generated by the python helper app will be rendered in their correct locations.





How much text can be passed to a Helper App?

The motivation for implementing the technique of rendering spheres, or any geometry, at the vertices of a polymesh was to see how much text could be passed to a python helper app as well as to determine the efficiency of the technique. For the torus shown above here is a print-out of the "data" string consisting of 1200 coordinates. Python seems to handle the transfer of the data string very well even when passing 30,000 coordinates.

However, "Maya+Python+RenderMan" does get somewhat sluggish when dealing with a polymesh with 250,000 vertices - 750,000 coordinates!

0.03 1200 0.494 0.0 -0.078 0.476 0.0 -0.155 0.446 0.0 -0.227 0.405 0.0 -0.294 
0.354 0.0 -0.354 0.294 0.0 -0.405 0.227 0.0 -0.446 0.155 0.0 -0.476 0.078 0.0 
-0.494 0.0 0.0 -0.5 -0.078 0.0 -0.494 -0.155 0.0 -0.476 -0.227 0.0 -0.446 
-0.294 0.0 -0.405 -0.354 0.0 -0.354 -0.405 0.0 -0.294 -0.446 0.0 -0.227 -0.476 
0.0 -0.155 -0.494 0.0 -0.078 -0.5 0.0 -0.0 -0.494 0.0 0.078 -0.476 0.0 0.155 
-0.446 0.0 0.227 -0.405 0.0 0.294 -0.354 0.0 0.354 -0.294 0.0 0.405 -0.227 0.0 
0.446 -0.155 0.0 0.476 -0.078 0.0 0.494 -0.0 0.0 0.5 0.078 0.0 0.494 0.155 0.0 
0.476 0.227 0.0 0.446 0.294 0.0 0.405 0.354 0.0 0.354 0.405 0.0 0.294 0.446 0.0 
0.227 0.476 0.0 0.155 0.494 0.0 0.078 0.5 0.0 0.0 0.588 0.294 -0.093 0.566 
0.294 -0.184 0.531 0.294 -0.27 0.482 0.294 -0.35 0.421 0.294 -0.421 0.35 0.294 
-0.482 0.27 0.294 -0.531 0.184 0.294 -0.566 0.093 0.294 -0.588 0.0 0.294 -0.595 
-0.093 0.294 -0.588 -0.184 0.294 -0.566 -0.27 0.294 -0.531 -0.35 0.294 -0.482 
-0.421 0.294 -0.421 -0.482 0.294 -0.35 -0.531 0.294 -0.27 -0.566 0.294 -0.184 
-0.588 0.294 -0.093 -0.595 0.294 -0.0 -0.588 0.294 0.093 -0.566 0.294 0.184 
-0.531 0.294 0.27 -0.482 0.294 0.35 -0.421 0.294 0.421 -0.35 0.294 0.482 -0.27 
0.294 0.531 -0.184 0.294 0.566 -0.093 0.294 0.588 -0.0 0.294 0.595 0.093 0.294 
0.588 0.184 0.294 0.566 0.27 0.294 0.531 0.35 0.294 0.482 0.421 0.294 0.421 
0.482 0.294 0.35 0.531 0.294 0.27 0.566 0.294 0.184 0.588 0.294 0.093 0.595 
0.294 0.0 0.835 0.476 -0.132 0.804 0.476 -0.261 0.753 0.476 -0.384 0.684 0.476 
-0.497 0.598 0.476 -0.598 0.497 0.476 -0.684 0.384 0.476 -0.753 0.261 0.476 
-0.804 0.132 0.476 -0.835 0.0 0.476 -0.845 -0.132 0.476 -0.835 -0.261 0.476 
-0.804 -0.384 0.476 -0.753 -0.497 0.476 -0.684 -0.598 0.476 -0.598 -0.684 0.476 
-0.497 -0.753 0.476 -0.384 -0.804 0.476 -0.261 -0.835 0.476 -0.132 -0.845 0.476 
-0.0 -0.835 0.963 0.132 -0.804 0.963 0.261 -0.753 0.963 0.384 -0.684 0.963 
0.497 -0.598 0.963 0.598 -0.497 0.963 0.684 -0.384 0.476 0.753 -0.261 0.476 
0.804 -0.132 0.476 0.835 -0.0 0.476 0.845 0.132 0.476 0.835 0.261 0.476 0.804 
0.384 0.476 0.753 0.497 0.476 0.684 0.598 0.476 0.598 0.684 0.476 0.497 0.753 
0.476 0.384 0.804 0.476 0.261 0.835 0.476 0.132 0.845 0.476 0.0 1.448 0.787 
-0.181 1.098 0.476 -0.357 1.029 0.476 -0.524 0.934 0.476 -0.679 0.816 0.476 
-0.816 0.679 0.476 -0.934 0.524 0.476 -1.029 0.357 0.476 -1.098 0.181 0.476 
-1.14 0.0 0.476 -1.155 -0.181 0.476 -1.14 -0.357 0.476 -1.098 -0.524 0.476 
-1.029 -0.679 0.476 -0.934 -0.816 0.476 -0.816 -0.934 0.476 -0.679 -1.029 0.476 
-0.524 -1.098 0.476 -0.357 -1.14 0.963 -0.181 -1.155 0.963 -0.0 -1.14 0.963 
0.181 -1.098 0.963 0.357 -1.029 0.963 0.524 -0.934 0.963 0.679 -0.816 0.963 
0.816 -0.679 0.476 0.934 -0.524 0.476 1.029 -0.357 0.476 1.098 -0.181 0.476 
1.14 -0.0 0.476 1.155 0.181 0.476 1.14 0.357 0.476 1.098 0.524 0.476 1.029 
0.679 0.476 0.934 0.816 0.476 0.816 0.934 0.476 0.679 1.029 0.476 0.524 1.098 
0.476 0.357 1.14 0.476 0.181 1.155 0.476 0.0 1.695 0.605 -0.22 1.644 0.605 
-0.434 1.251 0.294 -0.638 1.136 0.294 -0.826 0.993 0.294 -0.993 0.826 0.294 
-1.136 0.638 0.294 -1.251 0.434 0.294 -1.336 0.22 0.294 -1.387 0.0 0.294 -1.405 
-0.22 0.294 -1.387 -0.434 0.294 -1.336 -0.638 0.294 -1.251 -0.826 0.294 -1.136 
-0.993 0.294 -0.993 -1.136 0.294 -0.826 -1.251 0.294 -0.638 -1.336 0.782 -0.434 
-1.387 0.782 -0.22 -1.405 0.782 -0.0 -1.387 0.782 0.22 -1.336 0.782 0.434 
-1.251 0.782 0.638 -1.136 0.294 0.826 -0.993 0.294 0.993 -0.826 0.294 1.136 
-0.638 0.294 1.251 -0.434 0.294 1.336 -0.22 0.294 1.387 -0.0 0.294 1.405 0.22 
0.294 1.387 0.434 0.294 1.336 0.638 0.294 1.251 0.826 0.294 1.136 0.993 0.294 
0.993 1.136 0.294 0.826 1.251 0.294 0.638 1.644 0.605 0.434 1.695 0.605 0.22 
1.713 0.605 0.0 1.79 0.311 -0.235 1.735 0.311 -0.464 1.645 0.311 -0.681 1.522 
0.311 -0.882 1.061 0.0 -1.061 0.882 0.0 -1.214 0.681 0.0 -1.337 0.464 0.0 
-1.427 0.235 0.0 -1.482 0.0 0.0 -1.5 -0.235 0.0 -1.482 -0.464 0.0 -1.427 -0.681 
0.0 -1.337 -0.882 0.0 -1.214 -1.061 0.0 -1.061 -1.214 0.0 -0.882 -1.337 0.488 
-0.681 -1.427 0.488 -0.464 -1.482 0.488 -0.235 -1.5 0.488 -0.0 -1.482 0.488 
0.235 -1.427 0.488 0.464 -1.337 0.0 0.681 -1.214 0.0 0.882 -1.061 0.0 1.061 
-0.882 0.0 1.214 -0.681 0.0 1.337 -0.464 0.0 1.427 -0.235 0.0 1.482 -0.0 0.0 
1.5 0.235 0.0 1.482 0.464 0.0 1.427 0.681 0.0 1.337 0.882 0.0 1.214 1.061 0.0 
1.061 1.214 0.0 0.882 1.337 0.0 0.681 1.735 0.311 0.464 1.79 0.311 0.235 1.808 
0.311 0.0 1.695 0.018 -0.22 1.644 0.018 -0.434 1.56 0.018 -0.638 1.444 0.018 
-0.826 1.301 0.018 -0.993 0.826 -0.294 -1.136 0.638 -0.294 -1.251 0.434 -0.294 
-1.336 0.22 -0.294 -1.387 0.0 -0.294 -1.405 -0.22 -0.294 -1.387 -0.434 -0.294 
-1.336 -0.638 -0.294 -1.251 -0.826 -0.294 -1.136 -0.993 -0.294 -0.993 -1.136 
-0.294 -0.826 -1.251 -0.294 -0.638 -1.336 0.194 -0.434 -1.387 0.194 -0.22 
-1.405 0.194 -0.0 -1.387 -0.294 0.22 -1.336 -0.294 0.434 -1.251 -0.294 0.638 
-1.136 -0.294 0.826 -0.993 -0.294 0.993 -0.826 -0.294 1.136 -0.638 -0.294 1.251 
-0.434 -0.294 1.336 -0.22 -0.294 1.387 -0.0 -0.294 1.405 0.22 -0.294 1.387 
0.434 -0.294 1.336 0.638 -0.294 1.251 0.826 -0.294 1.136 0.993 -0.294 0.993 
1.136 -0.294 0.826 1.251 -0.294 0.638 1.336 -0.294 0.434 1.695 0.018 0.22 1.713 
0.018 0.0 1.14 -0.476 -0.181 1.098 -0.476 -0.357 1.029 -0.476 -0.524 0.934 
-0.476 -0.679 0.816 -0.476 -0.816 0.679 -0.476 -0.934 0.524 -0.476 -1.029 0.357 
-0.476 -1.098 0.181 -0.476 -1.14 0.0 -0.476 -1.155 -0.181 -0.476 -1.14 -0.357 
-0.476 -1.098 -0.524 -0.476 -1.029 -0.679 -0.476 -0.934 -0.816 -0.476 -0.816 
-0.934 -0.476 -0.679 -1.029 -0.476 -0.524 -1.098 -0.476 -0.357 -1.14 -0.476 
-0.181 -1.155 -0.476 -0.0 -1.14 -0.476 0.181 -1.098 -0.476 0.357 -1.029 -0.476 
0.524 -0.934 -0.476 0.679 -0.816 -0.476 0.816 -0.679 -0.476 0.934 -0.524 -0.476 
1.029 -0.357 -0.476 1.098 -0.181 -0.476 1.14 -0.0 -0.476 1.155 0.181 -0.476 
1.14 0.357 -0.476 1.098 0.524 -0.476 1.029 0.679 -0.476 0.934 0.816 -0.476 
0.816 0.934 -0.476 0.679 1.029 -0.476 0.524 1.098 -0.476 0.357 1.14 -0.476 
0.181 1.155 -0.476 0.0 0.835 -0.476 -0.132 0.804 -0.476 -0.261 0.753 -0.476 
-0.384 0.684 -0.476 -0.497 0.598 -0.476 -0.598 0.497 -0.476 -0.684 0.384 -0.476 
-0.753 0.261 -0.476 -0.804 0.132 -0.476 -0.835 0.0 -0.476 -0.845 -0.132 -0.476 
-0.835 -0.261 -0.476 -0.804 -0.384 -0.476 -0.753 -0.497 -0.476 -0.684 -0.598 
-0.476 -0.598 -0.684 -0.476 -0.497 -0.753 -0.476 -0.384 -0.804 -0.476 -0.261 
-0.835 -0.476 -0.132 -0.845 -0.476 -0.0 -0.835 -0.476 0.132 -0.804 -0.476 0.261 
-0.753 -0.476 0.384 -0.684 -0.476 0.497 -0.598 -0.476 0.598 -0.497 -0.476 0.684 
-0.384 -0.476 0.753 -0.261 -0.476 0.804 -0.132 -0.476 0.835 -0.0 -0.476 0.845 
0.132 -0.476 0.835 0.261 -0.476 0.804 0.384 -0.476 0.753 0.497 -0.476 0.684 
0.598 -0.476 0.598 0.684 -0.476 0.497 0.753 -0.476 0.384 0.804 -0.476 0.261 
0.835 -0.476 0.132 0.845 -0.476 0.0 0.588 -0.294 -0.093 0.566 -0.294 -0.184 
0.531 -0.294 -0.27 0.482 -0.294 -0.35 0.421 -0.294 -0.421 0.35 -0.294 -0.482 
0.27 -0.294 -0.531 0.184 -0.294 -0.566 0.093 -0.294 -0.588 0.0 -0.294 -0.595 
-0.093 -0.294 -0.588 -0.184 -0.294 -0.566 -0.27 -0.294 -0.531 -0.35 -0.294 
-0.482 -0.421 -0.294 -0.421 -0.482 -0.294 -0.35 -0.531 -0.294 -0.27 -0.566 
-0.294 -0.184 -0.588 -0.294 -0.093 -0.595 -0.294 -0.0 -0.588 -0.294 0.093 
-0.566 -0.294 0.184 -0.531 -0.294 0.27 -0.482 -0.294 0.35 -0.421 -0.294 0.421 
-0.35 -0.294 0.482 -0.27 -0.294 0.531 -0.184 -0.294 0.566 -0.093 -0.294 0.588 
-0.0 -0.294 0.595 0.093 -0.294 0.588 0.184 -0.294 0.566 0.27 -0.294 0.531 0.35 
-0.294 0.482 0.421 -0.294 0.421 0.482 -0.294 0.35 0.531 -0.294 0.27 0.566 
-0.294 0.184 0.588 -0.294 0.093 0.595 -0.294 0.0







© 2002- Malcolm Kesson. All rights reserved.