0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mayaでボロノイ分割する

Posted at

こちらの記事を参考に、拡張しました。
感謝します。

image.png

手順

pSphere1を分割したいとして、
boundingBoxで、範囲を取得
ロケータをたくさん設置
ボロノイ分割
ロケータを削除

MEL

global proc CutByVoronoi(string $target, float $in_pos1[], float $in_pos2[]) {
  float $v[], $p[], $r[], $norm;
  int $i;
  for($i=0; $i<3; $i++) {
    $v[$i] = $in_pos1[$i] - $in_pos2[$i];
    $p[$i] = $v[$i]/2 + $in_pos2[$i];
  }
  $norm = sqrt($v[0]*$v[0] + $v[1]*$v[1] + $v[2]*$v[2]);
  $r[0] = asind($v[1]/$norm) + 180;
  $r[1] = atan2d($v[0], $v[2]) + 180;
  $r[2] = 0.0;
  polyCut -ch false -df true -pc $p[0] $p[1] $p[2] -ro $r[0] $r[1] $r[2] $target;
  polyCloseBorder -ch false $target;
}

global proc ShatterByVoronoi(string $target) {
  string $tmp[], $dupObj, $sels[] = `ls -sl`;
  int $i=0;
  for($t1 in $sels) {
    $tmp = `duplicate -rr -name ("piece"+$i) $target`;
    $dupObj = $tmp[0];
    for($t2 in $sels) {
      if($t1 == $t2) continue;
      vector $pos1 = `xform -ws -q -t $t1`;
      vector $pos2 = `xform -ws -q -t $t2`;
      CutByVoronoi($dupObj, $pos1, $pos2);
    }
    $i++;
  }
}


{
    // バウンディングボックス取得
    $obj = "pSphere1";
    float $bbox[] = `xform -q -ws -boundingBox $obj`;
    string $locs[]; // ロケータ名リスト
    
    // 20個のロケータをランダム配置
    for ($i = 0; $i < 20; $i++) {
        float $x = rand($bbox[0],$bbox[3]);
        float $y = rand($bbox[1],$bbox[4]);
        float $z = rand($bbox[2],$bbox[5]);
        string $loc[] = `spaceLocator -p 0 0 0`;
        move -a $x $y $z $loc[0];
        $locs[$i] = $loc[0]; // ロケータ名を保存
    }
    select -r $locs;
    
    ShatterByVoronoi($obj);

    select -r $locs;
    delete;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?