r/blenderpython • • Mar 30 '21

Scipy Function as Node (sverchok addon)

1 Upvotes

Hello,

I am trying to use the scipy.interpolate.interpn function within blender. I assumed the only way to do this is to utilize the sverchok addon, so my first attempt at using this function is with the sverchok addon, but let me know if there is a better way to do this.

Although I have some basic knowledge of python, I think I am a bit out of my depth here. Nonetheless, I tried writing a script to use with the Scripted Node Lite node in sverchok:

"""
in in_points v
in values s
in xi v
out out_values s
"""

import numpy as np
import sys

from sverchok.utils.logging import exception, info
from sverchok.data_structure import zip_long_repeat

try:
    import scipy
    from scipy.interpolate import interpn
except ImportError as e:
    info("SciPy module is not available. Please refer to https://github.com/nortikin/sverchok/wiki/Non-standard-Python-modules-installation for how to install it.")
    raise e

out_values = []

for everypoint in xi:
    new_values = interpn(points, values, xi, method='linear', bounds_error=True, fill_value=0)

    out_values.append(new_values)

Here is what I get when I try to use the script as a scripted node in blender:

error screenshot

here are the files I am working with: https://drive.google.com/file/d/1a2a0l3rghzAbP91JEAb-mZXQZYpiLEsG/view?usp=sharing

If anyone could help me utilize this function in blender, or have any improvements for the script above, I would greatly appreciate it! Thanks,


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 • • Jun 17 '21

After a bit of code, ~165 lines, I have a method that can draw trapezoids on acute angled points and have the trapezoid's edges touch at a tangent to the existing line, creating sharp corners on grease pencil lines.

Post image
17 Upvotes

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 22 '21

How do I run a python script inside Blender from inside my terminal

2 Upvotes

These are my paths:

Blender is installed on C:\Program Files\Blender Foundation\Blender 2.92

My scripts are saved in C:\Blender\scripts

Yes, I know that I can simply run the scripts from inside the Blender GUI but I want to use Sublime Text as my editor.


r/blenderpython • • Jun 15 '21

Can you help? I wrote a script to add sharp corners on grease pencil lines on the points that have angle smaller than a defined value. I need to know how to get the radius of that point, as of now, I can read the stroke.line_width and the point.pressure, but their product is way (x100+) too big.

Post image
8 Upvotes

r/blenderpython • • Jun 19 '21

Adding a CollectionProperty to Spline

Thumbnail self.blender
1 Upvotes

r/blenderpython • • Jun 15 '21

Does anyone have any tips for speeding up scene.ray_cast() on very large scenes

1 Upvotes

