Wings 3D Development Forum

Full Version: How to check the text assigned to a Variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do I check the text assigned to a Variable? This is probably simple but I can't find any info that will help me.

I decided to go ahead and add Blend modes (add,multiply,subtract,etc.) to the YafaRay plug-in before taking on the task of cleaning up my code. I made the mistake of thinking it would be simple... Sad

Texname is the variable that gets assigned a name (w_default_Bronze_1 or w_default_Bronze_2). I need to check to see if I am working with w_default_Bronze_1 or if I am working with w_default_Bronze_2. I need to know this because they have to be handled differently when exporting them for YafaRay.

Quote: UpperLayer =
case string:chr(format(Texname), $2) of
false -> "taco";
true -> "bell";
_ -> ""
end,


I have tried lots of stuff but the above was my last try. When I export the .xml file from Wings3D I get the following...

Quote:<diffuse_shader sval="w_default_Bronze_1"/>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_1"/>
<input sval="w_default_Bronze_1_mod"/>

<type sval="layer"/>
<mode ival="0"/>
</list_element>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_1_mod"/>
<texco sval="uv"/>
<mapping sval="plain"/>
<texture sval="w_default_Bronze_1"/>
<type sval="texture_mapper"/>
<bump_strength fval="0.000"/>
</list_element>

The blank line above <type sval="layer"/> is where I need to add code. I am using "taco" and "bell" for testing purposes.... Smile If the function was working I would get either "taco" or "bell" but I get neither.

Thanks,
oort
(02-08-2013, 08:22 PM)oort Wrote: [ -> ]Texname is the variable that gets assigned a name (w_default_Bronze_1 or w_default_Bronze_2). I need to check to see if I am working with w_default_Bronze_1 or if I am working with w_default_Bronze_2. I need to know this because they have to be handled differently when exporting them for YafaRay.
Probably someone could put a different code - it always possible use different ways - I'll left two that works. Smile

