Nautilus plugin

@Mahdiyar thanks for the interests and suggestions. I have now changed a bit the way inputs and outputs work. For the input of 3 component they accept as image

  1. A path
  2. An image
  3. A mesh (if mesh is similar in topology to a Mesh Plane

So the fun thing for example is take a Twisted Torus and apply Reaction Diffusion Extended Reaction Belousov or Noise on Mesh then send the colored mesh to a tool that transform a FilePath, an Image or a Mesh to an Image, a Mesh, The rectangle cell and Width and Height Object to Image then apply color on face then we have a tileable Image.
One problem of mesh colored is that color is on vertex. It is better to represent an image with color on face, a face is a pixel. But the color on vertex is more simple and useful for other stuffs.


  public static bool ObjectToImage(IGH_Goo goo, out Image img, out string errorMessage)
        {
            bool output = false;
            img = null;
            errorMessage = "Nothing";
      
            if (goo.CastTo<System.Drawing.Image>(out img))
            {           
                output = true;
            }
            else
            {
                Mesh mesh = null;
                if (goo.CastTo<Mesh>(out mesh))
                {
                    if (mesh.VertexColors.Count == mesh.Vertices.Count)
                    {
                                      int min = Math.Min(Math.Min(mesh.Faces[0].A, mesh.Faces[0].B), Math.Min(mesh.Faces[0].C, mesh.Faces[0].D));
                        int width = Math.Max(Math.Max(mesh.Faces[0].A, mesh.Faces[0].B), Math.Max(mesh.Faces[0].C, mesh.Faces[0].D)) - 1;

                        if ((mesh.Vertices.Count % width == 0) && min ==0)
                        {                            
                            int height = mesh.Vertices.Count / width;

                            Bitmap bitmap = new System.Drawing.Bitmap(width, height);
                            for (int i = 0; i < width; i++)
                            {
                                for (int j = 0; j < height; j++)
                                {
                                    bitmap.SetPixel(i, j, mesh.VertexColors[j * width + i]);
                                }
                            }
                            img = bitmap;
                            output = true;
                        }
                        else
                        {
                            errorMessage = "Mesh not representing an image";
                        }
                    }
                    else
                    {
                        errorMessage = "No Color in the mesh vertices";
                    }
                }
                else
                {
                    string path;
                    if (goo.CastTo<string>(out path))
                    {
                        //https://github.com/dalefugier/Vectorize/blob/main/VectorizeGh/VectorizeGhComponent.cs
                        //string path = null;

                        // Validate path string
                        if (!string.IsNullOrEmpty(path)) path = path.Trim();

                        if (string.IsNullOrEmpty(path))
                        {
                            errorMessage = "Not a good image path";
                            return false;
                        }

                        // Validate path
                        if (!File.Exists(path))
                        {
                            errorMessage = "The specified file cannot be found.";
                            //  AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "The specified file cannot be found.");
                            return false;
                        }


                        try
                        {
                            // Creates a bitmap from the specified file.
                            img = System.Drawing.Image.FromFile(path) as System.Drawing.Bitmap;
                            if (null == img)
                            {
                                errorMessage = "The specified file cannot be identifed as a supported type.";
                                return false;
                            }

                            // Verify bitmap size
                            if (0 == img.Width || 0 == img.Height)
                            {
                                errorMessage = "Error reading the specified file.";
                                return false;
                            }
                            output = true;
                        }
                        catch (System.Exception ex)
                        {
                            errorMessage = ex.Message;
                            return false;
                        }

                    }
                }
            }
            return output;
        }
1 Like