0

I am trying to make random 3d blob objects using pyvista. First, I wrote a code as following ,

import os
import sys
import numpy as np
import cv2
import pyvista as pv
import trimesh as tm
import math
import random as rnd

smooth_iter = rnd.randint(50,200)
num_blob = rnd.randint(5,20)
xyz_list = []
rad_list=[]
rad_list2=[]
p = pv.Plotter(shape=(1,1))
g = pv.PolyData()
pi = 3.141592
for i in range(num_blob):
    x= rnd.uniform(-1.5,1.5)
    y= rnd.uniform(-1.5,1.5)
    z= rnd.uniform(-1.5,1.5)
    r1= rnd.uniform(0.3,2.5)
    r2= rnd.uniform(r1-0.2,r1+0.2)
    r3= rnd.uniform(r1-0.2,r1+0.2)
    th= rnd.uniform(-pi,pi)
    ps= rnd.uniform(-pi,pi)
    py= rnd.uniform(-pi,pi)
    xyz_list.append((x,y,z))
    rad_list.append((r1,r2,r3))
    ell = pv.PolyData(pv.ParametricEllipsoid(r1, r2, r3))
    ell.translate([x,y,z])
    ell.rotate_x(th)
    ell.rotate_y(ps)
    ell.rotate_z(py)
    p.add_mesh(ell)
print(xyz_list)
print(rad_list)
p.show()  

This code showed me the plotted result of overlapped random ellipsoids. But since the resulting p is pv.Plotter, not pv.Polydata, so I could not save/smoothing/edit the result. And I could not yet find what to do to generate custom edittable 3d object in pv.Polydata. Which function should I use or is there any other way to achieve my goal, such as using trimesh?

EJ Song
  • 123
  • 2
  • 13
  • As a superficial solution you can use `g = pv.UnstructuredGrid` and in the loop do `g.merge(ell, inplace=True)`, which will collect your ellipsoids in a single grid `g` which you can add to the plotter after the loop. However this won't give you a nice surface, just overlapping ellipsoids which probably isn't what you're looking for. I don't know how to turn this into a nice closed surface mesh with vtk, so I'm only leaving this comment. – Andras Deak -- Слава Україні Apr 05 '21 at 14:11
  • @AndrasDeak Thanks. I will try yours and further try – EJ Song Apr 06 '21 at 06:03

0 Answers0