Isotropix Forums

Rubik's cube example

Clarisse Scripting related topics

Rubik's cube example

Unread postby support_team » Wed Jan 08, 2014 4:23 pm

Here is a small script example creating a rubik's cube.
This script shows how to:
- create geometries
- assign materials
- assign textures
- create an image with a 3d layer in it

Usage: Run this script in any empty context: typically project://.

It will create inside rubiks_cube and cube_map contexts
In rubiks_cube you will find the rubik's cube geometry textured.
In cube_map there's a live rendered image used as texture to generate each cube's face colors.

python code

# creates a rubik's cube
# usage run the script in an empty context
# it will create inside rubiks_cube and cube_map contexts
# In rubiks_cube you will find the rubik's cube geometry textured
# In cube_map there's a live rendered image used as texture to generate
# each cube's face colors

def create_cube(x, y, z, material):
# create a cube at the specified coordinates with the specified material

# create the cube object
obj = ix.create_object("cube", "GeometryBox")
# assign the specified material to the shading group 0
obj.get_module().assign_material(material.get_module(), 0)
# modify the cube translate attribute
obj.attrs.translate[0] = x
obj.attrs.translate[1] = y
obj.attrs.translate[2] = z

def create_face(pos, color):
# create a plane at the specified position with the specified color
face = ix.create_object('face', 'GeometryPolygrid')
face.attrs.translate[0] = pos[0]
face.attrs.translate[1] = pos[1]
face.attrs.translate[2] = pos[2]
face.attrs.scale[0] = 0.2
face.attrs.scale[2] = 0.3
face.attrs.matte_object = True
face.attrs.matte_color[0] = color[0]
face.attrs.matte_color[1] = color[1]
face.attrs.matte_color[2] = color[2]

current_ctx = ix.application.get_current_context()

rubiks_cube_ctx = ix.create_context("rubiks_cube")
ix.application.cd(rubiks_cube_ctx.get_full_name())

# create the standard material we will attach to our cubes
mat = ix.create_object("cube_material", "MaterialStandard")
cube_texture = ix.create_object("cube_texture", "TextureMap")
# assign our texture to the diffuse attribute of our material
mat.attrs.diffuse.attr.set_texture(cube_texture)

# creating our rubik's cube geometries
for z in range(3):
for y in range(3):
for x in range(3):
create_cube(x, y, z, mat)

# now let's create texture our cubes
# we will create a new render that we will use as texture

cube_map_ctx = ix.create_context("cube_map")
ix.application.cd(cube_map_ctx.get_full_name())
#creating an image
img = ix.create_object('image', 'Image')
# adding a 3d layer
layer_3d = img.get_module().add_layer('Layer3d', 'background').get_object()
# adding a camera
camera = ix.create_object('camera', 'CameraPerspectiveAdvanced')
# adding a renderer
renderer = ix.create_object('raytracer', 'RendererRaytracer')
# setting up the image to 64x64
img.attrs.resolution[0] = 64
img.attrs.resolution[1] = 64
# setting up the camera and the renderer to the layer 3d
layer_3d.attrs.active_camera = camera
layer_3d.attrs.renderer = renderer
# creating the context where we will put our assets
assets_ctx = ix.create_context(cube_map_ctx.get_name() + "/assets")
ix.application.cd(assets_ctx.get_full_name())
# here we are going to create 6 colored plane to color our rubik's cube
# creating the planes we will use as texture to our rubik's cube
# creating white face
create_face([0, 0, -0.33], [1.0, 1.0, 1.0])
# creating orange face
create_face([-0.25, 0, 0], [1.0, 0.5, 0])
# creating green face
create_face([0, 0, 0], [0, 1.0, 0])
# creating red face
create_face([0.25, 0, 0], [1.0, 0, 0])
# creating blue face
create_face([0.5, 0, 0], [0, 0, 1.0])
# creating yellow face
create_face([0, 0, 0.33], [1.0, 1.0, 0])

# set camera position, orientation and fov
camera.attrs.translate[0] = 0.12
camera.attrs.translate[1] = 28.8
camera.attrs.rotate[0] = -90.0
camera.attrs.horizontal_field_of_view = 2
camera.attrs.vertical_field_of_view = 2

# set the image to our cube texture map
cube_texture.attrs.image = img

# set back the original app context
ix.application.cd(current_ctx.get_full_name())
Isotropix
Support Team
User avatar
support_team
 
Posts: 983
Joined: Thu Jan 31, 2013 12:10 pm

Re: Rubik's cube example

Unread postby sam » Wed Jan 08, 2014 4:47 pm

Now something funny:
Select all cubes, and choose Animate > Record Pivot
Now set the Translate of your selection to -1, -1 ,-1 to center the cube.
Choose again Animate > Record Pivot

Select all cubes that are forming a face and rotate them using the rotate tool. Repeat and have fun ;)
rubiks_cube.png
Sam Assadian
Isotropix
CEO/Founder
User avatar
sam
 
Posts: 916
Joined: Sat Jan 26, 2013 12:33 am

Re: Rubik's cube example

Unread postby matthiasBuehler » Fri Jan 10, 2014 3:46 pm

Very nice !

Cool ! I've seen Rubik's Cubes in other python tutorials, nice one !

Thanks for sharing some Python insight !

Matt
matthiasBuehler
 
Posts: 55
Joined: Mon Nov 18, 2013 9:20 pm


Return to Scripting