r/blenderpython • • Feb 22 '24

Python code to Shader Nodes

3 Upvotes

I think the python API is pretty cumbersome for creating materials, so I've made a decorator that converts more typical python to materials/node groups. Check it out https://github.com/JamesPickersgill/python-shader-nodes, although note this is a just a proof of concept so lots is missing. If anyone else thinks this is useful I can spend some more time fleshing it out.


r/blenderpython • • Jul 25 '23

Help getting properties

Post image
3 Upvotes

Does anyone know if there is a way in the api to loop through a properties tab to get all the values? I'm looking for something like a list or dictionary of all the current values. For example if I wanted to get all of the render property values from a project, I could call a method that would return all values. Any help is greatly appreciated


r/blenderpython • • Jun 09 '23

Hello, I am new to python , I already know how to code in c#, i have no idea why this wont work! help would be massively appreciated, I would like to print the location of each vertex of a cube.

3 Upvotes

r/blenderpython • • Dec 16 '21

Filmic Explained

3 Upvotes

Hi! I wanted to ask that what does Filmic View Transform and Filmic - Base Contrast Look in Color Management exactly do to the rendered images? Does it hamper the RGB color space in the images, if yes then how much and how? It would be really helpful if someone could help me out with this.


r/blenderpython • • Nov 01 '21

How do I save a list of objects in the blend file? So that I can save and open file keeping the state in my addon.

3 Upvotes

r/blenderpython • • Jul 14 '21

How do you select a vert on a Lattice in python? I want to create hooks

3 Upvotes

r/blenderpython • • Jun 23 '21

addons overriding each other. need someone to hold my hand through this

3 Upvotes

so basically i have two addons. on the second addon i basically copy&pasted the code from the first add on changed a few names. however whenever i install both add ons, only one addon will show up at a time. i know nothing about coding and i am basically just winging it. any help is greatly appreciated

addon one-

bl_info = {
	"name": "The Countryside Nature Collection",
	"description": "Nature models",
	"author": "TRBRenders",
	"version": (1, 0),
	"blender": (2, 83, 0),
	"location": "View 3D > Properties Panel",
	"wiki_url": "",
	"tracker_url": "",
	"support": "COMMUNITY",
	"category": "Add Mesh"
	}


# Libraries
import bpy
import os
from bpy.props import *
from bpy.types import Panel, Operator, PropertyGroup
from bpy.utils import previews
from bpy.types import WindowManager


# Panel
class REAL_PT_trees(Panel):
	bl_space_type = "VIEW_3D"
	bl_context = "objectmode"
	bl_region_type = "UI"
	bl_label = "Nature"
	bl_category = "The Countryside Nature Collection"

	def draw(self, context):
		scn = bpy.context.scene
		layout = self.layout
		settings = scn.trees
		wm = context.window_manager

		# Categories
		col = layout.column(align=True)
		row = col.row(align=True)
		row.prop(settings, "category", expand=True)

		# Previews
		row = col.row()
		row.template_icon_view(wm, "tree_previews", show_labels=True)

		# Furniture name
		name = bpy.data.window_managers["WinMan"].tree_previews
		row = col.row(align=True)
		row.alignment = 'CENTER'
		row.label(text=name)

		# Add Button
		row = col.row(align=True)
		row.scale_y = 1.5
		row.operator("trees.add", icon="ADD", text="Add")


# Append
class TREES_OT_Add(Operator):
	bl_idname = "trees.add"
	bl_label = "Add Tree"
	bl_description = "Add the selected Models"
	bl_options = {'REGISTER', 'UNDO'}

	def execute(self, context):
		category = context.scene.trees.category
		selected = bpy.data.window_managers["WinMan"].tree_previews

		# Append tree
		filepath = os.path.join(os.path.dirname(__file__), "Blends/" + category + os.sep + selected + ".blend")
		with bpy.data.libraries.load(filepath) as (data_from, data_to):
			data_to.objects = [name for name in data_from.objects]
		for obj in data_to.objects:
				context.collection.objects.link(obj)

		return{'FINISHED'}


