0
$\begingroup$

How to make the Material.fsxm_nobasespec boolean has inverse result.

In default the value for True is 1 and False is 0. I want it to be the inverse.

Example True = 0 and False = 1

import bpy
from bpy.types import Material
from bpy.props import IntProperty, BoolProperty, StringProperty, FloatProperty, EnumProperty, FloatVectorProperty

class FSXMaterial(bpy.types.Panel):
    """FSX Material properties Panel"""
    bl_label = "FSX/P3D Material Params"
    bl_idname = "FSXMATERIAL_PT_fsx_props"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "material"
    bl_options = {'DEFAULT_CLOSED'}

def draw_header(self,context):
    mat = context.active_object.active_material        
    layout = self.layout
    if mat:
        layout.prop(mat, "fsxm_isFSXmaterial", text = "")

def draw(self, context):
    mat = context.active_object.active_material
    layout = self.layout
    if mat:
        layout.label(text = "Enhanced Parameters")
        box = layout.box()
        box.prop(mat, 'fsxm_nobasespec')

def register():
    bpy.utils.register_module(__name__)

    Material.fsxm_nobasespec = BoolProperty(name = "Do Not Use Specular Map", default = False)

def unregister():
    bpy.utils.unregister_module(__name__)

    del Material.fsxm_nobasespec

if __name__ == "__main__":
register()
$\endgroup$
  • 2
    $\begingroup$ Question is ambiguous. Where, why do you want to reverse this property. blender properties are what users see and interact with. The rest of the code does have a place where you use it except for displaying, or do you want to perform logic on the reverse. I am asking this as True and False are internal python defined data types. These can not be altered. But perhaps you want to do something that can be easily done. Please update your question with what you want to do in the end with it. $\endgroup$ – J. Bakker Feb 5 '18 at 10:22
  • $\begingroup$ These data are used to export to .x file to be converted into .mdl file for game use. So, the template for it defined the value should be in inverse. That is why I want the data to be in inverse $\endgroup$ – Manochvarma Feb 5 '18 at 16:10
  • $\begingroup$ Consider something like Material.use_specular_map = BoolProp(default=True) . IMO it's less confusing (and more readable) than M.dont_use_specular_map = BP(default=False) $\endgroup$ – batFINGER Feb 6 '18 at 8:36
1
$\begingroup$

There are several ways how to convert a Boolean value to its 'reverse'

option 1: using not

not will reverse False into True and True into False.

spec = not mat.fsxm_nobasespec

option 2: using a translation dict

REVERSE_BOOLEAN_DICT = {
    True: 0,
    False: 1,
}
spec = RESERVER_BOOLEAN_DICT.get(mat.fsxm_nobasespec)

These options can be wrapped in a property for convenience.

@property
def reverse_basespec(self):
    return not self.fsxm_nobasespec
Material.fsxm_basespec = reverse_basespec

Looking at the code that is posted as comment below the best option is to:

Exporter.File.Write("BaseMaterialSpecular {\n") 
Exporter.File.Write(" %i; // Not Using Specular Map\n" %(not Material.fsxm_nobasespec)) 
Exporter.File.Write("}\n").

Please note that this is using python2 formatting conventions. Using python3 conventions it is best to use.

Exporter.File.Write("""BaseMaterialSpecular {{
 {fsxm_nobasespec:d}; // Not Using Specular Map
}}
""".format(fsxm_nobasespec=not Material.fsxm_nobasespec))
$\endgroup$
  • $\begingroup$ Hi J. Bakker, Thank you for the above answer. Template meaning something like this: template BaseMaterialSpecular { <E294ED4E-5C5A-4927-B19A-6A2D445FAF24> Boolean AllowBaseMaterialSpecular; } This is used to refer and translate the result from the Boolean generated in blender into readable .x file by the game engine. $\endgroup$ – Manochvarma Feb 7 '18 at 6:31
  • $\begingroup$ This is the code that export the reading from blender into .x file: Exporter.File.Write("BaseMaterialSpecular {\n") Exporter.File.Write(" %i; // Not Using Specular Map\n" %(Material.fsxm_nobasespec)) Exporter.File.Write("}\n"). So, if I followed your wrapped code above, how should I write it in the exporter code given here? $\endgroup$ – Manochvarma Feb 7 '18 at 6:49
  • $\begingroup$ Thank you very much for the codes. Both works perfectly. If I need to add some info/comments like Not Using Specular Map as in python2 conventions, how can I add that into the python3 format conventions? $\endgroup$ – Manochvarma Feb 7 '18 at 11:24
  • $\begingroup$ Hi @J. Bakker, is there any ways to add the ‘Not using specular map’ info on python3 conventions? That helps to give some info on the .x file. $\endgroup$ – Manochvarma Feb 10 '18 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.