v 1.1
Module Python pour C4D R13.
Matière paramétrable via un champ de code Python.
Les trois variables r, v, b sont à définir. Elles représentent la couleur du pixel calculé (en nombre flottant).
Le code personnalisé s'incruste dans la fonction Output() de la classe c4d.plugins.ShaderData.
Les variables sh (BaseShader) et cd (ChannelData) peuvent donc être utilisées dans votre code.
D'autres variables sont disponibles, tels que :
x, y, z : renvoient les coordonnées du point calculé.
t : renvoie la valeur du champ Temps de l'interface.
-------
Quelques exemples de code :
Couleur
Code PHP:
r = 1
v = 0.5
b = 0
Normales
Code PHP:
if cd.vd:
n = cd.vd.n
r = n.x
v = n.y
b = n.z
Coordonnées textures
Code PHP:
n = cd.p
r = n.x
v = n.y
b = n.z
Bruit modifié
Code PHP:
n = Noise(Vector(x, y, z), (t+cos(x)+cos(y)+cos(z))*10)
r = v = b = n
Formule
Code PHP:
m = cos(x + cos(y) + cos(x) + cos(z))
r = v = b = abs(0-(m*3))
-------
Code PHP:
import os
import math
import c4d
from c4d import plugins, Vector
from c4d.utils.noise import Noise
from math import *
MODULE_ID = 1028230
class MatFormule(plugins.ShaderData):
tps = 0
ech = 0.1
echxyz = Vector(1, 1, 1)
contra = 0
lumi = 0
cola = Vector(0, 0, 0)
colb = Vector(1, 1, 1)
col_ = True
con_ = True
colc = Vector(1, 0, 0)
codetxt = 'n = Noise(Vector(x, y, z), (t+cos(x)+cos(y)+cos(z))*10)\nr = v = b = n'
code = compile(codetxt, '<string>', 'exec')
mode = 1
min = True
max = False
prof = 2
def Init(self, node):
donnees = node.GetDataInstance()
donnees.SetReal(1000, self.tps)
donnees.SetReal(1001, self.ech)
donnees.SetVector(1003, self.echxyz)
donnees.SetReal(1002, self.contra)
donnees.SetReal(1006, self.lumi)
donnees.SetVector(1004, self.cola)
donnees.SetVector(1005, self.colb)
donnees.SetString(1007, self.codetxt)
donnees.SetVector(1008, self.colc)
donnees.SetLong(1010, self.mode)
donnees.SetBool(1011, self.min)
donnees.SetBool(1012, self.max)
donnees.SetLong(1014, self.prof)
return True
def recup(self, sh):
donnees = sh.GetDataInstance()
self.tps = donnees.GetReal(1000)
self.ech = donnees.GetReal(1001)
self.echxyz = donnees.GetVector(1003)
self.contra = donnees.GetReal(1002)
self.lumi = donnees.GetReal(1006)
if self.contra == 0 and self.lumi == 0:
self.con_ = False
else: self.con_ = True
self.cola = donnees.GetVector(1004)
self.colb = donnees.GetVector(1005)
if self.colb == Vector(1, 1, 1) and self.cola == Vector(0, 0, 0):
self.col_ = False
else: self.col_ = True
txt = donnees.GetString(1007)
self.colc = donnees.GetVector(1008)
self.SetExceptionColor(self.colc)
self.codetxt = txt
self.code = compile(txt, '<string>', 'exec')
self.mode = donnees.GetLong(1010)
self.min = donnees.GetBool(1011)
self.max = donnees.GetBool(1012)
self.prof = donnees.GetLong(1014)
def InitRender(self, sh, irs):
self.recup(sh)
return 0
def Output(self, sh, cd):
if cd.vd and self.mode is 1: # 3d
vec = cd.vd.p * self.ech
else: # 2d et aperçu
if self.mode is 0:
vec = (cd.p) * 100 * self.ech # Coordonnées UVW
else:
vec = (cd.p-0.5) * 100 * self.ech # Coordonnées UVW
x = vec.x * self.echxyz.x
y = vec.y * self.echxyz.y
z = vec.z * self.echxyz.z
t = self.tps
r = v = b = 0
exec self.code
vec = Vector(r, v, b)
if self.col_ is True:
dif = self.colb - self.cola
vec.x = dif.x * vec.x + self.cola.x
vec.y = dif.y * vec.y + self.cola.y
vec.z = dif.z * vec.z + self.cola.z
if self.con_ is True:
vec = ((vec-0.5) * (self.contra + 1)) + 0.5 + self.lumi
if self.min is True:
if vec.x < 0: vec.x = 0
if vec.y < 0: vec.y = 0
if vec.z < 0: vec.z = 0
if self.max is True:
if vec.x > 1: vec.x = 1
if vec.y > 1: vec.y = 1
if vec.z > 1: vec.z = 1
vec = c4d.utils.TransformColor(vec, self.prof)
return vec
def FreeRender(self, sh): # Libère la mémoire
return
if __name__=='__main__':
plugins.RegisterShaderPlugin(MODULE_ID, "Formule", 0, MatFormule, "matformule", 0)
-------
Lien alternatif