Sharing is Good for Everyone :)

2312
9
03-16-2012 07:37 AM
by Anonymous User
Not applicable
Original User: thyandrecardoso

Hey, I thought I might share some things here, in the sense that it might be useful to someone.

I've coded some functions to retrieve R, G and B values, given Hue Saturation and Lightness:
```hue2R(H, S, L)  =
case S == 0.0:
L
else:
hue2RGB( temp1(L, S), temp2(L, S), H + truncTo1000( 1.0/3.0 ) )
hue2G(H, S, L)  =
case S == 0.0:
L
else:
hue2RGB( temp1(L, S), temp2(L, S), H)

hue2B(H, S, L)  =
case S == 0.0:
L
else:
hue2RGB( temp1(L, S), temp2(L, S), H - truncTo1000( 1.0 / 3.0 ) )

hue2RGB(v1, v2, vH ) =              //Function Hue_2_RGB
case vH < 0.0:
hue2RGB(v1, v2, truncTo1000(vH + 1.0))

case vH > 1.0:
hue2RGB(v1, v2, truncTo1000(vH - 1.0))

else:
case truncTo1000(6.0 * vH) < 1.0:
( truncTo1000(v1 + ( v2 - v1 )) * truncTo1000(6.0 * vH)  )

case truncTo1000(2.0 * vH) < 1.0:
v2

case truncTo1000(3.0 * vH) < 2.0:
( (v1 + ( v2 - v1 )) * truncTo1000( ( 2.0 / 3.0 ) - vH) * 6.0 )

else:
0

temp2(l, sat) =
case l < 0.5:
l * (1+sat)
else:
(l+sat) - (l*sat)
temp1(l, sat) =
(2.0 * l) - temp2(l, sat)

truncTo1000(val) =
truncTo(val, 1000)

truncTo100(val) =
truncTo(val, 100)

truncTo(val, order) =
((rint(val * order)) / order)
```

Which allows one to do things like the ones in the pictures attached!

################################################

On the python side, some really small snippets that I personally find very usefull (please don't mind possible lack of naming conventions or whatever... I'm not a python programmer):
```def getAttrFilterFunction(ce_instance, attr, value):
return lambda x: ce_instance.getAttribute(x, attr) == value
def getAttrContainingFilter(ce_instance, attr, value):
return lambda x: value in ce_instance.getAttribute(x, attr)
def getAttrExistsFilter(ce_instance, attr):
return lambda x: attr in ce_instance.getAttributeList(x)
# determines if shape is sidewalk
def getSidewalkFilter(ce_instance):
return lambda x: ce_instance.getUUID(x).__str__().endswith(":1") or ce_instance.getUUID(x).__str__().endswith(":2")
# determines if shape is street
def getStreetFilter(ce_instance):
return lambda x: ce_instance.getUUID(x).__str__().endswith(":0")
```

For example, with these you can use the python "filter" function:
```allShapes = ce.selection()

myStreetsFilter = getStreetFilter(ce)

onlyStreets = filter(myStreetsFilter, allShapes)
```

Hey, please share other useful snippets! And please warn me about bugs in the things I've posted 🙂

Cheers!
André
Tags (2)
9 Replies
New Contributor III
On the archived forums of the former Procedural, there was already some things concerning colour definitions.

I went to http://www.imagemagick.org/script/color.php and copied all the colour names and hex values to a CGA rule file. Normally, on each project I include that file ("misc.cga" -- actually, I symlink it) and use it for various purposes.

Having named colours, helps a lot when you are coding a building facade or whatever. I go to the site, choose the colour I want, and just copy its name:

```import misc: "misc.cga"

Lot -->
extrude(10)
color(misc.RosyBrown1)
Mass.
```

The color definitions are on the attached file.

Hope it helps!
by Anonymous User
Not applicable
Original User: thyandrecardoso

The CE documentation talks about terrain layer attributes and functions... Here is an example of a spherical terrain:
```attr SCALE = 1000
attr R = 500

attr elevation =
case sqrt( ( pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)) ) > R:
0.0
else:
case sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2))) < 0:
0
else:
sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)))
```
New Contributor III
The CE documentation talks about terrain layer attributes and functions... Here is an example of a spherical terrain:
```attr SCALE = 1000
attr R = 500

attr elevation =
case sqrt( ( pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)) ) > R:
0.0
else:
case sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2))) < 0:
0
else:
sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)))
```

Don't know if anyone noticed, but there's a bug 😛

To get the half  sphere, we only need this:
```attr SCALE = 1000
attr R = 500

attr elevation =
case sqrt( ( pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)) ) > R:
0.0
else:
sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)))
```

[ATTACH=CONFIG]13095[/ATTACH]
I think sqrt is always > 0... lol

The erased part might be useful if you want just the top of a sphere, to make a slightly rounded terrain. For example:
[ATTACH=CONFIG]13094[/ATTACH]
```attr SCALE = 1000
attr R = 1500

attr elevation =
case sqrt( ( pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2)) ) > R:
0.0
else:
case sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2))) - 1430 < 0:
0
else:
(sqrt(R*R - (pow((u-0.5)*SCALE, 2) + pow((v-0.5)*SCALE, 2))) - 1430) + elevationDelta
```
by Anonymous User
Not applicable
Original User: thyandrecardoso