I am running the scene raycast function on a bunch of different objects in a scene with thousands of objects and it can take a half a second to a second per ray, which seems incredibly slow to me. I'm wondering if anyone here might have tips on how I can optimize this a bit. Please note, I'm raycasting to collection instances so I don't think I can use object.ray_cast since non mesh object types aren't supported (if I'm wrong about this please let me know)


r/blenderpython • • Jun 05 '21

Hypercube/Tesseract script in blender 2.93

7 Upvotes

People have been asking for it so here it is.

The hypercube script that was originally written for blender 2.41, that I ported to 2.66 and now 2.93 (I'm not the original author). I have provided the script here and the pasteall link. Follow the instructions in the script or watch the vid at the end. Make sure that 'Developer Extras' is enabled in the preferences.

#!BPY
# SPACEHANDLER.VIEW3D.EVENT
"""
Name: 'Hypercube'
Blender: 241
Group: 'Add'
Tooltip: 'Creates a Hypercube and lets you rotate it.'
"""
 
#    Copyright (C) 2010 Wanja Chresta
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
__author__ = "Wanja Chresta"
__url__ = ["www.identi.ca/brachiel"]
__version__= '0.1'
__bpydoc__= '''\
This simple script creates a hypercube and binds keys so that you'll be able to rotate the Hypercube in the forth dimension.
'''
# the comment in line 1 tells blender to send us the events
 
        # Ported to Blender 2.66.0 r54697 with Python 3.3, by Meta_Riddley
        # Ported to Blender 2.93 by Meta_Riddley
        
        #------- How to use ----------
        # New blend file, delete default cube. Go to camera view.
        # New text document, paste code.
        # Run script.
        # In settings make sure that spacebar opens the search menu
        # In 3D-view, press space-bar and write hypercube and select hypercube in the list.
        # Now the hypercube can be rotated using the q,w,e,a,s and d keys.
        # Press ESC to exit without deleting the hypercube
        
#TODO: Completely rewrite the script to follow modern operator conventions and api
 
import bpy
import math
import mathutils
import bgl
import blf
#editmode = Window.EditMode()    # are we in edit mode?  If so ...
#if editmode:
#        Window.EditMode(0) # leave edit mode before getting the mesh
 
HYPERCUBE = None
HYPERCUBE_VERTS = []
 
GLOBAL_ANGLES = [0., 0., 0.]
 
 
def project(vec4):
        """projects [x,y,z,t] to Mathutils.Vector(x',y',z') in a fancy non-isometric way"""
        cx, cy, cz = [0., 0., 0.] # center
       
        x,y,z,t = vec4
               
        return mathutils.Vector([ x + (cx - x)*t/4., y + (cy - y)*t/4., z + (cz - z)*t/4.])
 
        # with this transformation we get a fancy image of our hypercube. If you want isometric, just
        # do this instead:
        # return Mathutils.Vector(vec4[0:3])
def rotate_hypercube(a=0, b=0, c=0):
        global GLOBAL_ANGLES
        rotate = mathutils.Matrix(((
                        (math.cos(a),0,0,-math.sin(a)),
                        (0,math.cos(b),0,-math.sin(b)),
                        (0,0,math.cos(c),-math.sin(c)),
                        (math.sin(a),math.sin(b),math.sin(c),math.cos(a)*math.cos(b)*math.cos(c)))))      
        # only used to display angles in gui
        GLOBAL_ANGLES[0] += a
        GLOBAL_ANGLES[1] += b
        GLOBAL_ANGLES[2] += c
        transform_hypercube(rotate)
       
      
       
def transform_hypercube(transformation):
        global HYPERCUBE, HYPERCUBE_VERTS
        transform_hypercube_from(HYPERCUBE, HYPERCUBE_VERTS, transformation)
        
def transform_hypercube_from(ob, hypercube_vert, transformation):
        for i in range(len(hypercube_vert)):
                #print(hypercube_vert[i])
                hypercube_vert[i] = transformation @ hypercube_vert[i]
               
        mesh = ob.data
        i = 0
        for v in mesh.vertices:
                v.co = project(hypercube_vert[i])
                i += 1 
       
def update_hypercube():
        global HYPERCUBE, HYPERCUBE_VERTS
       
        mesh = HYPERCUBE.data
        i = 0
        for v in mesh.vertices:
                v.co = project(HYPERCUBE_VERTS[i])
                i += 1 
               
 
def create_hypercube(name="hyperCube"):
        global HYPERCUBE_VERTS, HYPERCUBE
               
        HYPERCUBE, HYPERCUBE_VERTS = create_hypercube_(name)
 
       
def create_hypercube_(name="hyperCube"):
        # define vertices and faces for a pyramid
                       
        hypercube = [ [i, j, k, l] for i in [0,1] for j in [0,1] for k in [0,1] for l in [0,1] ]
        hypercube_verts = list(map(mathutils.Vector, hypercube))
       
        # TODO: find a better (and working) way to do this
        faces = []
        for k in [0,1]: # tries (and fails) to create the faces with normals pointing to the outside
                for l in [0,1]:
                        faces.append(list(map(hypercube.index, [ [i^j,j,k,l] for j in [k,k^1] for i in [l,l^1] ])))
                        faces.append(list(map(hypercube.index, [ [i^j,k,j,l] for j in [k,k^1] for i in [l,l^1] ])))
                        faces.append(list(map(hypercube.index, [ [i^j,k,l,j] for j in [k,k^1] for i in [l,l^1] ])))
                        faces.append(list(map(hypercube.index, [ [k,i^j,j,l] for j in [k,k^1] for i in [l,l^1] ])))
                        faces.append(list(map(hypercube.index, [ [k,i^j,l,j] for j in [k,k^1] for i in [l,l^1] ])))
                        faces.append(list(map(hypercube.index, [ [k,l,i^j,j] for j in [k,k^1] for i in [l,l^1] ])))
                                               
       
        #print "We got %i vertices and %i faces" % (len(hypercube), len(faces))
       
        me = bpy.data.meshes.new(name)          # create a new mesh
        Iter_Coords = map(project, hypercube)
        me.from_pydata(list(Iter_Coords),[],[])          # add vertices to mesh
        me.from_pydata([],[],faces)           # add faces to the mesh (also adds edges)
       
#        me.vertex_colors = 1              # enable vertex colors
#        me.faces[1].col[0].r = 255       # make each vertex a different color
#        me.faces[1].col[1].g = 255
#        me.faces[1].col[2].b = 255
       
        scn = bpy.context.scene         # link object to current scene
        ob = bpy.data.objects.new("cube", me)
        scn.collection.objects.link(ob)
       
        return ob, hypercube_verts
 
 
 
def drawHandler(x,y):
        blf.position(0, 15, 30, 0)
        blf.draw(0,"""Hypercube: Point here and use the {q,w,e,a,s,d} keys to rotate the hypercube""")
               
        blf.position(0, 15, 40, 0)
        blf.draw(0,"""Angles: %s""" % GLOBAL_ANGLES)
               
        blf.position(0, 15, 50, 0)
        blf.draw(0,"""Press ESC to quit (without deleting the Hypercube)""")
     
        bgl.glEnable(bgl.GL_BLEND)
        #bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
        bgl.glLineWidth(2)
      
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        #bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

def buttonHandler(evt):
        return # ignore the rest; we don't need that 
 
#if editmode:
#        Window.EditMode(1)  # optional, just being nice
create_hypercube()
 
# center the hypercube
for v in HYPERCUBE_VERTS:
        v[0] = 2*v[0] - 1
        v[1] = 2*v[1] - 1
        v[2] = 2*v[2] - 1
        v[3] = 2*v[3] - 1
 
update_hypercube()  
class Hyper(bpy.types.Operator):
    """Rotate the HyperCube"""
    bl_idname = "view3d.hyper"
    bl_label = "hypercube"

    def modal(self, context, event):
        context.area.tag_redraw()
        
        #Controls how fast the hypercube is rotated
        d = math.pi/300     #Hi-res rotation
        #d = math.pi/30     #Low-res rotation
       
        if event.type == "ESC": # Example if esc key pressed
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')  # then exit script
            return {'FINISHED'}
        elif event.type == "A":
                rotate_hypercube(d, 0., 0.)
                print("test")
        elif event.type == "D":
                rotate_hypercube(-d, 0., 0.)
        elif event.type == "W":
                rotate_hypercube(0., d, 0.)
        elif event.type == "S":
                rotate_hypercube(0., -d, 0.)
        elif event.type == "Q":
                rotate_hypercube(0., 0., d)
        elif event.type == "E":
                rotate_hypercube(0., 0., -d)  
        else:
                return {"RUNNING_MODAL"}
        
        return {'RUNNING_MODAL'}
    
    def execute(self, context):
        print("Executed")
        return {'PASS_THROUGH'}
        
    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            args = (self, context)
            self._handle = bpy.types.SpaceView3D.draw_handler_add(drawHandler, args, 'WINDOW', 'POST_PIXEL')
            self.mouse_path = [] 
                        
            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}

def register():
    bpy.utils.register_class(Hyper)

def unregister():
    bpy.utils.unregister_class(Hyper)

if __name__ == "__main__":
    register()

Pasteall link: https://pasteall.org/du4K


r/blenderpython • • May 22 '21

Deleting script-generated objects each time I run my script

4 Upvotes

What is the best practice so that every time I run my script, the objects created in the previous execution are deleted?

I want to create a setup() function and a corresponding teardown() function so to speak.


r/blenderpython • • Jun 01 '21

Is there a way of getting the right most vertex in world space without looping over all the vertices?

5 Upvotes

When I say "right", I mean if I were to loop over all the vertices, it would be the vertex with the larger x value. I've tried using object.bound_box but I don't think there's a way to convert them to world space.


r/blenderpython • • Jun 03 '21

What is your favourite devolopment env for Blender Python dev? This Visual Studio plugin by Jaques Lucke is often breaking on me, but it is the best I have found so far. Any suggestions?

6 Upvotes

I am making a multi file python addon as you do. Often when the code breaks the plugin needs me to shut down Blender then restart it from the Visual Studio pallette. Reload addons and reload scripts just gives errors about trying to register already registered classes.


r/blenderpython • • May 01 '23

Blender Tutorial - Procedural Modeling Masterclass Space Station Generator

Thumbnail
youtube.com
2 Upvotes

r/blenderpython • • May 07 '23

Section Box - Unbounded Cross Sections Blender Addon out now!

Thumbnail
youtu.be
5 Upvotes

r/blenderpython • • Jun 25 '16

Fractal (Mandelbrot) in 3d using Blender Python

Thumbnail
slicker.me
1 Upvotes

r/blenderpython • • Oct 19 '17

Need help running a python script in batch mode [crosspost from /r/blender]

Thumbnail
reddit.com
2 Upvotes

r/blenderpython • • Jun 20 '14

Would anyone happen to know how to apply forces (using python) to rigid body objects? (In the integrated system, not the game engine)

2 Upvotes

I'm trying to apply forces to rigid body objects, but because my simulations are getting larger and larger, I would like to be able to bake them to disk. The integrated physics system is perfect for this but I can't figure out how to apply a force to each object on each frame. I have a script written out for calculating the force but I can't find out how to get that into the simulation. Any help would be great, thank you!


r/blenderpython • • Dec 15 '16

GitHub - lowlevel86/blender-to-gnuplot: Export to gnuplot from blender 2.49b.

Thumbnail
github.com
2 Upvotes

r/blenderpython • • May 25 '17

Blender Discord Hit 100 Users

4 Upvotes

Hey I'm the owner of the fastest growing Blender Discord; I would like to say, YAY we made it to 100 members. If you would like to join make a Discord account and start chatting.

We have Young and old, experienced and new users. So it doesn't matter who you are just pop in!

Blender Discord


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 • • Nov 28 '20

Bmesh - applying scale

2 Upvotes

Hello, hoping I can get some help with bmesh ops in Python. After changing the dimensions of a bmesh I see that the inset behaves unevenly around the edges; in the same way that you can fix in the 3d view by applying scale. Is there a way to apply scale after resizing a bmesh in python?

Thank you in advance.


r/blenderpython • • Nov 19 '20

How do I connect two vertices in python

2 Upvotes

I have randomly generated objects that I want to connect like a spiderweb. I have the coordinates of the vertices I want connected. Is there a simple way to create a line(edge?) between the two points.

I am new to blender and every solution I have found has been a manual way of connecting points.

Thanks


r/blenderpython • • Dec 01 '20

Help learning the API

5 Upvotes

Hi !

I'm a beginner to programming addons and scripts for Blender (I have some knowledge in python tho)

I'm trying to create an addon which require to open several files as a sequence. I don't need to open them, I need to save their filepaths in the correct order. I would like to use the Built-in ImportHelper class, but i can't get it to read multiple filepaths at once.

Any thoughts ?


r/blenderpython • • Nov 13 '20

'new_from_object' and shape key data

1 Upvotes

Hi,

I'm porting a third-party import/export plugin from Blender 2.79 to 2.8. However, I'm having issues adapting its handling of temporary meshes and shape key data.

In preparation for export, it creates a copy of the mesh to operate upon without altering the original:

[Blender 2.79; 'object' is the currently selected model]

# Make a copy of the mesh to keep the original intact
mesh = object.to_mesh(bpy.context.scene, False, 'RENDER', False, False)

temp_obj = bpy.data.objects.new('temp_obj', mesh)

2.9's 'Object.to_mesh' no longer works in this context. To quote the doc, "the result is temporary and can not be used by objects from the main database."

My solution was to use:

# Make a copy of the mesh to keep the original intact
depsgraph = bpy.context.evaluated_depsgraph_get()
object_eval  = object.evaluated_get(depsgraph)
mesh = bpy.data.meshes.new_from_object(object_eval, preserve_all_data_layers=True, depsgraph=depsgraph)

temp_obj = bpy.data.objects.new(name='temp_obj', object_data=mesh)

Problem, this isn't copying the mesh's shape key data. Is there something I missed, or should I just rethink this whole process?