Flat mirror (my first useful script)


(AkhIL) #1

to run this script you must have cgkit-1.2.0 or newer.


import Blender
from Blender.Mathutils import Matrix as BMatrix
import cgtypes

class object:
		def __init__ (self,OB=None):
			if type(OB) == type(""):
				OB=Blender.Object.Get(OB)
		
			if `type (OB)` == "<type 'Blender Object'>":
				self.ob=OB
				mtx=[]
				for r in range(4):
					for c in range(4):
						mtx.append(self.ob.matrix[c][r])
				self.matrix=cgtypes.mat4(mtx)
			else:
				self.matrix=cgtypes.mat4
				self.ob=Null
		def applyMatrix(self):
			mtx=[]
			for c in range(4):
				m=[]
				for r in range(4):
					m.append(self.matrix[c][r])
				mtx.append(m)

			mtx=BMatrix(mtx[0],mtx[1],mtx[2],mtx[3])
			self.ob.setMatrix(mtx)

flipZ=cgtypes.mat4(	1,0, 0,0,
					0,1,0,0,
					0,0,-1,0,
					0,0, 0,1)

cam=object('Camera') #camera to mirror
mir=object('Plane') #mirror plane. Can be every object. Script use local Z-axis
mcam=object('CameraM') #mirrored camera. Use linked (Alt-D) copy of camera

mcam.matrix=mir.matrix*flipZ*mir.matrix.inverse()*cam.matrix
mcam.applyMatrix()

How it can be used:

  1. you can first render image from mirrored camera without mirror plane and then map rendered image as “screen” to mirror plane.
  2. you can make plane transparent and then use sequencer to compose image from camera over image from mirrored camera

In both cases use linked copy of the scene to make new mirrored scene.


(MagicMan) #2

So if I understand your description properly, this could be an effective alternative method to raymirror on a flat surface? Unfortunatly I dont have cgkit so I guess I cant run it.


(AkhIL) #3

I hate raytrace because it’s slow. More then with “fake” methods you have more control. For example red object can have blue reflection.

This is test render. Mirror is just transparent plane. Two scenes combined in sequencer.

http://akhil.nightmail.ru/tmp/flatMirror.jpg