Material attribute copy script

Post any tutorial and tools you might find it useful for other creators.
User avatar
davidzoli
Posts: 3226
Joined: 22 Jun 2014 10:08
Location: Budapest, Hungary
Contact:

Material attribute copy script

#1 Post by davidzoli » 20 Sep 2015 19:44

I've written a Blender script to make material conversion easier. If you have an imported (pmd) model, you don't have the right material attributes which needed by the Blender Tools. You have to set the attributes according their "old" values.

Image
Looks familiar?

I couldn't find a shorter way, but set them manually. One by one.
At this point you can use my script.

Download here: https://github.com/davidzoli/SCSBT-helper
A short video about its functions
https://www.youtube.com/watch?v=xNQrXoJL8G4



Below the line there is the original first post. Kind of outdated things, but I leave it about the other posts.
-------------------------------------------------------------------------------------------------

Code: Select all

floats = ["add_ambient", "reflection", "reflection2", "shadow_bias", "shininess", "tint_opacity"]
colors = ["diffuse", "env_factor", "specular"]
sets   = ["fresnel", "tint"]

for key in bpy.context.active_object.active_material["scs_mat_options"].keys():
    newval = ''
    _key = key
    str(key+" found")
    # variable is a float
    if (_key in floats):
        newval = float(bpy.context.active_object.active_material["scs_mat_options"][_key])
    # variable is a color
    if (_key in colors):
        newval = Color(map(float, bpy.context.active_object.active_material["scs_mat_options"][_key].strip(")").lstrip("(").split(', ')))
    # variable is a set of floats
    if (_key in sets) or (_key.find("aux") >= 0):
        if(_key.find("[") >= 0):
            _key = _key.replace("[","").replace("]","")
        newval = list(map(float, bpy.context.active_object.active_material["scs_mat_options"][key].strip(")").lstrip("(").split(', ')))
    # set the attributes
    if (newval!=''):
        str("shader_attribute_"+_key+" set to: "+str(newval))
        setattr(bpy.context.active_object.active_material.scs_props, "shader_attribute_"+_key, newval)
    str("---")

It works except the aux attributes

Code: Select all

AttributeError: bpy_struct: attribute "shader_attribute_aux8" from "MaterialSCSTools" is read-only
What does it mean (how it can be locked) and how can I set them anyway?
Last edited by davidzoli on 19 Jan 2016 20:41, edited 4 times in total.
Image

User avatar
davidzoli
Posts: 3226
Joined: 22 Jun 2014 10:08
Location: Budapest, Hungary
Contact:

Re: Material attribute copy script

#2 Post by davidzoli » 20 Sep 2015 20:44

I've made some progression to set the aux attributes not with a list but values.
Error goes away, but the attribute not set

Code: Select all

floats = ["add_ambient", "reflection", "reflection2", "shadow_bias", "shininess", "tint_opacity"]
colors = ["diffuse", "env_factor", "specular"]
sets   = ["fresnel", "tint"]

for key in bpy.context.active_object.active_material["scs_mat_options"].keys():
    newval = ''
    _key = key
    str(key+" found")
    # variable is a float
    if (_key in floats):
        newval = float(bpy.context.active_object.active_material["scs_mat_options"][_key])
    # variable is a color
    if (_key in colors):
        newval = Color(map(float, bpy.context.active_object.active_material["scs_mat_options"][_key].strip(")").lstrip("(").split(', ')))
    # variable is a set of floats
    if (_key in sets):
        if(_key.find("[") >= 0):
            _key = _key.replace("[","").replace("]","")
        newval = list(map(float, bpy.context.active_object.active_material["scs_mat_options"][key].strip(")").lstrip("(").split(', ')))
    if (_key.find("aux") >= 0):
        if(_key.find("[") >= 0):
            _key = _key.replace("[","").replace("]","")
        newval2 = list(map(float, bpy.context.active_object.active_material["scs_mat_options"][key].strip(")").lstrip("(").split(', ')))
        i=0
        for listval in newval2:
            setattr(bpy.context.active_object.active_material.scs_props, "shader_attribute_"+_key+"["+str(i)+"]", listval)
            str("shader_attribute_"+_key+"["+str(i)+"] set to: "+str(listval))
            i=i+1
    # set the attributes
    if (newval!=''):
        str("shader_attribute_"+_key+" set to: "+str(newval))
        setattr(bpy.context.active_object.active_material.scs_props, "shader_attribute_"+_key, newval)
    str("---")
Image

User avatar
50keda
SCS Software
Posts: 2708
Joined: 07 Dec 2012 11:59

Re: Material attribute copy script

#3 Post by 50keda » 21 Sep 2015 07:37

Well you can't set this aux values because they are type of bpy.types.CollectionProperty which can not be set directly.

So to help you out change this:

