Blender python script library

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Python Scripts for Blender

License

SManAT/PyBlender

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This is info library for Python Scripts in Blender. I will collect here usefull tipps and examples of scripts to deal with blender >2.8.

Which Python does Blender use?

import sys sys.exec_prefix

Maybe that is C:\Program Files\Blender Foundation\Blender 2.82\2.82\python. If you want to use another Version of Python, just delete this folder.

Using pip Change within a terminal to the python path. Then you can use (Win)

.\bin\python.exe -m pip .\bin\python.exe -m pip install mathutils 

Where is my Console? To see errors and hints have a look at the system console. To do This Window > Toggle System Console

Some Basics for Beginners like me

bpy.context.scene.collection Scene Master collection

bpy.data.collections Main data structure and there are all collections

bpy.ops.collection Collection Operators

Include Python modules (libraries)

How to include your own libraries? You have to change the Python SystemPath (i call it).
E.q. you have your Modules in a subpath called subdir .

import sys sys.exec_prefix import os import bpy # get the Directory-Part from the path to the Blender File rootPath = os.path.abspath(os.path.join(os.path.dirname(bpy.data.filepath))) libPath = os.path.join(rootPath, "subdir") #add libPath to SystemPath sys.path.insert(0, libPath) print(sys.path) # now you can import your own modules, e.q. import myClass
import math import sys import os import bpy rootPath = os.path.abspath(os.path.join(os.path.dirname(bpy.data.filepath))) libPath = os.path.join(rootPath, "../libs") sys.path.insert(0, libPath) from Collection import Collection from BlenderStuff import BlenderStuff from Object import Object # variables myScene = bpy.context.scene C = bpy.context D = bpy.data # Main Program ================================================================= output_collection = "Output" BStuff = BlenderStuff() _Collection = Collection() _Object = Object() # delete if exists and create it new _Collection.create_Collection(output_collection) """Main Entry Point""" # 2Do bpy.ops.wm.save_as_mainfile(filepath="pysaved.blend") 

Use the lib LSystem3D.py. The turtel has the following vectors in 3D

turtle

import bpy import os import sys rootPath = os.path.abspath(os.path.join(os.path.dirname(bpy.data.filepath))) libPath = os.path.join(rootPath, "../lib") sys.path.insert(0, libPath) from BlenderStuff import BlenderStuff from LSystem3D import LSystem3D from mathutils import Vector #Lindenmayer System ---------------------------------- LSys = LSystem3D() #no iterations here, just a test LSys._alpha = 120 LSys.rotate_around_H(45) #Draw a equilateral triangle LSys._code = "F&F&F" LSys.calculatePoints() LSys.printVectorlist() listOfVectors = LSys.getVectorList() #Blender Part --------------------------------------- BStuff = BlenderStuff() coll = "Path" BStuff.create_Collection(coll) BStuff.MakePolyLine("LSysTest", "LSysTest", listOfVectors, coll)

About

Python Scripts for Blender

Источник

Introduction:

This reference documents the Blender Python API, a growing collection of Python modules (libraries) that give access to part of the program’s internal data and functions.

Through scripting Blender can be extended in real-time via Python, an impressive high level, multi-paradigm, open source language. Newcomers are recommended to start with the tutorial that comes with it.

This opens many interesting possibilities, ranging from automating repetitive tasks to adding new functionality to the program: procedural models, importers and exporters, even complex applications and so on. Blender itself comes with some scripts, but many others can be found in the Scripts & Plugins sections and forum posts at the Blender-related sites listed below.

Scripting and Blender:

  1. They can be loaded or typed as text files in the Text Editor window, then executed with ALT+P.
  2. Via command line: ‘blender -P ‘ will start Blender and executed the given script. can be a filename in the user’s file system or the name of a text saved in a .blend Blender file: ‘blender myfile.blend -P textname’.
  3. Properly registered scripts can be selected directly from the program’s menus.
  4. Scriptlinks: these are also loaded or typed in the Text Editor window and can be linked to objects, materials or scenes using the Scriptlink buttons tab. Script links get executed automatically when their events (ONLOAD, REDRAW, FRAMECHANGED) are triggered. Normal scripts can create ( Text ) and link other scripts to objects and events, see Object.Object.addScriptLink , for example.

Registering scripts:

  • be either in the default scripts dir or in the user defined scripts path (see Info window, paths tab);
  • have a proper header.