# Previews
def generate_previews(self, context):
	category = context.scene.trees.category
	directory = os.path.join(os.path.dirname(__file__), "Icons")
	pcoll = trees_collection["main"]
	if directory == pcoll.images_location:
		return pcoll.tree_previews
	# Generate the thumbnails
	enum_items = []
	for i, image in enumerate(sorted(os.listdir(directory))):
		icon = pcoll.get(image)
		if not icon:
			filepath = os.path.join(directory, image)
			thumb = pcoll.load(image, filepath, 'IMAGE')
		else:
			thumb = pcoll[image]
		enum_items.append((image[:-4], image[:-4], "", thumb.icon_id, i))
	pcoll.tree_previews = enum_items
	pcoll.images_location = directory
	bpy.context.window_manager['tree_previews'] = 0
	return enum_items


# Properties
class TreeSettings(PropertyGroup):
	category : bpy.props.EnumProperty(
		name="Category",
		items= [
			
			
			],
		description="Select a category"
		)


#############################################################################################
trees_collection = {}

classes = (
	REAL_PT_trees,
	TREES_OT_Add,
	TreeSettings
	)

register, unregister = bpy.utils.register_classes_factory(classes)

# Register
def register():
	for cls in classes:
		bpy.utils.register_class(cls)
	bpy.types.Scene.trees = bpy.props.PointerProperty(type=TreeSettings)

	# Icons
	WindowManager.tree_previews = EnumProperty(
		name="Icons",
		items=generate_previews,
		description="Select Desired Models"
		)
	pcoll = bpy.utils.previews.new()
	pcoll.images_location = ""
	pcoll.tree_previews = ()
	trees_collection["main"] = pcoll


# Unregister
def unregister():
	for cls in classes:
		bpy.utils.unregister_class(cls)
	del bpy.types.Scene.trees
	del WindowManager.tree_previews

	for preview in trees_collection.values():
		bpy.utils.previews.remove(preview)
	trees_collection.clear()


if __name__ == "__main__":
	register()

addon two-

bl_info = {
	"name": "The Coral And Creatures Collection",
	"description": "Coral And Creature Models",
	"author": "TRBRenders",
	"version": (1, 0),
	"blender": (2, 92, 0),
	"location": "View 3D > Properties Panel",
	"wiki_url": "",
	"tracker_url": "",
	"support": "COMMUNITY",
	"category": "Add Mesh"
	}


# Libraries
import bpy
import os
from bpy.props import *
from bpy.types import Panel, Operator, PropertyGroup
from bpy.utils import previews
from bpy.types import WindowManager


# Panel
class REAL_CT_trees(Panel):
	bl_space_type = "VIEW_3D"
	bl_context = "objectmode"
	bl_region_type = "UI"
	bl_label = "Coral And Creatures"
	bl_category = "The Coral And Creatures Collection"

	def draw(self, context):
		scn = bpy.context.scene
		layout = self.layout
		settings = scn.trees
		wm = context.window_manager

		# Categories
		col = layout.column(align=True)
		row = col.row(align=True)
		row.prop(settings, "category", expand=True)

		# Previews
		row = col.row()
		row.template_icon_view(wm, "tree_previews", show_labels=True)

		# Furniture name
		name = bpy.data.window_managers["WinMan"].tree_previews
		row = col.row(align=True)
		row.alignment = 'CENTER'
		row.label(text=name)

		# Add Button
		row = col.row(align=True)
		row.scale_y = 1.5
		row.operator("trees.add", icon="ADD", text="Add")


# Append
class TREES_CT_Add(Operator):
	bl_idname = "trees.add"
	bl_label = "Add Tree"
	bl_description = "Add the selected Models"
	bl_options = {'REGISTER', 'UNDO'}

	def execute(self, context):
		category = context.scene.trees.category
		selected = bpy.data.window_managers["WinMan"].tree_previews

		# Append tree
		filepath = os.path.join(os.path.dirname(__file__), "Blends/" + category + os.sep + selected + ".blend")
		with bpy.data.libraries.load(filepath) as (data_from, data_to):
			data_to.objects = [name for name in data_from.objects]
		for obj in data_to.objects:
				context.collection.objects.link(obj)

		return{'FINISHED'}


# Previews
def generate_previews(self, context):
	category = context.scene.trees.category
	directory = os.path.join(os.path.dirname(__file__), "Icons")
	pcoll = trees_collection["main"]
	if directory == pcoll.images_location:
		return pcoll.tree_previews
	# Generate the thumbnails
	enum_items = []
	for i, image in enumerate(sorted(os.listdir(directory))):
		icon = pcoll.get(image)
		if not icon:
			filepath = os.path.join(directory, image)
			thumb = pcoll.load(image, filepath, 'IMAGE')
		else:
			thumb = pcoll[image]
		enum_items.append((image[:-4], image[:-4], "", thumb.icon_id, i))
	pcoll.tree_previews = enum_items
	pcoll.images_location = directory
	bpy.context.window_manager['tree_previews'] = 0
	return enum_items


