Texture mapping based on dpi or texture width.

I have an issue with texture mapping.
I got a flat plane that needs to be textured. it should in real world be 2x3m but the issue is i need to texture it with a whole lot of different textures of different size(batch job).

The problem is that it wants to stretch the textures to fit the whole plane which means it does not follow the height/width ratio of the texture and becomes distorted.

The things i know is the dpi of the texture should be constant and the with is almost certainly 2m wide.
is there any way to make it tile correctly when i change texture?
either based on that 1px=xmm or texture width = plane width.

the correct way is to crop the texture to a 1x1 size.

as for your problem, no, i could not find a solution. at least not without any addons.
one suggestion is trying to add an Empty and select Image, see if that can give you the result you want.

beside that, i know you can import images as plane with an addon, but you have to look that up aswell as i dont use that addon and dont know which one it is.

Thank you, guess i might have to look at different add-ons. i’ve never really touched any.
One idea i’ve had is that i might be able to check the height/width ratio with a script outside of blender.

Is there any way to modify one of the scale variables in the mapping node from command line?

probably, but tell me the scenario. why do you want this? i can think of a thousand different ways to solve this problem but it completely depends on what the scenario is…

I have a floor plane (a subdivided mesh) that needs to get textures including displacement and normal maps.
I have also created a php page/script to upload the texture,displacement and normal map to a set folder and also execute a command line script which renders a blend file. It shouldn’t be too hard to get the php script to check the texture proportions and output it to a file or something if it would be of help.

The blend file is configured to take the uploaded files (the files are always uploaded with the same name) and render it. Once rendered it’s saved and viewable from the php page as an end result.

The textures etc are generated from a weaving software and gets the size of the pattern,which is 2m wide and variable length.
So everything is fine and dandy as long as the pattern is about 2x2m.

I do also have blend files where this plane is composed of 0.5x0.5m tiles and using the same textures, but once i got a solution for the 1piece plane applying the same solution to the tiles should not be a problem i guess.

I don’t know if there’s a better way to do this… But you can make a script to look for the dimensions of each texture and calculate the correct scales (making a list for look up later).

OSL has a gettextureinfo() function that let’s you retrieve texture information… I don’t know if it retrieves DPI information but in the OSL specifications is stated that it can retrieve values from the header and metadata of the texture file. With that info, it shouldn’t be hard to make appropriate changes in the coordinate system for each texture.

Thank you! That definitely sounds like something to work with. I am truly a beginner in blender and have not touched OSL (had to google blender and OSL :stuck_out_tongue: )

I think the dimensions to get the height/width ratio is all what i actually need and then change the mapping y-scale.
If someone could help me with the script it’s greatly appreciated, anyway i will start looking on how to code and implement things trough blender OSL.

this is just a test thing… you may need to add some more logic to it:

shader FixTextureUV(string image="", vector Vector=P, output color Color=0.0)
{
    int res[2];
    gettextureinfo(image, "resolution", res);

    float yFac=(3*float(res[1]))/(2*float(res[0]));

    Color=texture(image, Vector[0], Vector[1]*yFac);
}

ps… I noticed this is very slow. I have to make some tests to check if it’s a bug, or a gettextureinfo() problem.

Thank you! will look into this!

I solved it!.. Kind of.
It works but OSL gives a HUGE performance hit. Probably because of the texture size.

What usually takes about 7-8min to render now takes more than 2 hours to render. It takes 15-20min before the 1st tile starts to actually render (even though it says it renders but the cpu usage is only consuming one thread)

Here’s how i solved it, i think it might be of interest to someone who does not use as huge files as i do:

shader FixTextureVector(string image="", vector Vector=P, output point Vector2=0.0){
    int res[2];
    gettextureinfo(image, "resolution", res);


    float yFac=(float(res[0]))/(float(res[1]));
    Vector2 = point(Vector[0],Vector[1]*yFac,Vector[2]);


}



Is there any way to solve it without OSL?

you can build this same script with nodes… but you need to input the values of your texture dimensions by hand, or create a python script to do it.

Fixed it!
My php page generates a python script with the correct scaling for each image.

This is the simple python script:

import bpy
import math


# get the material
mat = bpy.data.materials['Tile1']


# get all material nodes
nodes = mat.node_tree.nodes


# get the mapping node
map_node = nodes.get("Mapping")


# set the rotation z component
map_node.scale[1] = #Scaling-Goes-Here#

Thanks for all help!