new to Python, so struggle a little. With some help from here I managed to create little code to count objects in my scene. It works great in console, now I want to create a panel which will automatically update object count. Here's the code for object counter:
from collections import defaultdict
counter = defaultdict(int)
for obj in bpy.data.collections['Stands'].all_objects:
dot_split = obj.name.split('.')
counter[dot_split[0].split('.')[0].strip()] += 1
for name, count in counter.items():
print(f"{name:35} | {count:1d}")
I want to try and implement this code into ui_panel_simple template.
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
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="Hello world!", icon='WORLD_DATA')
def register():
bpy.utils.register_class(HelloWorldPanel)
def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
register()
Here is what I tried to do:
```import bpy
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self,context):
layout = self.layout
def obj(self):
from collections import defaultdict
counter = defaultdict(int)
for obj in bpy.data.collections['Stands'].all_objects:
dot_split = obj.name.split('.')
counter[dot_split[0].split('.')[0].strip()] += 1
for name, count in counter.items():
return f"{name:35} | {count:1d}"
col = layout.column()
col.label(text = "1" + obj)
def register():
bpy.utils.register_class(HelloWorldPanel)
def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
register()
Obviously, it doesn't work. I have been trying for 3 days now and I am at a loss...
Here is the latest message from console:
location: :-1 Python: Traceback (most recent call last): File "C:\Users\fedor\Regional group stand.blend\ob_count.py", line 29, in draw TypeError: can only concatenate str (not "function") to str
1
, not the result of the function. You need to call it. BTW you don't needself
as an argument here.def obj():
should be enough. Try replacing the label line withcol.label(text = "1" + obj())
orcol.label(text = "1" + str(obj()))
to make sure you're dealing with string types. Not a blender related question TBH, I suggest you familiarize yourself with python basics a bit more before diving into the Blender API. Cheers :) $\endgroup$col.label(text = "1" + str(obj()))
$\endgroup$