# Properties
class TreeSettingss(PropertyGroup):
	category : bpy.props.EnumProperty(
		name="Category",
		items= [
			
			
			],
		description="Select a category"
		)


#############################################################################################
trees_collection = {}

classes = (
	REAL_CT_trees,
	TREES_CT_Add,
	TreeSettingss
	)

register, unregister = bpy.utils.register_classes_factory(classes)

# Register
def register():
	for cls in classes:
		bpy.utils.register_class(cls)
	bpy.types.Scene.trees = bpy.props.PointerProperty(type=TreeSettingss)

	# Icons
	WindowManager.tree_previews = EnumProperty(
		name="Icons",
		items=generate_previews,
		description="Select Desired Models"
		)
	pcoll = bpy.utils.previews.new()
	pcoll.images_location = ""
	pcoll.tree_previews = ()
	trees_collection["main"] = pcoll


# Unregister
def unregister():
	for cls in classes:
		bpy.utils.unregister_class(cls)
	del bpy.types.Scene.trees
	del WindowManager.tree_previews

	for preview in trees_collection.values():
		bpy.utils.previews.remove(preview)
	trees_collection.clear()


if __name__ == "__main__":
	register()

r/blenderpython • • Jun 23 '21

Noob Question - Python install

3 Upvotes

Hi,
I installed visual studio code and pointed the python interpreter to the blender python bin folder. I then attempted to install "fake bpy" (e.g. pip install fake-bpy-module-2.91 ) which was succesful. When I looked at my script, it couldn't resolve "bpy" (after a restart). I already had a newer version of python installed and when I switched to that interpreter auto-complete started working.
From what I have read, it is always best to use the blender version of python but autocomplete only works when I switch to the version I installed on windows.
Should I just uninstall my python on windows? I only use it for one script. I just wonder if there is some issues with fake-bpy not being recognized because of a python install in another location.
Any help would be appreciated.


r/blenderpython • • May 17 '21

Blender keeps crashing after opening the System console + save

3 Upvotes

Title. Sometimes it's fine if I toggle the system console off again but sometimes it still crashes. Why does this happen ? It makes the scripting experience pretty bad


r/blenderpython • • Apr 03 '21

Lip sync using speech to text

3 Upvotes

Hi there. I was wondering if there is any way to control shape keys in real time using a microphone. I was thinking that it would make lip-syncing a lot easier if you could see the movement of a character happen in real time and control it using a microphone. The way that I would guess that this would work, is that the real time audio from the microphone would be translated into text or specific "tags" that could be used to control shape keys with their intensity. I was thinking that this would be similar to the kind of tracking available in vseeface or luppet, but could be recorded directly into the blender timeline. Would something like this be possible and/or does it already exist?


r/blenderpython • • Feb 19 '21

Check if object is duplication of other object through python

3 Upvotes

Hello!

I'm writing an export script for my scenes in blender to use in a custom game engine. If I create a house and name it "House" and then duplicate it, it gets named "House.001" by default. When I loop through the objects in the scene in python, is there a way to check if an object is a duplication of another object and therefor has the same mesh? It would be helpful to not have to save (and later load) the geometry data more than once when it's the same.

I could of course just assume that all objects with names ending with ".00X" is a duplication, but that would lead to problems if I change the name of the object or if I modify the object without changing the name.

Thanks for help!


r/blenderpython • • Jan 22 '21

Python code on dividing a mesh into smaller meshes consisting of a particular number of tris.

3 Upvotes

Can anybody help me with writing a code for a taking a pre existing mesh with suppose 1000 tris. Then breaking down the bigger mesh into smaller meshes which consist of maybe 10 tris. So at the end for the above case there will be 100 different mesh each consisting of 10 tris.


r/blenderpython • • Jan 08 '21

Isometric Camera Setup

3 Upvotes

I made an Operator (I think) to automatically setup and align a camera for pixel perfect Isometric stuff.

https://github.com/hansonry/blender-isometric-scripts/blob/master/BlenderIsoCameraUpdate.py

It works best if your image width = 2 * image height - 2 and image height is even.

You can set your background to transparent and your Rendering Samples to 1 to really see it. If you want to maintain your high sampling count you can generate a mask for a clean cut.