Try ‘blender -d’ to know where your default dir for scripts is, it will inform either the dir or the file with that info already parsed, which is in the same dir of the scripts folder.

The header should be like this one (all double and single apostrophes below are required):

#!BPY """ Name: 'Script Name' Blender: 233 Group: 'Export' Submenu: 'All' all Submenu: 'Selected' sel Submenu: 'Configure (gui)' gui Tooltip: 'Export to some format.' """
  • Name is the string that will appear in the menu;
  • Blender is the minimum program version required to run the script;
  • Group defines where the script will be put, see all groups in the Scripts Window’s header, menu «Scripts»;
  • Submenu adds optional submenus for further control;
  • Tooltip is the (short) tooltip string for the menu entry.

Submenu lines are not required, use them if you want to provide extra options. To see which submenu the user chose, check the «__script__» dictionary in your code: __script__[‘arg’] has the defined keyword (the word after the submenu string name: all, sel or gui in the example above) of the chosen submenu. For example, if the user clicked on submenu ‘Selected’ above, __script__[‘arg’] will be «sel».

If your script requires extra data or configuration files, there is a special folder where they can be saved: see ‘datadir’ in Blender.Get .

Interaction with users:

  • simply run and exit;
  • grab the main input event queue and process (or pass to Blender) selected keyboard, mouse, redraw events;
  • pop messages, menus and small number and text input boxes;
  • draw graphical user interfaces (guis) with OpenGL calls and native program buttons, which stay there accepting user input like any other Blender window until the user closes them;
  • make changes to the 3D View (set visible layer(s), view point, etc);
  • use external Python libraries, if available.

Command line mode:

Python was embedded in Blender, so to access bpython modules you need to run scripts from the program itself: you can’t import the Blender module into an external Python interpreter. But with «OnLoad» script links, the «-b» background mode and additions like the «-P» command line switch, Blender.Save , Blender.Load , Blender.Quit and the Library module, for many tasks it’s possible to control Blender via some automated process using scripts.

Demo mode:

Blender has a demo mode, where once started it can work without user intervention, «showing itself off». Demos can render stills and animations, play rendered or real-time animations, calculate radiosity simulations and do many other nifty things. If you want to turn a .blend file into a demo, write a script to run the show and link it as a scene «OnLoad» scriptlink. The demo will then be played automatically whenever this .blend file is opened, unless Blender was started with the «-y» parameter.

The Game Engine API:

Blender has a game engine for users to create and play 3d games. This engine lets programmers add scripts to improve game AI, control, etc, making more complex interaction and tricks possible. The game engine API is separate from the Blender Python API this document references and you can find its own ref doc in the docs section of the main sites below.

Blender Data Structures:

Programs manipulate data structures. Blender python scripts are no exception. Blender uses an Object Oriented architecture. The bpython interface tries to present Blender objects and their attributes in the same way you see them through the User Interface ( the GUI ). One key to bpython programming is understanding the information presented in Blender’s OOPS window where Blender objects and their relationships are displayed.

Each Blender graphic element ( Mesh, Lamp, Curve, etc.) is composed from two parts: An Object and ObData. The Object holds information about the position, rotation and size of the element. This is information that all elements have in common. The ObData holds information specific to that particular type of element.

Each Object has a link to its associated ObData. A single ObData may be shared by many Objects. A graphic element also has a link to a list of Materials. By default, this list is associated with the ObData.

All Blender objects have a unique name. However, the name is qualified by the type of the object. This means you can have a Lamp Object called Lamp.001 ( OB:Lamp.001 ) and a Lamp ObData called Lamp.001 ( LA:Lamp.001 )

For a more in-depth look at Blender internals, and some understanding of why Blender works the way it does, see the Blender Architecture document.

A note to newbie script writers:

Interpreted languages are known to be much slower than compiled code, but for many applications the difference is negligible or acceptable. Also, with profiling to identify slow areas and well thought optimizations, the speed can be considerably improved in many cases. Try some of the best bpython scripts to get an idea of what can be done, you may be surprised.

Author: The Blender Python Team

Note: this documentation was generated by epydoc, which can output html and pdf. For pdf it requires a working LaTeX environment.

Requires: Blender 2.34 or newer.

Источник

Читайте также:  Php convert time to unix time
Оцените статью