Primitive properties#

Each geometry type specifies a number of per-primitive properties. For example, the Sphere geometry has per-primitive position, radius, and color.

[1]:
import fresnel
scene = fresnel.Scene()

Setting properties when creating the geometry#

Any of the properties may be set when the geometry is created, or they may be left as default values.

[2]:
geometry = fresnel.geometry.Sphere(scene,
                                   position = [[1,0,1],
                                               [1,0,-1],
                                               [-1,0,1],
                                               [-1,0,-1]],
                                   radius=1.0,
                                   material = fresnel.material.Material(color=fresnel.color.linear([0.42,0.267,1]))
                                   # per-primitive color left default
                                   )
scene.camera = fresnel.camera.Orthographic.fit(scene)
[3]:
fresnel.preview(scene)
[3]:
../../_images/examples_00-Basic-tutorials_01-Primitive-properties_4_0.png

Changing properties after creation#

Access the per-primitive properties as if they were numpy arrays. The radius property for the Sphere geometry sets the radius of each primitive.

[4]:
geometry.radius[:] = [0.5, 0.6, 0.8, 1.0]
[5]:
fresnel.preview(scene)
[5]:
../../_images/examples_00-Basic-tutorials_01-Primitive-properties_7_0.png

The position property sets the position of each sphere in the scene’s coordinate system.

[6]:
geometry.position[:] = [[1.5,0,1],
                        [1.5,0,-1],
                        [-1.5,0,1],
                        [-1.5,0,-1]]
[7]:
fresnel.preview(scene)
[7]:
../../_images/examples_00-Basic-tutorials_01-Primitive-properties_10_0.png

The color property sets a per primitive color. The geometry material color and the primitive color are mixed with fraction primitive_color_mix. A value of 1.0 selects the primitive color, 0.0 selects the material color and values in between mix the colors.

[8]:
geometry.material.primitive_color_mix = 1.0
geometry.color[:] = fresnel.color.linear([[1,1,1], [0,0,1], [0,1,0], [1,0,0]])
[9]:
fresnel.preview(scene)
[9]:
../../_images/examples_00-Basic-tutorials_01-Primitive-properties_13_0.png
[10]:
geometry.material.primitive_color_mix = 0.5
[11]:
fresnel.preview(scene)
[11]:
../../_images/examples_00-Basic-tutorials_01-Primitive-properties_15_0.png

Reading primitive properties#

Primitive properties may be read as well as written.

[12]:
geometry.radius[:]
[12]:
array([0.5, 0.6, 0.8, 1. ], dtype=float32)
[13]:
geometry.position[:]
[13]:
array([[ 1.5,  0. ,  1. ],
       [ 1.5,  0. , -1. ],
       [-1.5,  0. ,  1. ],
       [-1.5,  0. , -1. ]], dtype=float32)
[14]:
geometry.color[:]
[14]:
array([[1., 1., 1.],
       [0., 0., 1.],
       [0., 1., 0.],
       [1., 0., 0.]], dtype=float32)

Common errors#

Primitive properties may be accessed like numpy arrays, but they may not be assigned directly.

[15]:
geometry.radius = 1.0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-15-020bd663bace> in <module>
----> 1 geometry.radius = 1.0

AttributeError: can't set attribute

This page was generated from a jupyter notebook. You can download and run the notebook locally from the fresnel-examples repository.