Code: Select all

    if (_key.find("aux") >= 0):
        if(_key.find("[") >= 0):
            _key = _key.replace("[","").replace("]","")
        newval2 = list(map(float, bpy.context.active_object.active_material["scs_mat_options"][key].strip(")").lstrip("(").split(', ')))
        i=0
        for listval in newval2:
            setattr(bpy.context.active_object.active_material.scs_props, "shader_attribute_"+_key+"["+str(i)+"]", listval)
            str("shader_attribute_"+_key+"["+str(i)+"] set to: "+str(listval))
            i=i+1
to this:

Code: Select all

    if (_key.find("aux") >= 0):
        if(_key.find("[") >= 0):
            _key = _key.replace("[","").replace("]","")
        newval2 = list(map(float, bpy.context.active_object.active_material["scs_mat_options"][key].strip(")").lstrip("(").split(', ')))

        auxiliary_prop = getattr(bpy.context.active_object.active_material.scs_props, "shader_attribute_" + _key, None)

        # clean old values possible left from previous shader
        while len(auxiliary_prop) > 0:
            auxiliary_prop.remove(0)

        for listval in newval2:
            item = auxiliary_prop.add()
            item.value = listval
            item.aux_type = _key
PS: I haven't tested it, but it should work, if you will have any problems just shout.
Last edited by 50keda on 21 Sep 2015 16:19, edited 1 time in total.

User avatar
CrazyGijs
Posts: 2484
Joined: 24 May 2014 18:24
Location: The Netherlands
Contact:

Re: Material attribute copy script

#4 Post by CrazyGijs » 21 Sep 2015 15:01

One question, how can I use scripts in Blender? :mrgreen:
Image

Intel Core i5 9600k, Asus ROG STRIX Z390-F, 16GB 3000 Mhz Corsair RAM, ASUS ROG STRIX RTX 2070 Super OC 8GB, Logitech G502, Fanatec CSL Elite wheel base & pedals LC + P1 rim, Driving Force Shifter + DIY Scania SKRS.

User avatar
davidzoli
Posts: 3226
Joined: 22 Jun 2014 10:08
Location: Budapest, Hungary
Contact:

Re: Material attribute copy script

#5 Post by davidzoli » 21 Sep 2015 16:12

Thank you 50keda!
It works now. Has some debug message, but works like a charm :)
https://gist.github.com/davidzoli/47704 ... 48e178f7a8


@CrazyGijs
First option
Image
Image

User avatar
CristianoH125
Posts: 2480
Joined: 01 Jul 2014 13:26
Location: Portugal

Re: Material attribute copy script

#6 Post by CristianoH125 » 11 Oct 2015 20:14

Hey there, im using this script alot and i noticed that the values arent the same if compared to the values on the old mat
Why ?
What a Lovely Day... !

User avatar
davidzoli
Posts: 3226
Joined: 22 Jun 2014 10:08
Location: Budapest, Hungary
Contact:

Re: Material attribute copy script

#7 Post by davidzoli » 11 Oct 2015 21:10

Help me a bit :)
Please give an example. What is the old mat file, and what is the new?
Image

User avatar
CristianoH125
Posts: 2480
Joined: 01 Jul 2014 13:26
Location: Portugal

Re: Material attribute copy script

#8 Post by CristianoH125 » 11 Oct 2015 21:15

Diferents values like specular and diffuse

Code: Select all

Original mat 
        material : "eut2.dif.spec" {
	texture[0] : "/vehicle/trailer_eu/chereau/textures/wheel.tobj"
	texture_name[0] : "texture_base"
	add_ambient : 0.2
	diffuse : { 0.5 , 0.5 , 0.5 }
	reflection : 0
	shininess : 75
	specular : { 1.0 , 1.0 , 1.0 }
}
Exported mat with blender tools using your script
material : "eut2.dif.spec" {
	texture[0] : "/vehicle/trailer_eu/chereau/wheel.tobj"
	texture_name[0] : "texture_base"
	diffuse : { 0.214041 , 0.214041 , 0.214041 }
	specular : { 1 , 1 , 1 }
	shininess : 75
	add_ambient : 0.2
	reflection : 0
}
What a Lovely Day... !

User avatar
50keda
SCS Software
Posts: 2708
Joined: 07 Dec 2012 11:59

Re: Material attribute copy script

#9 Post by 50keda » 12 Oct 2015 08:21

@Cristiano: for color conversion problem please see this: viewtopic.php?f=159&t=176424&start=80#p425693

The thing is that colors in middle format are converted with conversion tools and as color space changes it means new values.

User avatar
CristianoH125
Posts: 2480
Joined: 01 Jul 2014 13:26
Location: Portugal

Re: Material attribute copy script

#10 Post by CristianoH125 » 12 Oct 2015 10:38

How do i use that formula ? :?
What a Lovely Day... !

Post Reply

Return to “User Tutorials & Tools”

Who is online

Users browsing this forum: No registered users and 1 guest