dkberlin
(dkberlin)
January 6, 2018, 2:06am
1
Hey guys,
so we need to come up with a custom ui within blender for our exam in python this semester.
the idea is quite simple:
offer some lightning presets (like portrait etc) -> add the lights -> and then be able to adjust them (intensity, colour etc.) all within a custum panel on the left.
im used to c# and i dont know blender, but i came up with a start with some internet researching. in a video that can be found
i found what i believe to be a pretty good start for the project.
the thing is: i dont want to create new objects, i want to adjust existing lightnings.
now i know that with the console inside blender i can see pretty much exactly what i would have to “copy” for my project.
but i need a little push in the right direction like
how to for example select all lights with a menu
how to put a slider in a panel and link it to the intensity of the chosen light
how to add sliders for R G and B and link them
etc.
do you guys can give me some directions? or point me to a good tutorial? i am pretty sure within the next weeks ill be coming back to this thread with more questions
thanks in advance!
look in blender text editor
there is a menu for some templates scripting
show us some script you started !
lights are a specific object in blender
so should be easy to find these in a scene
there is an RGB color button for panel
happy bl
here is sample script for different objects in blender
qtyob=0
jj=0
for ob in bpy.data.objects:
if ob.type == "LAMP":
qtyLAMP+=1
elif ob.type == "CURVE":
qtyCURVE+=1
elif ob.type == "SURFACE":
qtySURFACE+=1
elif ob.type == "TEXT":
qtyTEXT+=1
elif ob.type == "ARMATURE":
qtyARMATURE+=1
elif ob.type == "LATTICE":
qtyLATTICE+=1
elif ob.type == "EMPTY":
qtyEMPTY+=1
elif ob.type == "CAMERA":
qtyCAMERA+=1
elif ob.type == "META":
qtyMETA+=1
elif ob.type == "MESH":
qtyMESH+=1
jj+=1
# print (ob.name)
qtyob=jj
print ()
print('MESH =',qtyMESH,' EMPTY =',qtyEMPTY,' TEXT =',qtyTEXT)
print ( 'CURVE =',qtyCURVE,' SURFACE =',qtySURFACE)
print ('LAMP =',qtyLAMP ,'CAMERA =',qtyCAMERA)
print ( 'META =',qtyMETA,' LATTICE =',qtyLATTICE,' ARMATURE =',qtyARMATURE)
print ()
print ('Qty of objects =',qtyob)
print ()
&&&
bpy.ops.object.select_all(action='SELECT')
obj_sel = bpy.context.selected_objects
print ('list LEN obj_sel=',len(obj_sel))
print ()
jj=0
for i in obj_sel:
print ('jj=',jj,' ',i.name,' Type',i.type)
jj+=1
print ()
select what you need !
happy bl
dkberlin
(dkberlin)
January 8, 2018, 12:27am
4
hey guys, thanks for the answers so far. this is the code i wrote until now:
import bpy
from bpy.types import Menu, Panel, UIList
class ViewLightningPanel():
# where the new panel will be accessable
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
# the new panel
class LampAdjustPanel(ViewLightningPanel, Panel):
bl_idname = "panel_lampadjust"
bl_label = "Lamp Adjustment"
bl_context = "objectmode"
bl_category = "LMD"
# draw a new button, call operator on click
def draw(self, context):
layout = self.layout
col = layout.column(align = True)
layout.operator("object.lamp_selection_operator", text = "Select All Lamps")
layout.operator("object.lamp_strength_tozero_operator", text = "Switch Off All Lamps")
# operator for button
class SelectAllLampsOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.lamp_selection_operator"
bl_label = "Simple Lamp Selection Operator"
def execute(self, context):
SelectAllLamps(context)
return {'FINISHED'}
class SwitchOffAllLampsOperator(bpy.types.Operator):
bl_idname = "object.lamp_strength_tozero_operator"
bl_label = "Simple Lamp Switch Off Operator"
def execute(self, context):
SwitchOffAllLamps(context)
return {'FINISHED'}
def SwitchOffAllLamps(context):
sce = bpy.context.scene
for object in sce.objects:
if object.type == "LAMP":
bpy.data.lamps[object.name].node_tree.nodes["Emission"].inputs[1].default_value = 1000
# function for operator
def SelectAllLamps(context):
sce = bpy.context.scene
for object in sce.objects:
if object.type != "LAMP":
object.select = False
else:
object.select = True
def register():
bpy.utils.register_class(LampAdjustPanel)
bpy.utils.register_class(SelectAllLampsOperator)
bpy.utils.register_class(SwitchOffAllLampsOperator)
def unregister():
bpy.utils.unregister_class(LampAdjustPanel)
bpy.utils.unregister_class(SelectAllLampsOperator)
bpy.utils.unregister_class(SwitchOffAllLampsOperator)
if __name__ == "__main__":
register()
# seems to be needed (?)
# bpy.ops.object.simple_operator()
what i need now is a way to get some user input - or at best a slider of some sort - to change the lights intensity and color…
dkberlin
(dkberlin)
January 8, 2018, 3:31am
5
Ok, here’s some update on my code so far:
import bpy
from bpy.types import Menu, Panel, UIList
class ViewLightningPanel():
# where the new panel will be accessable
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
# the new panel
class LampAdjustPanel(ViewLightningPanel, Panel):
bl_idname = "panel_lampadjust"
bl_label = "Lamp Adjustment"
bl_context = "objectmode"
bl_category = "LMD"
# draw a new button, call operator on click
def draw(self, context):
layout = self.layout
col = layout.column(align = True)
layout.operator("object.lamp_selection_operator", text = "Select All Lamps")
layout.operator("object.switchoffalllamps_operator", text = "Switch Off All Lamps")
layout.operator("brightness.operator", text = "Change Luminosity")
class BrightnessOperator(bpy.types.Operator):
bl_idname = "brightness.operator"
bl_label = "Set Luminosity"
brightnessValue = bpy.props.IntProperty(name="Luminosity", description ="the actual brightness", default = 555, min = 10)
def execute(self,context):
self.report({'INFO'}, str(self.brightnessValue))
SetLampStrength(context, int(self.brightnessValue))
return{'FINISHED'}
def invoke(self,context,event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(self,"brightnessValue")
# operator for button
class SelectAllLampsOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.lamp_selection_operator"
bl_label = "Simple Lamp Selection Operator"
def execute(self, context):
SelectAllLamps(context)
return {'FINISHED'}
class SwitchOffAllLampsOperator(bpy.types.Operator):
bl_idname = "object.switchoffalllamps_operator"
bl_label = "Simple Lamp Switch Off Operator"
def execute(self, context):
SetLampStrength(context, 0)
return {'FINISHED'}
def SetLampStrength(context, lampStrength):
helligkeit = lampStrength
sce = bpy.context.scene
for object in sce.objects:
if object.type == "LAMP":
bpy.data.lamps[object.name].node_tree.nodes["Emission"].inputs[1].default_value = helligkeit
# function for operator
def SelectAllLamps(context):
sce = bpy.context.scene
for object in sce.objects:
if object.type != "LAMP":
object.select = False
else:
object.select = True
def register():
bpy.utils.register_class(LampAdjustPanel)
bpy.utils.register_class(SelectAllLampsOperator)
bpy.utils.register_class(SwitchOffAllLampsOperator)
bpy.utils.register_class(BrightnessOperator)
def unregister():
bpy.utils.unregister_class(LampAdjustPanel)
bpy.utils.unregister_class(SelectAllLampsOperator)
bpy.utils.unregister_class(SwitchOffAllLampsOperator)
bpy.utils.unregister_class(BrightnessOperator)
if __name__ == "__main__":
register()
# seems to be needed (?)
# bpy.ops.object.simple_operator()
next step: i need to find out how to get the colour picker into the panel - or some RGB sliders…
there is a button for rgb color for panel
but have not done any panel script in last 2 years
will see if I can find sample for that tonight
thanks
happy bl
try this one for RGB button
property colorbutton
import bpy
class OBJECT_PT_hello(bpy.types.Panel):
bl_label = "Hello World Panel"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Helloworld!", icon='WORLD_DATA')
row = layout.row()
row.label(text="Activeobject is: " + obj.name)
row = layout.row()
row.prop(obj, "name")
# Material Property Added
mat =bpy.data.materials["Material"]
row.prop(mat, "diffuse_color",text="")
def register():
bpy.utils.register_class(OBJECT_PT_hello)
def unregister():
bpy.utils.unregister_class(OBJECT_PT_hello)
if __name__ == "__main__":
register()
happy bl