I have no idea what I am doing when it comes to scripting for blender, but if you know and you could sanity check my integration, I would appreciate that. Also I am trying to figure out how to enforce that image width = 2 * image height - 2 and that the image height is even. Any hints there would be helpful.

Thanks


r/blenderpython • • Dec 29 '20

How to add Boid brain settings to a particle system via python script? Question.

Thumbnail
blender.stackexchange.com
3 Upvotes

r/blenderpython • • Nov 22 '20

Can you overwrite mouse click in 3d view?

3 Upvotes

The idea is to prevent a mouse click in the 3d view, edit and sculpt modes, unless a certain condition in the script is met. Is this something that can be overwritten?


r/blenderpython • • Jun 08 '20

Questions on exporting unique vertex attributes

3 Upvotes

I'm working on a custom exporter. I'm not too familiar with blender and its API, so I looked at the obj exporter and got a bit confused at what it's trying to do.

I'm only interested in exporting basic geometry (for now) in a way that makes sense for a 3d game engine, so vertices can't have multiple values for the same attribute etc, but also I want to use as few vertices as possible. I'm not interested in export performance yet as my meshes are meant to be generally small.

Currently I have a working solution that works like this:

  1. Create an empty dictionary that maps vertex information to an index

  2. For each entry in the mesh loops:

    1. Gather all attributes (vertex index, normal, uvs, etc) into a single object and check if it exists in the dictionary.

    2. If it doesn't:

      1. Add the entry as the key and set value to be the current dictionary size. The value will also be used as the output index for this loop entry.

      2. Add the attributes as a new Vertex in the output vertices list.

    3. If it does exist, use that value as the output index of this loop entry.

    4. Add the output index to the output indices list.

I don't feel very good about hashing and comparing floating point numbers, but then again I assume blender explicitly uses the exact same floating point value when the normal is supposed to be the same so hashing and equality checking will work as expected.

Is what I'm doing the right way or is there a more proper way to do this? Does blender have an integer id for unique normals, colors, uvs, etc the way it does for positions that I can use instead of hashing floats?


r/blenderpython • • Feb 18 '20

Is there a way to access the python scripts in a blender file in a python script to save them?

3 Upvotes

I’m trying to write a script to save all my scripts but I can’t figure out if it’s possible to even access that with the blender api. Does anyone know if this is possible?


r/blenderpython • • Jan 02 '20

Ray casting and bounding boxes

3 Upvotes

Hello everybody!

I've already explained my problem in this post on SO. Basically I need to identify bounding boxes for the objects in the rendered image, but only accounting for the portion of the object which is in view, not the one behind other objects. I've tried using the scene.ray_cast function, but that gives highly incorrect results.

I need this in order to generate synthetic images as a training dataset for a neural network, but I'm really stuck on this point and I won't get my MSc degree in space engineering if I don't solve it LOL

Please, someone help me


r/blenderpython • • Nov 28 '19

Race Charts using Blender Python

3 Upvotes

Anyone has idea about Race Bar charts using Blender Python ?

I am very new to blender


r/blenderpython • • Aug 31 '19

Api throws context error when adding UI tool to sculpt or particle mode

3 Upvotes

r/blenderpython • • Aug 30 '19

problem with particle system behaviour

3 Upvotes

I have demonstrated the complete problem in here, but in short, when objects are used as instances in particle system with effectors, then they have this weird behaviour that they rotate around their axes, now I'd expect some kind of option to limit this rotation in order to stop a complete revolution of the particle instance, as I have demonstrated in the link above, but there isn't.

So, I'd like to know if there is a way to access this data via a script in order to clamp it down to only a certain value.


r/blenderpython • • Mar 18 '18

what are application handlers?

3 Upvotes

r/blenderpython • • Dec 26 '17

Blender Screen Space SubSurface Scatter Trex SSS

Thumbnail
youtube.com
3 Upvotes

r/blenderpython • • Nov 30 '17

new to python but with one target in mind from now: Creating a bot to do my Job :D

3 Upvotes

to all of you guys that are more than experts at Python, could it be possible to build a code to "read-write", in my job I have to fill LOTS of information about people, im a touristic guide, I want the bot to fill every field bassed on my whatsapp lists, I know it can be done but FOR NOW i dont have idea of how, to recognize the field "name" then recognize the first name on my list, and put it on the list, and so on, name, surname, age, country, gender, email, booking number, and such... it takes hell of 2-3 hours of my precious time (after my 9-12 hours of work) every single day, so I trully need to improve that.