HoudiniVex笔记_P8_Conditional条件

原视频:https://www.youtube.com/playlist?list=PLzRzqTjuGIDhiXsP0hN3qBxAZ6lkVfGDI
Bili:Houdini最强VEX算法教程 - VEX for Algorithmic Design_哔哩哔哩_bilibili

Houdini版本:19.5

1、if-else条件语句

1)、if-else基本用法如下所示

if(chf('value') < 0.5){
    s@text = 'Less than 0.5';
}else{
    s@text = 'More than 0.5';
}

2)、if-else if-else 基本用法如下所示 

if(chf('value') < 0.25){
    s@text = 'Less than 0.25';
}else if(chf('value') < 0.75){
    s@text = 'More than 0.25 and less than 0.75';
}
//可继续添加多个 else if() 条件判断语句
else{
    s@text = 'More than 0.75';
}

2、单行if条件语句

if(chf('value') < 0.5) s@text = 'Less than 0.25';    //if判断条件为True,则运行后面代码
else s@text = 'More than 0.75';                      if判断条件为False,则运行else代码

3、三元运算符

s@text = chf('value') < 0.5 ? 'Less than 0.5' :'More than 0.5';
//             判断条件      ?   条件为True    : 条件为False

4、基本比较运算符

基本比较运算符有【小于<、大于>、等于==、不等于!=、小于等于<=、大于等于>=】,用法如下

float value = chf('value');
string text = '';

if(value < 0.5){
    text += 'Value is less than 0.5\n';
}if(value <= 0.5){
    text += 'Value is less than or eaual 0.5\n';
}if(value > 0.5){
    text += 'Value is more than o0.5\n';
}if(value >= 0.5){
    text += 'Value is more than or eaual 0.5\n';
}if(value == 0.5){ 
    text += 'Value is eaual 0.5\n';
}if(value != 0.5){
    text += 'Value is not eaual 0.5\n';
}

s@text = text;

满足多个条件 ,则显示多个结果。

5、字符串匹配运算

匹配单个字符、*号匹配多个字符、[Cc]表示可匹配大写C或者小写c、^排除符号

string text = chs('text');
string outtext = '';

//  ~=  ?   ?号匹配单个字符
if(text ~= 'AB?D'){
    outtext += 'Condition 1 matched.\n';
}

//  ~=  *   *号匹配多个字符
if(text ~= 'AB*D'){
    outtext += 'Condition 2 matched.\n';
}

//  ~=  []   [Cc]表示可匹配大写C或者小写c
if(text ~= 'AB[Cc]D'){
    outtext += 'Condition 3 matched.\n';
}

//  ~= (string, 排除条件)]   匹配的结果排除包大写字母C或小写字母c,即排除ABCD、ABCD
if(text ~= 'AB*D, ^AB[Cc]D'){
    outtext += 'Condition 4 matched.\n';
}

s@text = outtext;

6、逻辑运算符

&& 与/同时/而且/And、|| 或/或者/or

float value = chf('value');
string text = '';

if(value > 0.5 && value < 0.75){    //  0.5<x<0.75
    text += 'Value is more than 0.25 and less than 0.75\n';
}

if(value < 0.5 || value > 0.75){    //  0.5<x 或者 0.5<x<0.75
    text += 'Value is less than 0.25 or more than 0.75\n';
}

//  x<0.2 或者 0.8<x     或者   0.4<x<0.6
if((value < 0.2 || value >0.8) || (value > 0.4 && value < 0.6)){
    text += 'Value is less than 0.2 or inbetween 0.4 to 0.6 or more than 0.8\n';
}

s@text = text;

7、使用Return结束进程

使用return后,结束进程,后面代码将被忽略

float value = chf('value');
string text = '';

if(value < 0.5){
    s@text += 'Return.';
    return;
    //运行return后,结束进程,下面代码不再运行
}

s@text = 'Not return';

8、使用Break结束循环

float value = chf('value'); //值范围0~1
string text = '';

for( int i=0; i<10; i++){
    text += itoa(i) + '\n';
    
    if(i >= value*10){
        break;
        //结束循环
    }
}
    
s@text += text;

9、使用Continue结束(循环中的)一次迭代

在一个循环里面,会执行n次代码,每执行一次称为一次迭代

float value = chf('value'); //值范围0~1
float value2 = chf('value2');
string text = '';

for( int i=0; i<10; i++){    
    if(i >= value*10 && i<(value+value2)*10){
        continue;
        //符合条件,下面代码不执行,直接下一次迭代)
    }
    
     text += itoa(i) + '\n';
}
    
s@text += text;

10、练习—条件与属性

利用全局变量的时间属性对小球进行移动和缩放。
创建类型为detail的attributewrangle节点(point),写入下面代码:

vector pos = set(@Time, 0, 0);  //点随时间移动
int pt = addpoint(0, pos);

float scale = 1.0;
if(pos.x % 2.0 > 1.0){  //  %   求余数运算
    scale = 2.0;
}

setpointattrib(0, 'pscale', pt, scale); //缩放

 添加copytopoints节点,用小球Sphere代替点,结果为:

【小球随时间移动,0~3帧大小不变,3~4帧变大2倍,4~5大小不变,5~6帧变大2倍,……】

11、练习—条件与组Group

创建节点:Sphere、Scatter、Group(命名为g1)、Group(g2)、Group(g3)、类型为Points的AttributeWrangle(move),并依次连接;
其中 g1 / g2 / g3 的BaseGroup分别设置为@P.x>0 / @P.y>0 / @P.y>0;

在move节点写入以下代码:

if(inpointgroup(0, 'g1', @ptnum) == 1){
    @P.x += @Time;
}

if(inpointgroup(0, 'g2', @ptnum) == 1){
    @P.y += @Time;
}

if(inpointgroup(0, 'g3', @ptnum) == 1){
    @P.z += @Time;
}

运行代码,结果为环绕小球生成的粒子分成8块,随时间增加而向外扩散

 11、练习—条件与数组Array

创建类型为detail的attributewrangle节点(random_points),写入下面代码:

int pts[] = array();

//创建1000个点
for(int i=0; i<1000; i++){
    vector pos = rand(i*3.243); //rand(seed)  根随机返回0~1范围内的值
    int pt = addpoint(0, pos);
    push(pts, pt);
}

float threshold = chf('threshold'); //浮点类型,范围0~1

for(int i=0; i<len(pts); i++){
    int pt = pts[i];            //遍历访问创建的1000个点
    
    float val = rand(i*5.12);   //随机返回0~1范围内的值
    
    //threshold相当于一个阈值/开关,对点进行着色/缩放
    if(val < threshold){  
         //threshold=0.1,有10%的点执行以下代码
         //threshold=0.3,有30%的点执行以下代码
        setpointattrib(0, 'pscale', pt, 0.04);       // 缩放0.04
        setpointattrib(0, 'Cd', pt, set(1, 0, 0));    //设置为红色
    }else{
        setpointattrib(0, 'pscale', pt, 0.01);
        setpointattrib(0, 'Cd', pt, set(1, 1, 0));
    }
}

结果为:threshold相当于一个阈值/开关,对点进行着色/缩放 。例如/threshold=0.1,有10%的点 缩放0.04且设置为红色;threshold=0.3,有30%的点 缩放0.04且设置为红色;……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值