loading...

Zero Hardcoded Values

djraptor11 profile image DJRaptor ・2 min read

Some solutions to this problem involve graph or block based programming interfaces, they let the user safely change values in logic without having to worry about syntax errors. But these solutions like Unreal Engine Blueprints, are not simple for programmers to automate and setup the initial logic for the users.

Alt Text

Above is the solution used in Blender-On-Acid, the script contains zero hardcoded values. Values are abstracted by single letter Latin variable names, these are mapped to the text editor UI, where they can be edited at runtime and saved as metadata in the blender file. In the example above i'm editing the variable Ƹ, which is linked to the material diffuse color.

Setting default values for the UI is done in the script used to setup the blender scene, see below.

import bpy

world = bpy.data.worlds[0]
script = bpy.data.texts.new(name='myscript')
world.blender_script = script
## default material color ##
script.clr_slot0 = [1,0,0, 1]
## default material add random ##
script.num_slot5 = 0.1
script.num_slot6 = 0.3
## default drop point x,y,z ##
script.num_slot0 = 0.0
script.num_slot1 = 0.0
script.num_slot2 = 10.0
## default size ##
script.num_slot3 = 2.0
## default random threshold ##
script.num_slot4 = 0.98
## default cube to copy mesh data from ##
script.obj_slot0 = bpy.data.objects['Cube']

src = '''
import bpy, _bpy
from random import *

if random() > №:
    mat = bpy.data.materials.new(name='mymat')
    mat.use_nodes = False
    mat.diffuse_color = Ƹ
    mat.diffuse_color[1] += uniform(Ω, Å)

    ob = bpy.data.objects.new(name='myob', object_data=Ƃ.data.copy())
    ob.data.materials[0] = mat
    bpy.context.collection.objects.link(ob)
    ob.location = [₦, €, ₴]
    ob.ode_size[0] = ₵
    ob.ode_size[1] = ₵
    ob.ode_size[2] = ₵

    ob.rotation_mode = "QUATERNION"
    _bpy.ode_add_object( ob )


'''

script.from_string( src )

Posted on by:

Discussion

pic
Editor guide