Revisiting the filters I posted previously, I just had the epiphany (rather late though lol ) that you can be less verbose than what I proposed earlier.
For example, with:
```def getAttrFilterFunction(ce_instance, attr, value):
return lambda x: ce_instance.getAttribute(x, attr) == value
```

You can do:
```>>> myFilter = getAttrFilterFunction(ce, "customAttr", "fantasticShape")
>>> fantasticShapes = ce.getObjectsFrom(ce.scene, myFilter)
```
New Contributor III
Hey, bellow are the rules to build a simple stairway from a rectangular base.
It is very simple, and the results are nothing spectacular, but it might be interesting for some one out there 🙂

The pictures show how we can parametrize the function call, and control the model through the inspector...

[ATTACH=CONFIG]15682[/ATTACH]
[ATTACH=CONFIG]15683[/ATTACH]
[ATTACH=CONFIG]15684[/ATTACH]
[ATTACH=CONFIG]15685[/ATTACH]
[ATTACH=CONFIG]15686[/ATTACH]

```const stairStepUnitRise = 0.17
const stairStepUnitRun = 0.28
@Hidden
attr currentScopeSx = 0

## dummy, calling rule bellow with some defaults
## the level has, by default, 45% of the lenght of the shape in x
StairwayWithLevel(topColor, frontColor, levelColor) -->
StairwayWithLevel(topColor, frontColor, levelColor, 0, 0, 0)

StairwayWithLevel(topColor, frontColor, levelColor, levelDim, centerDim, displacement) -->
set(currentScopeSx, scope.sx)
## first, divide into level and two stair flight of stairs
split(x){
(0.45 * scope.sx) + levelDim:
StairwayLevel(levelColor, currentScopeSx*0.45+ levelDim, currentScopeSx) |
~1:
split(z){
~1 + displacement:
t(0, ((rint((currentScopeSx-(currentScopeSx*0.45+ levelDim)) / stairStepUnitRun)) - 1)*stairStepUnitRise, 0)
StairFlight(topColor, frontColor) |
centerDim:
NIL|
~1:
rotateScope(0, 180, 0)
StairFlight(topColor, frontColor)
}
}

StairwayLevel(levelColor, length, total) -->
t(0, ((rint((total-length) / stairStepUnitRun)) - 1)*stairStepUnitRise, 0)
s('1, stairStepUnitRise, '1)
i("builtin:cube")
comp(f){
top:
color(levelColor)
set(material.specular.r, 0)
set(material.specular.g, 0)
set(material.specular.b, 0)
set(material.shininess, 0)
set(material.opacity, 1)
SetMaterial("stairwaylevel")|
all:
SetMaterial("stairwaylevel")

}

StairFlight(topColor, frontColor) -->
split(x){
~stairStepUnitRun:
t(0, (split.index)*stairStepUnitRise, 0)
StairwayStep(topColor, frontColor)
|
{
stairStepUnitRun:
t(0, (split.index)*stairStepUnitRise, 0)
StairwayStep(topColor, frontColor)
}*
}

StairwayStep(topColor, frontColor) -->
s('1, stairStepUnitRise, '1)
i("builtin:cube")
comp(f){
top:
color(topColor)
set(material.specular.r, 0)
set(material.specular.g, 0)
set(material.specular.b, 0)
set(material.shininess, 0)
set(material.opacity, 1)
SetMaterial("stairwaystep")|
all:
color(frontColor)
set(material.specular.r, 0)
set(material.specular.g, 0)
set(material.specular.b, 0)
set(material.shininess, 0)
set(material.opacity, 1)
SetMaterial("stairwaystep")

}

SetMaterial(name) -->
set(material.name, name)
TerminalMaterial.
```
by Anonymous User
Not applicable
Original User: thyandrecardoso

Hi,

In order to get some quick and dirty animations from some of my projects I've written some python code to do (part of) the things stated in this post's title. Namely, rotating the camera around a previously selected object, framing a selected object, changing the values of shapes attrs, regenerating the shape's model and taking a snapshot.

This code is attached, and I've done a small (and ugly) video of this working, using the "Candler" building, from the tutorial 09:

Beware, everything inside is crappy code 🙂
So, I would be very grateful if someone decided to enhance and correct the code inside, and share it back. Mainly the functionality for "iterateThroughAttrValues" function, which has many problems.
by Anonymous User
Not applicable
Original User: matthiasbuehler

Hey Andre !

Cool work !

Why's the model changing to black each second step ?

Cheers !

Matt
New Contributor III
On the script entry, one of the attr definitions says:
```'/ce/rule/wallC': {
0: 0,
1: ["#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF", "#000000", "#FFFFFF"]
}
```

The code is just going through that list of colors (white, black, white, black), for the "wallC" attr...
There were more interesting things to do, but I just wanted to share the code and I don't have much time to do these things :rolleyes:

Ultimately, what I've shared may not have direct utilization by others on this community, but it might spark some ideas... hope it does 🙂

Hey Andre !

Cool work !

Why's the model changing to black each second step ?

Cheers !

Matt
by Anonymous User
Not applicable
Original User: matthiasbuehler

I see.

No, that's cool. As long it's designed.. 😉

Thanks for sharing !!

matt