This one will check for fail (but, I don't think you need to check if is number, since you have formated the string before):
Code:
Tokens=string:tokens(Texname,"_"),
    Num=lists:last(Tokens),
    case string:to_integer(Num) of
        {error, _} -> io:format("Conversion error\n",[]);
        {1, _} -> io:format("Item 1 selected\n",[]);
        {2, _} -> io:format("Item 2 selected\n",[])
    end,
For Texname="w_default_Bronze_2" tokens will return ["w","default","Bronze","2"].

This can crash Wings if you can get an invalid number to Num:
Code:
Tokens=string:tokens(Texname,"_"),
    Num=lists:last(Tokens),
    case list_to_integer(Num) of
        1 -> io:format("Item 1 selected\n",[]);
        2 -> io:format("Item 2 selected\n",[]);
        N -> io:format("Item ~p selected\n",[N])
    end,
Function references - check than to understand what each one do:
Micheus,
Thanks for helping. That doesn't give me exactly what I need. I need to clarify what I need...

For w_default_Bronze_1 I need to get the following text added to the exported .xml file...

Quote:<upper_layer sval="w_default_Bronze_2"/>

and for w_default_Bronze_2 I need " " (no text) added to the exported .xml file.

Below is what I am trying to get in my .xml file, although this may not be exactly what the final code needs to be.

Thanks,
oort

Quote: <diffuse_shader sval="w_default_Bronze_1"/>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_1"/>
<input sval="w_default_Bronze_1_mod"/>
<upper_layer sval="w_default_Bronze_2"/>
<mode ival="1"/>
<type sval="layer"/>
</list_element>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_1_mod"/>
<texco sval="uv"/>
<mapping sval="plain"/>
<texture sval="w_default_Bronze_1"/>
<type sval="texture_mapper"/>
<bump_strength fval="0.0"/>
</list_element>

<bump_shader sval="w_default_Bronze_2"/>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_2"/>
<input sval="w_default_Bronze_2_mod"/>

<mode ival="0"/>
<type sval="layer"/>
</list_element>
<list_element>
<element sval="shader_node"/>
<name sval="w_default_Bronze_2_mod"/>
<texco sval="uv"/>
<mapping sval="plain"/>
<texture sval="w_default_Bronze_2"/>
<type sval="texture_mapper"/>
<bump_strength fval="5.000"/>
</list_element>

</material>
Sorry. Blush
(02-08-2013, 10:32 PM)oort Wrote: [ -> ]For w_default_Bronze_1 I need to get the following text added to the exported .xml file...
Quote:<upper_layer sval="w_default_Bronze_2"/>
and for w_default_Bronze_2 I need " " (no text) added to the exported .xml file.
Let see if I am right now... Smile

Code:
...
    Tokens=string:tokens(Texname,"_"),
    Num=lists:last(Tokens),
    UpperLayer = case list_to_integer(Num) of
        1 -> string:join(lists:subtract(Tokens, [Num]),"_") ++"_2";
        _ -> ""
    end,

Code:
...
    Tokens=string:tokens(Texname,"_"),
    Num=lists:last(Tokens),
    UpperLayer = case list_to_integer(Num) of
        1 -> lists:sublist(Texname, length(Texname)-length(Num)) ++"2";
        _ -> ""
    end,

Code:
...
    Idx=string:rchr(Texname,$_),
    Num=string:sub_string(Texname,Idx+1),
    UpperLayer = case string:to_integer(Num) of
        {1, _} -> string:sub_string(Texname, 1, length(Texname)-length(Num)) ++"2";
        _ -> ""
    end,
All these samples are considering a fixed value for the Texname's sufix - equal "1".

In case you can have other declarations it would be better try to create something more generic. By using the last example, we could create a function and use it like follow:

Code:
...
    % if at this point Texname="w_default_Bronze_1" we'll get "w_default_Bronze_2"
    UpperLayer = get_up_level(Texname),
    ...
    % if at this point Texname="w_default_Bronze_5" we'll get "w_default_Bronze_6"
    UpperLayer = get_up_level(Texname),
    ...
------------
get_up_level(Texname) ->
    Idx=string:rchr(Texname,$_),
    Num=string:sub_string(Texname,Idx+1),
    case string:to_integer(Num) of
        {N, _} when is_integer(N) -> string:sub_string(Texname, 1, length(Texname)-length(Num)) ++integer_to_list(N+1);
        _ -> ""
    end.
Use a real XML parser or write one. It should be trival. I've done it a few times and if you do it right ... to handle very basics ... you don't need much code.
Micheus,
Thanks... I ended up having to use Split instead of Tokens. I was getting my "1" and "2" but it was not recognized as an integer so "UpperLayer" was not being defined. It took me a while to figure it out but it is working now. Since Blend modes can only have two textures in YafaRay I can probably use the simpler version of your code for now. Unless further testing proves that it is needed...

Thanks again!
oort

Quote:%% Start Identify Modulator # (w_default_Bronze_1 or w_default_Bronze_2)
Split=reConfusedplit(Texname,"_",[{return, list}]),
Num=lists:last(Split),
UpperLayer =
case Num of
"1" -> "<upper_layer sval=\""++UpperLayerName++"\"/>";
_ -> ""
end,

%% End Identify Modulator #

ggaliens,
No idea what a real xml parser does but I am sure it would require skills I do not yet have... Smile Sad
(02-11-2013, 08:00 AM)oort Wrote: [ -> ]I ended up having to use Split instead of Tokens.
ggaliens had introduced me re before, but I always forget about it. Smile

About the xml management, I already thought to suggest it too, but I know how complicated can be learn something new when we are capable to get the same result using what we know. Smile

I think, that after you finish coding the plugin, you could take a look about use a xml builder in order to optimize/organize your final code - think it as a version review.

As I saw in the yafaray documentation:
Quote:Each XML document should start with naming the XML version: <?xml version="1.0"?>

it's xml 1.0 compliant, so you can use xmerl module for that.
Here you have a simple example how to generate an xml file from Erlang.

/Micheus
Micheus,
Thanks for the info on the xmerl module. I may use that to clean up my exported xml code. Yes, something to do after I finish coding the plug-in... Smile

oort
Does each variable must have a unique name, and access its value by using that name. When you choose a name for a variable, the first character must be one of the letters A through Z, !, ?, @, #, $, or _. Any remaining characters of the variable's name can be any of the preceding characters as well as 0 to 9 ?
No. Check the Erlang Programming Rules - Variable Names
Pages: 1 2