How to unwrap Spherical surface function

2 views (last 30 days)
apops
apops on 10 May 2022
Answered: VINAYAK LUHA on 27 Sep 2023
Hi,
I would like to unwrap a plotted sphere into a 2D image so it can be used as texture or import the 3D figure with colors to use in blender (colormap is important) I know matlab functions can be expoerted into stl or x3d files but when I open it in blender it is just a sphere without colors. Any help will be appreciated

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 27 Sep 2023
Hi Apops,
I understand that you have plotted a sphere in MATLAB, and you want to export it along with its colormap and use it in Blender. Here is a workaround involving exporting the sphere in “.obj” format, which unlike “.stl” format, also supports color information:
  1. Download the Wavefront OBJ Toolbox from MATLAB File exchange using the following link- https://in.mathworks.com/matlabcentral/fileexchange/27982-wavefront-obj-toolbox .
  2. Extract the “write_wobj.m” file in your current path.
  3. Save the “gcf” of your figure as a “.png” file to be used later for applying texture from image in blender.
  4. Create the “.obj” for the sphere plot as per the example given in the toolbox.
  5. Import the “.obj” file in blender.
  6. Select the object and create a new material for the object.
  7. Define a new material for the object and under “base color” select “image texture”.
You can now see a preview of the object with the colormap applied on it in the preview window.
Here is the code for generating “.obj” file and the “image texture” file your reference:
[x, y, z] = sphere(100);
c = hypot(x, y);
surf(x, y, z, c, 'FaceColor', 'interp', 'EdgeColor', 'none');
colormap jet;
colorbar;
OBJ.vertices = [x(:), y(:), z(:)];
OBJ.material = struct('type', 'newmtl', 'data', 'sphere_material');
OBJ.objects(1).type = 'g';
OBJ.objects(1).data = 'sphere_object';
OBJ.objects(2).type = 'usemtl';
OBJ.objects(2).data = 'sphere_material';
OBJ.objects(3).type = 'f';
OBJ.objects(3).data.vertices = surf2patch(x, y, z).faces;
filename = 'sphere.obj';
write_wobj(OBJ, filename);
filename = 'sphere_texture.png';
saveas(gcf, filename);
Explore the documentation of the following functions for more details:
Hope this helps.
Regards,
Vinayak Luha

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!